Skip to content

Commit

Permalink
Merge pull request #30 from jeroendk/deprecated-commands
Browse files Browse the repository at this point in the history
Removed container dependencies
  • Loading branch information
Nightbr authored Oct 21, 2019
2 parents 7aa801c + d22f46f commit 6065060
Show file tree
Hide file tree
Showing 6 changed files with 170 additions and 94 deletions.
47 changes: 28 additions & 19 deletions src/Command/SynchronizeMergeFieldsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,44 +2,53 @@

namespace Welp\MailchimpBundle\Command;

use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
use Welp\MailchimpBundle\Provider\ListProviderInterface;
use Welp\MailchimpBundle\Subscriber\ListSynchronizer;

class SynchronizeMergeFieldsCommand extends ContainerAwareCommand
class SynchronizeMergeFieldsCommand extends Command
{
/**
* List Synchronizer.
*
* @var ListSynchronizer
*/
private $listSynchronizer;

/**
* The configured list provider.
*
* @var ListProviderInterface
*/
private $listProvider;

public function __construct(ListSynchronizer $listSynchronizer, ListProviderInterface $listProvider)
{
$this->listSynchronizer = $listSynchronizer;
$this->listProvider = $listProvider;

parent::__construct();
}

protected function configure()
{
$this
->setDescription('Synchronizing merge fields in MailChimp')
->setName('welp:mailchimp:synchronize-merge-fields')
// @TODO add params : listId
;
}
}

protected function execute(InputInterface $input, OutputInterface $output)
{
$output->writeln(sprintf('<info>%s</info>', $this->getDescription()));

$listProviderKey = $this->getContainer()->getParameter('welp_mailchimp.list_provider');
try {
$listProvider = $this->getContainer()->get($listProviderKey);
} catch (ServiceNotFoundException $e) {
throw new \InvalidArgumentException(sprintf('List Provider "%s" should be defined as a service.', $listProviderKey), $e->getCode(), $e);
}

if (!$listProvider instanceof ListProviderInterface) {
throw new \InvalidArgumentException(sprintf('List Provider "%s" should implement Welp\MailchimpBundle\Provider\ListProviderInterface.', $listProviderKey));
}

$lists = $listProvider->getLists();
$lists = $this->listProvider->getLists();

foreach ($lists as $list) {
$this->getContainer()->get('welp_mailchimp.list_synchronizer')
->synchronizeMergeFields($list->getListId(), $list->getMergeFields());
;
$this->listSynchronizer->synchronizeMergeFields($list->getListId(), $list->getMergeFields());
}
}
}
84 changes: 56 additions & 28 deletions src/Command/SynchronizeSubscribersCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,48 @@

namespace Welp\MailchimpBundle\Command;

use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use DrewM\MailChimp\MailChimp;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
use Welp\MailchimpBundle\Provider\ListProviderInterface;
use Welp\MailchimpBundle\Subscriber\ListSynchronizer;

class SynchronizeSubscribersCommand extends ContainerAwareCommand
class SynchronizeSubscribersCommand extends Command
{
/**
* @inheritDoc
* List Synchronizer.
*
* @var ListSynchronizer
*/
private $listSynchronizer;

/**
* The configured list provider.
*
* @var ListProviderInterface
*/
private $listProvider;

/**
* Mailchimp API class.
*
* @var MailChimp
*/
private $mailchimp;

public function __construct(ListSynchronizer $listSynchronizer, ListProviderInterface $listProvider, MailChimp $mailchimp)
{
$this->listSynchronizer = $listSynchronizer;
$this->listProvider = $listProvider;
$this->mailchimp = $mailchimp;

parent::__construct();
}

/**
* {@inheritdoc}
*/
protected function configure()
{
Expand All @@ -30,28 +61,17 @@ protected function configure()
}

/**
* @inheritDoc
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$output->writeln(sprintf('<info>%s</info>', $this->getDescription()));
$listProviderKey = $this->getContainer()->getParameter('welp_mailchimp.list_provider');
try {
$listProvider = $this->getContainer()->get($listProviderKey);
} catch (ServiceNotFoundException $e) {
throw new \InvalidArgumentException(sprintf('List Provider "%s" should be defined as a service.', $listProviderKey), $e->getCode(), $e);
}

if (!$listProvider instanceof ListProviderInterface) {
throw new \InvalidArgumentException(sprintf('List Provider "%s" should implement Welp\MailchimpBundle\Provider\ListProviderInterface.', $listProviderKey));
}
$lists = $this->listProvider->getLists();

$lists = $listProvider->getLists();

foreach ($lists as $list) {

foreach ($lists as $list) {
$output->writeln(sprintf('Synchronize list %s', $list->getListId()));
$batchesResult = $this->getContainer()->get('welp_mailchimp.list_synchronizer')->synchronize($list);
$batchesResult = $this->listSynchronizer->synchronize($list);
if ($input->getOption('follow-sync')) {
while (!$this->batchesFinished($batchesResult)) {
$batchesResult = $this->refreshBatchesResult($batchesResult);
Expand All @@ -65,48 +85,56 @@ protected function execute(InputInterface $input, OutputInterface $output)
}

/**
* Refresh all batch from MailChimp API
* Refresh all batch from MailChimp API.
*
* @param array $batchesResult
*
* @return array
*/
private function refreshBatchesResult($batchesResult)
{
$refreshedBatchsResults = [];
$mailchimp = $this->getContainer()->get('welp_mailchimp.mailchimp_master');

foreach ($batchesResult as $key => $batch) {
$batch = $mailchimp->get("batches/".$batch['id']);
$batch = $this->mailchimp->get('batches/'.$batch['id']);
array_push($refreshedBatchsResults, $batch);
}

return $refreshedBatchsResults;
}

/**
* Test if all batches are finished
* Test if all batches are finished.
*
* @param array $batchesResult
*
* @return bool
*/
private function batchesFinished($batchesResult)
{
$allfinished = true;
foreach ($batchesResult as $key => $batch) {
if ($batch['status'] != 'finished') {
if ('finished' != $batch['status']) {
$allfinished = false;
}
}

return $allfinished;
}

/**
* Pretty display of batch info
* Pretty display of batch info.
*
* @param array $batch
*
* @return string
*/
private function displayBatchInfo($batch)
{
if ($batch['status'] == 'finished') {
if ('finished' == $batch['status']) {
return sprintf('batch %s is finished, operations %d/%d with %d errors. http responses: %s', $batch['id'], $batch['finished_operations'], $batch['total_operations'], $batch['errored_operations'], $batch['response_body_url']);
} else {
return sprintf('batch %s, current status %s, operations %d/%d with %d errors', $batch['id'], $batch['status'], $batch['finished_operations'], $batch['total_operations'], $batch['errored_operations']);
}

return sprintf('batch %s, current status %s, operations %d/%d with %d errors', $batch['id'], $batch['status'], $batch['finished_operations'], $batch['total_operations'], $batch['errored_operations']);
}
}
45 changes: 27 additions & 18 deletions src/Command/WebhookCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,36 @@

namespace Welp\MailchimpBundle\Command;

use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
use Welp\MailchimpBundle\Provider\ListProviderInterface;
use Welp\MailchimpBundle\Subscriber\ListRepository;

class WebhookCommand extends ContainerAwareCommand
class WebhookCommand extends Command
{
/**
* The configured list provider.
*
* @var ListProviderInterface
*/
private $listProvider;

/**
* The configured list repository.
*
* @var ListRepository
*/
private $listRepository;

public function __construct(ListProviderInterface $listProvider, ListRepository $listRepository)
{
$this->listProvider = $listProvider;
$this->listRepository = $listRepository;

parent::__construct();
}

protected function configure()
{
$this
Expand All @@ -23,27 +45,14 @@ protected function execute(InputInterface $input, OutputInterface $output)
{
$output->writeln(sprintf('<info>%s</info>', $this->getDescription()));

$listProviderKey = $this->getContainer()->getParameter('welp_mailchimp.list_provider');
try {
$listProvider = $this->getContainer()->get($listProviderKey);
} catch (ServiceNotFoundException $e) {
throw new \InvalidArgumentException(sprintf('List Provider "%s" should be defined as a service.', $listProviderKey), $e->getCode(), $e);
}

if (!$listProvider instanceof ListProviderInterface) {
throw new \InvalidArgumentException(sprintf('List Provider "%s" should implement Welp\MailchimpBundle\Provider\ListProviderInterface.', $listProviderKey));
}

$lists = $listProvider->getLists();
$lists = $this->listProvider->getLists();

foreach ($lists as $list) {
$url = $list->getWebhookUrl().'?hooksecret='.$list->getWebhookSecret();
$output->writeln('Add webhook to list: '.$list->getListId());
$output->writeln('Webhook url: '.$url);

$this->getContainer()->get('welp_mailchimp.list_repository')
->registerMainWebhook($list->getListId(), $url);
;
$this->listRepository->registerMainWebhook($list->getListId(), $url);
}

$output->writeln('✔ done');
Expand Down
26 changes: 22 additions & 4 deletions src/DependencyInjection/WelpMailchimpExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@

namespace Welp\MailchimpBundle\DependencyInjection;

use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;

class WelpMailchimpExtension extends Extension
{
Expand All @@ -14,15 +15,32 @@ public function load(array $configs, ContainerBuilder $container)
$configuration = new Configuration();
$config = $this->processConfiguration($configuration, $configs);
$container->setParameter('welp_mailchimp.lists', $config['lists']);
$container->setParameter('welp_mailchimp.list_provider',$config['list_provider']);

$container->setParameter('welp_mailchimp.list_provider', $config['list_provider']);
$container->setParameter('welp_mailchimp.api_key', isset($config['api_key']) ? $config['api_key'] : null);

$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config'));
$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('services.yml');

// create an alias for the chosen list provider service
$alias = $config['list_provider'];
$container->setAlias('welp_mailchimp.list_provider.current', $alias);

// Load all the used subscriber providers in the factory
$this->loadSubscriberProviders($container, $config['lists']);
}

public function getAlias()
{
return 'welp_mailchimp';
}

public function loadSubscriberProviders(ContainerBuilder $container, $lists)
{
$providerFactory = $container->getDefinition('welp_mailchimp.provider.factory');
foreach ($lists as $list) {
$providerKey = $list['subscriber_provider'];
$providerFactory->addMethodCall('addProvider', [$providerKey, new Reference($providerKey)]);
}
}
}
Loading

0 comments on commit 6065060

Please sign in to comment.