From b2b9d040e467fd283038b3f840a955dd3c626932 Mon Sep 17 00:00:00 2001 From: Jeroen de Kok Date: Sat, 11 Nov 2017 19:12:55 +0100 Subject: [PATCH 1/8] - Added optional list_provider paramter to the config - Added a ListProviderInterface and the default "ConfigListProvider" that parses the list configuration from the config file. - Changed the Sync command to use the list provider for getting the lists - Added provider factory for the creation of the chosen provider class, so the list provider can use this service --- src/Command/SynchronizeSubscribersCommand.php | 48 ++++++------------- src/DependencyInjection/Configuration.php | 3 ++ .../WelpMailchimpExtension.php | 1 + src/Provider/ConfigListProvider.php | 38 +++++++++++++++ src/Provider/ListProviderInterface.php | 15 ++++++ src/Provider/ProviderFactory.php | 36 ++++++++++++++ src/Resources/config/services.yml | 13 +++++ 7 files changed, 121 insertions(+), 33 deletions(-) create mode 100644 src/Provider/ConfigListProvider.php create mode 100644 src/Provider/ListProviderInterface.php create mode 100644 src/Provider/ProviderFactory.php diff --git a/src/Command/SynchronizeSubscribersCommand.php b/src/Command/SynchronizeSubscribersCommand.php index 5495c64..5f0df28 100644 --- a/src/Command/SynchronizeSubscribersCommand.php +++ b/src/Command/SynchronizeSubscribersCommand.php @@ -7,9 +7,7 @@ use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException; - -use Welp\MailchimpBundle\Provider\ProviderInterface; -use Welp\MailchimpBundle\Subscriber\SubscriberList; +use Welp\MailchimpBundle\Provider\ListProviderInterface; class SynchronizeSubscribersCommand extends ContainerAwareCommand { @@ -37,19 +35,23 @@ protected function configure() protected function execute(InputInterface $input, OutputInterface $output) { $output->writeln(sprintf('%s', $this->getDescription())); - - $lists = $this->getContainer()->getParameter('welp_mailchimp.lists'); - if (sizeof($lists) == 0) { - throw new \RuntimeException("No Mailchimp list has been defined. Check the your config.yml file based on MailchimpBundle's README.md"); + $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); } - foreach ($lists as $listId => $listParameters) { - $providerServiceKey = $listParameters['subscriber_provider']; - - $provider = $this->getProvider($providerServiceKey); - $list = new SubscriberList($listId, $provider); + if (!$listProvider instanceof ListProviderInterface) { + throw new \InvalidArgumentException(sprintf('List Provider "%s" should implement Welp\MailchimpBundle\Provider\ListProviderInterface.', $listProviderKey)); + } + - $output->writeln(sprintf('Synchronize list %s', $listId)); + $lists = $listProvider->getLists(); + + foreach ($lists as $list) { + + $output->writeln(sprintf('Synchronize list %s', $list->getListId())); $batchesResult = $this->getContainer()->get('welp_mailchimp.list_synchronizer')->synchronize($list); if ($input->getOption('follow-sync')) { while (!$this->batchesFinished($batchesResult)) { @@ -108,24 +110,4 @@ private function displayBatchInfo($batch) 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']); } } - - /** - * Get subscriber provider - * @param string $providerServiceKey - * @return ProviderInterface $provider - */ - private function getProvider($providerServiceKey) - { - try { - $provider = $this->getContainer()->get($providerServiceKey); - } catch (ServiceNotFoundException $e) { - throw new \InvalidArgumentException(sprintf('Provider "%s" should be defined as a service.', $providerServiceKey), $e->getCode(), $e); - } - - if (!$provider instanceof ProviderInterface) { - throw new \InvalidArgumentException(sprintf('Provider "%s" should implement Welp\MailchimpBundle\Provider\ProviderInterface.', $providerServiceKey)); - } - - return $provider; - } } diff --git a/src/DependencyInjection/Configuration.php b/src/DependencyInjection/Configuration.php index 4bdab45..7cb5093 100644 --- a/src/DependencyInjection/Configuration.php +++ b/src/DependencyInjection/Configuration.php @@ -25,6 +25,9 @@ public function getConfigTreeBuilder() ->scalarNode('api_key') ->isRequired() ->end() + ->scalarNode('list_provider') + ->defaultValue('welp_mailchimp.list_provider') + ->end() // lists ->arrayNode('lists') ->useAttributeAsKey('listId') diff --git a/src/DependencyInjection/WelpMailchimpExtension.php b/src/DependencyInjection/WelpMailchimpExtension.php index d4c4d94..545e39d 100644 --- a/src/DependencyInjection/WelpMailchimpExtension.php +++ b/src/DependencyInjection/WelpMailchimpExtension.php @@ -14,6 +14,7 @@ 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.api_key', isset($config['api_key']) ? $config['api_key'] : null); $loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config')); diff --git a/src/Provider/ConfigListProvider.php b/src/Provider/ConfigListProvider.php new file mode 100644 index 0000000..cd68204 --- /dev/null +++ b/src/Provider/ConfigListProvider.php @@ -0,0 +1,38 @@ +lists = $lists; + $this->providerFactory = $providerFactory; + } + + /** + * Default list provider, retrieve the lists from the config file. + * {@inheritdoc} + */ + public function getLists() + { + if (sizeof($this->lists) == 0) { + throw new \RuntimeException("No Mailchimp list has been defined. Check the your config.yml file based on MailchimpBundle's README.md"); + } + $lists = array(); + foreach ($this->lists as $listId => $listParameters) { + $providerServiceKey = $listParameters['subscriber_provider']; + + $provider = $this->providerFactory->create($providerServiceKey); + $lists[] = new SubscriberList($listId, $provider); + } + + return $lists; + } +} diff --git a/src/Provider/ListProviderInterface.php b/src/Provider/ListProviderInterface.php new file mode 100644 index 0000000..f040401 --- /dev/null +++ b/src/Provider/ListProviderInterface.php @@ -0,0 +1,15 @@ +container = $container; + } + + /** + * Get subscriber provider + * @param string $providerServiceKey + * @return ProviderInterface $provider + */ + public function create($providerServiceKey) + { + try { + $provider = $this->container->get($providerServiceKey); + } catch (ServiceNotFoundException $e) { + throw new \InvalidArgumentException(sprintf('Provider "%s" should be defined as a service.', $providerServiceKey), $e->getCode(), $e); + } + + if (!$provider instanceof ProviderInterface) { + throw new \InvalidArgumentException(sprintf('Provider "%s" should implement Welp\MailchimpBundle\Provider\ProviderInterface.', $providerServiceKey)); + } + + return $provider; + } +} \ No newline at end of file diff --git a/src/Resources/config/services.yml b/src/Resources/config/services.yml index 20d32fa..75db023 100644 --- a/src/Resources/config/services.yml +++ b/src/Resources/config/services.yml @@ -2,6 +2,8 @@ parameters: welp_mailchimp.list_synchronizer_class: Welp\MailchimpBundle\Subscriber\ListSynchronizer welp_mailchimp.list_repository_class: Welp\MailchimpBundle\Subscriber\ListRepository welp_mailchimp.subscriber_listener_class: Welp\MailchimpBundle\Event\SubscriberListener + welp_mailchimp.provider_factory_class: Welp\MailchimpBundle\Provider\ProviderFactory + welp_mailchimp.config_list_provider_class: Welp\MailchimpBundle\Provider\ConfigListProvider services: # MailChimp @@ -18,6 +20,17 @@ services: class: "%welp_mailchimp.list_synchronizer_class%" arguments: - '@welp_mailchimp.list_repository' + + welp_mailchimp.provider.factory: + class: "%welp_mailchimp.provider_factory_class%" + arguments: + - '@service_container' + + welp_mailchimp.list_provider: + class: "%welp_mailchimp.config_list_provider_class%" + arguments: + - '@welp_mailchimp.provider.factory' + - '%welp_mailchimp.lists%' # Events welp_mailchimp.subscriber_listener: From d855f41aaaa3c72854507172b298c00db81f6464 Mon Sep 17 00:00:00 2001 From: Jeroen de Kok Date: Sat, 11 Nov 2017 21:08:31 +0100 Subject: [PATCH 2/8] - Added mergeFields as attribute in SubscriberList. - Modified Sync Merge Fields command to use listProvider. --- src/Command/SynchronizeMergeFieldsCommand.php | 22 ++++++++++++++----- src/Provider/ConfigListProvider.php | 2 +- src/Subscriber/SubscriberList.php | 19 +++++++++++++++- 3 files changed, 35 insertions(+), 8 deletions(-) diff --git a/src/Command/SynchronizeMergeFieldsCommand.php b/src/Command/SynchronizeMergeFieldsCommand.php index 1ad0150..fc44ab1 100644 --- a/src/Command/SynchronizeMergeFieldsCommand.php +++ b/src/Command/SynchronizeMergeFieldsCommand.php @@ -5,6 +5,8 @@ use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; +use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException; +use Welp\MailchimpBundle\Provider\ListProviderInterface; class SynchronizeMergeFieldsCommand extends ContainerAwareCommand { @@ -15,20 +17,28 @@ protected function configure() ->setName('welp:mailchimp:synchronize-merge-fields') // @TODO add params : listId ; - } + } protected function execute(InputInterface $input, OutputInterface $output) { $output->writeln(sprintf('%s', $this->getDescription())); - $lists = $this->getContainer()->getParameter('welp_mailchimp.lists'); - if (sizeof($lists) == 0) { - throw new \RuntimeException("No Mailchimp list has been defined. Check the your config.yml file based on MailchimpBundle's README.md"); + $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)); } - foreach ($lists as $listId => $listParameters) { + $lists = $listProvider->getLists(); + + foreach ($lists as $list) { $this->getContainer()->get('welp_mailchimp.list_synchronizer') - ->synchronizeMergeFields($listId, $listParameters['merge_fields']); + ->synchronizeMergeFields($list->getListId(), $list->getMergeFields()); ; } } diff --git a/src/Provider/ConfigListProvider.php b/src/Provider/ConfigListProvider.php index cd68204..2f574f7 100644 --- a/src/Provider/ConfigListProvider.php +++ b/src/Provider/ConfigListProvider.php @@ -30,7 +30,7 @@ public function getLists() $providerServiceKey = $listParameters['subscriber_provider']; $provider = $this->providerFactory->create($providerServiceKey); - $lists[] = new SubscriberList($listId, $provider); + $lists[] = new SubscriberList($listId, $provider, $listParameters['merge_fields']); } return $lists; diff --git a/src/Subscriber/SubscriberList.php b/src/Subscriber/SubscriberList.php index 278847a..c097711 100644 --- a/src/Subscriber/SubscriberList.php +++ b/src/Subscriber/SubscriberList.php @@ -21,15 +21,23 @@ class SubscriberList */ protected $provider; + /** + * Merge fields + * @var array + */ + protected $mergeFields; + /** * * @param string $listId * @param ProviderInterface $provider + * @param array $mergeFields */ - public function __construct($listId, ProviderInterface $provider) + public function __construct($listId, ProviderInterface $provider, array $mergeFields = array()) { $this->listId = $listId; $this->provider = $provider; + $this->mergeFields = $mergeFields; } /** @@ -49,4 +57,13 @@ public function getProvider() { return $this->provider; } + + /** + * Get the list merge fields + * @return array + */ + public function getMergeFields() + { + return $this->mergeFields; + } } From d0f8e771d5659acba6fe957d67854733ec0b251f Mon Sep 17 00:00:00 2001 From: Jeroen de Kok Date: Sat, 11 Nov 2017 21:23:58 +0100 Subject: [PATCH 3/8] - Added two attributes to SubscriberList for the webhook info - Modified the WebhookCommand to use the listProvider. --- src/Command/SynchronizeSubscribersCommand.php | 3 +- src/Command/WebhookCommand.php | 24 +++++++--- src/Provider/ConfigListProvider.php | 5 +- src/Subscriber/SubscriberList.php | 48 +++++++++++++++++++ 4 files changed, 70 insertions(+), 10 deletions(-) diff --git a/src/Command/SynchronizeSubscribersCommand.php b/src/Command/SynchronizeSubscribersCommand.php index 5f0df28..14d548a 100644 --- a/src/Command/SynchronizeSubscribersCommand.php +++ b/src/Command/SynchronizeSubscribersCommand.php @@ -44,8 +44,7 @@ protected function execute(InputInterface $input, OutputInterface $output) if (!$listProvider instanceof ListProviderInterface) { throw new \InvalidArgumentException(sprintf('List Provider "%s" should implement Welp\MailchimpBundle\Provider\ListProviderInterface.', $listProviderKey)); - } - + } $lists = $listProvider->getLists(); diff --git a/src/Command/WebhookCommand.php b/src/Command/WebhookCommand.php index cdc8101..9f87514 100644 --- a/src/Command/WebhookCommand.php +++ b/src/Command/WebhookCommand.php @@ -5,6 +5,8 @@ use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; +use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException; +use Welp\MailchimpBundle\Provider\ListProviderInterface; class WebhookCommand extends ContainerAwareCommand { @@ -21,18 +23,26 @@ protected function execute(InputInterface $input, OutputInterface $output) { $output->writeln(sprintf('%s', $this->getDescription())); - $lists = $this->getContainer()->getParameter('welp_mailchimp.lists'); - if (sizeof($lists) == 0) { - throw new \RuntimeException("No Mailchimp list has been defined. Check the your config.yml file based on MailchimpBundle's README.md"); + $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); } - foreach ($lists as $listId => $listParameters) { - $url = $listParameters['webhook_url'].'?hooksecret='.$listParameters['webhook_secret']; - $output->writeln('Add webhook to list: '.$listId); + if (!$listProvider instanceof ListProviderInterface) { + throw new \InvalidArgumentException(sprintf('List Provider "%s" should implement Welp\MailchimpBundle\Provider\ListProviderInterface.', $listProviderKey)); + } + + $lists = $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($listId, $url); + ->registerMainWebhook($list->getListId(), $url); ; } diff --git a/src/Provider/ConfigListProvider.php b/src/Provider/ConfigListProvider.php index 2f574f7..ffbb5d3 100644 --- a/src/Provider/ConfigListProvider.php +++ b/src/Provider/ConfigListProvider.php @@ -30,7 +30,10 @@ public function getLists() $providerServiceKey = $listParameters['subscriber_provider']; $provider = $this->providerFactory->create($providerServiceKey); - $lists[] = new SubscriberList($listId, $provider, $listParameters['merge_fields']); + $subscriberList = new SubscriberList($listId, $provider, $listParameters['merge_fields']); + $subscriberList->setWebhookUrl($listParameters['webhook_url']); + $subscriberList->setWebhookSecret($listParameters['webhook_secret']); + $lists[] = $subscriberList; } return $lists; diff --git a/src/Subscriber/SubscriberList.php b/src/Subscriber/SubscriberList.php index c097711..dcaf3d8 100644 --- a/src/Subscriber/SubscriberList.php +++ b/src/Subscriber/SubscriberList.php @@ -27,6 +27,18 @@ class SubscriberList */ protected $mergeFields; + /** + * MailChimp webhook URL + * @var string + */ + protected $webhookUrl; + + /** + * MailChimp webhook secret + * @var string + */ + protected $webhookSecret; + /** * * @param string $listId @@ -66,4 +78,40 @@ public function getMergeFields() { return $this->mergeFields; } + + /** + * Get the list webhook URL + * @return string + */ + public function getWebhookUrl() + { + return $this->webhookUrl; + } + + /** + * Set the list webhook URL + * @param string + */ + public function setWebhookUrl($webhookUrl) + { + $this->webhookUrl = $webhookUrl; + } + + /** + * Get the list webhook secret + * @return string + */ + public function getWebhookSecret() + { + return $this->webhookSecret; + } + + /** + * Set the list webhook secret + * @param string + */ + public function setWebhookSecret($webhookSecret) + { + $this->webhookSecret = $webhookSecret; + } } From 6dd000faa7e9acee8c94170e1a573b8d26b43831 Mon Sep 17 00:00:00 2001 From: Jeroen de Kok Date: Sat, 11 Nov 2017 21:47:19 +0100 Subject: [PATCH 4/8] - added method to listProvider for retrieving one list. - Modified webhookcontroller to use listprovider. --- src/Controller/WebhookController.php | 23 +++++++++----- src/Provider/ConfigListProvider.php | 42 +++++++++++++++++++++----- src/Provider/ListProviderInterface.php | 6 ++++ 3 files changed, 56 insertions(+), 15 deletions(-) diff --git a/src/Controller/WebhookController.php b/src/Controller/WebhookController.php index a4b1047..a0e2809 100644 --- a/src/Controller/WebhookController.php +++ b/src/Controller/WebhookController.php @@ -9,6 +9,7 @@ use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException; +use Welp\MailchimpBundle\Provider\ListProviderInterface; use Welp\MailchimpBundle\Event\WebhookEvent; /** @@ -58,16 +59,24 @@ public function indexAction(Request $request) } $listId = $data['list_id']; - $lists = $this->getParameter('welp_mailchimp.lists'); + + $listProviderKey = $this->getParameter('welp_mailchimp.list_provider'); + try { + $listProvider = $this->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)); + } + + $list = $listProvider->getList($listId); // Check the webhook_secret $authorized = false; - foreach ($lists as $id => $listParameters) { - if ($listId == $id) { - if ($listParameters['webhook_secret'] == $hooksecret) { - $authorized = true; - } - } + if($list != null && $list->getWebhookSecret() == $hooksecret) { + $authorized = true; } if (!$authorized) { diff --git a/src/Provider/ConfigListProvider.php b/src/Provider/ConfigListProvider.php index ffbb5d3..bc39e41 100644 --- a/src/Provider/ConfigListProvider.php +++ b/src/Provider/ConfigListProvider.php @@ -26,16 +26,42 @@ public function getLists() throw new \RuntimeException("No Mailchimp list has been defined. Check the your config.yml file based on MailchimpBundle's README.md"); } $lists = array(); - foreach ($this->lists as $listId => $listParameters) { - $providerServiceKey = $listParameters['subscriber_provider']; + foreach ($this->lists as $listId => $listParameters) + { + $lists[] = $this->createList($listId, $listParameters); + } + return $lists; + } - $provider = $this->providerFactory->create($providerServiceKey); - $subscriberList = new SubscriberList($listId, $provider, $listParameters['merge_fields']); - $subscriberList->setWebhookUrl($listParameters['webhook_url']); - $subscriberList->setWebhookSecret($listParameters['webhook_secret']); - $lists[] = $subscriberList; + /** + * Default list provider, retrieve one list from the config file. + * {@inheritdoc} + */ + public function getList($listId) + { + foreach ($this->lists as $id => $listParameters) + { + if($id == $listId) + { + return $this->createList($id, $listParameters); + } } + } + + /** + * create one SubscriberList + * @param string $listId + * @param string $listParameters + * @return SubscriberList + */ + private function createList($listId, $listParameters) + { + $providerServiceKey = $listParameters['subscriber_provider']; + $provider = $this->providerFactory->create($providerServiceKey); + $subscriberList = new SubscriberList($listId, $provider, $listParameters['merge_fields']); + $subscriberList->setWebhookUrl($listParameters['webhook_url']); + $subscriberList->setWebhookSecret($listParameters['webhook_secret']); - return $lists; + return $subscriberList; } } diff --git a/src/Provider/ListProviderInterface.php b/src/Provider/ListProviderInterface.php index f040401..b650954 100644 --- a/src/Provider/ListProviderInterface.php +++ b/src/Provider/ListProviderInterface.php @@ -12,4 +12,10 @@ interface ListProviderInterface * @return array of SubscriberList */ public function getLists(); + + /** + * Get one Mailchimp list by id + * @return SubscriberList + */ + public function getList($listId); } From 0d8b5a871ef7ab7c474ee8a156db8d5c0a734701 Mon Sep 17 00:00:00 2001 From: Jeroen de Kok Date: Mon, 13 Nov 2017 22:14:27 +0100 Subject: [PATCH 5/8] Added tests for the configListProvider --- tests/Provider/configListProviderTest.php | 100 ++++++++++++++++++++++ 1 file changed, 100 insertions(+) create mode 100644 tests/Provider/configListProviderTest.php diff --git a/tests/Provider/configListProviderTest.php b/tests/Provider/configListProviderTest.php new file mode 100644 index 0000000..21e7229 --- /dev/null +++ b/tests/Provider/configListProviderTest.php @@ -0,0 +1,100 @@ +providerFactory = $this->createMock(\Welp\MailchimpBundle\Provider\ProviderFactory::class); + + $this->providerFactory + ->method('create') + ->will($this->returnValue($this->createMock(\Welp\MailchimpBundle\Provider\FosSubscriberProvider::class))) + ; + + $this->listConfig = array( + 'sampleid' => array( + 'subscriber_provider' => 'sample.provider', + 'webhook_url' => 'url', + 'webhook_secret' => 'secret', + 'merge_fields' => array( + array( + 'tag' => 'FNAME', + 'name' => 'First Name', + 'type'=> 'text', + 'public' => true + ), + array( + 'tag' => 'LNAME', + 'name' => 'Last Name', + 'type'=> 'text', + 'public' => true + ) + ) + ), + 'sampleid2' => array( + 'subscriber_provider' => 'sample.provider', + 'webhook_url' => 'url', + 'webhook_secret' => 'secret', + 'merge_fields' => array( + array( + 'tag' => 'FNAME2', + 'name' => 'First Name', + 'type'=> 'text', + 'public' => true + ), + array( + 'tag' => 'LNAME2', + 'name' => 'Last Name', + 'type'=> 'text', + 'public' => true + ) + ) + ) + ); + } + + public function testGetListsEmpty() + { + //test with empty lists + $configListProvider = new ConfigListProvider($this->providerFactory, array()); + $this->expectException(\RuntimeException::class); + $configListProvider->getLists(); + } + + public function testGetLists() + { + + $configListProvider = new ConfigListProvider($this->providerFactory, $this->listConfig); + $lists = $configListProvider->getLists(); + + $this->assertEquals(2, count($lists)); + $this->assertTrue($lists[0] instanceof SubscriberList); + $this->assertEquals("FNAME", $lists[0]->getMergeFields()[0]['tag']); + } + + public function testGetList() + { + + $configListProvider = new ConfigListProvider($this->providerFactory, $this->listConfig); + $list = $configListProvider->getList("sampleid2"); + + $this->assertTrue($list instanceof SubscriberList); + $this->assertEquals("sampleid2", $list->getListId()); + $this->assertEquals("FNAME2", $list->getMergeFields()[0]['tag']); + $this->assertEquals("LNAME2", $list->getMergeFields()[01]['tag']); + $this->assertEquals("url", $list->getWebhookUrl()); + $this->assertEquals("secret", $list->getWebhookSecret()); + } + + +} From 0098144e29c4cd041a901c3b749f6e9a2fa574bd Mon Sep 17 00:00:00 2001 From: Jeroen de Kok Date: Tue, 14 Nov 2017 21:28:02 +0100 Subject: [PATCH 6/8] - Added SubscriberListInterface so one could use their own list entity when making use of a custom list provider --- src/Provider/ListProviderInterface.php | 6 +-- src/Subscriber/ListSynchronizer.php | 4 +- src/Subscriber/SubscriberList.php | 2 +- src/Subscriber/SubscriberListInterface.php | 51 ++++++++++++++++++++++ 4 files changed, 57 insertions(+), 6 deletions(-) create mode 100644 src/Subscriber/SubscriberListInterface.php diff --git a/src/Provider/ListProviderInterface.php b/src/Provider/ListProviderInterface.php index b650954..c80825d 100644 --- a/src/Provider/ListProviderInterface.php +++ b/src/Provider/ListProviderInterface.php @@ -3,19 +3,19 @@ namespace Welp\MailchimpBundle\Provider; /** - * Subscriber provider interface + * List provider interface */ interface ListProviderInterface { /** * Get all the available Mailchimp lists - * @return array of SubscriberList + * @return array of SubscriberListInterface */ public function getLists(); /** * Get one Mailchimp list by id - * @return SubscriberList + * @return SubscriberListInterface */ public function getList($listId); } diff --git a/src/Subscriber/ListSynchronizer.php b/src/Subscriber/ListSynchronizer.php index 560b9e3..55c6a23 100644 --- a/src/Subscriber/ListSynchronizer.php +++ b/src/Subscriber/ListSynchronizer.php @@ -22,10 +22,10 @@ public function __construct(ListRepository $listRepository) /** * Synchronise user from provider with MailChimp List - * @param SubscriberList $list + * @param SubscriberListInterface $list * @return void */ - public function synchronize(SubscriberList $list) + public function synchronize(SubscriberListInterface $list) { $listData = $this->listRepository->findById($list->getListId()); diff --git a/src/Subscriber/SubscriberList.php b/src/Subscriber/SubscriberList.php index dcaf3d8..7663977 100644 --- a/src/Subscriber/SubscriberList.php +++ b/src/Subscriber/SubscriberList.php @@ -7,7 +7,7 @@ /** * SubscriberList linked a MailChimpList with a SubscriberProvider */ -class SubscriberList +class SubscriberList implements SubscriberListInterface { /** * MailChimp ListId diff --git a/src/Subscriber/SubscriberListInterface.php b/src/Subscriber/SubscriberListInterface.php new file mode 100644 index 0000000..001a60b --- /dev/null +++ b/src/Subscriber/SubscriberListInterface.php @@ -0,0 +1,51 @@ + Date: Tue, 14 Nov 2017 21:34:35 +0100 Subject: [PATCH 7/8] Added documentation for the list provider feature. --- mkdocs.yml | 1 + src/Resources/doc/configuration.md | 1 + src/Resources/doc/index.md | 2 +- src/Resources/doc/list-provider.md | 100 +++++++++++++++++++++++++++++ 4 files changed, 103 insertions(+), 1 deletion(-) create mode 100644 src/Resources/doc/list-provider.md diff --git a/mkdocs.yml b/mkdocs.yml index 041c4b4..9362132 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -10,6 +10,7 @@ pages: - Setup: setup.md - Configuration: configuration.md - Subscriber Provider: subscriber-provider.md + - List Provider: list-provider.md - Usage: usage.md - Webhook: webhook.md - Tests: tests.md diff --git a/src/Resources/doc/configuration.md b/src/Resources/doc/configuration.md index 1c32145..43e5168 100644 --- a/src/Resources/doc/configuration.md +++ b/src/Resources/doc/configuration.md @@ -9,6 +9,7 @@ For each list you want to sync you must define a configuration in your `config.y ```yaml welp_mailchimp: api_key: YOURMAILCHIMPAPIKEY + list_provider: 'welp_mailchimp.list_provider' lists: listId1: # provider used in full synchronization diff --git a/src/Resources/doc/index.md b/src/Resources/doc/index.md index f60dc96..b338965 100644 --- a/src/Resources/doc/index.md +++ b/src/Resources/doc/index.md @@ -5,7 +5,7 @@ [![Packagist](https://img.shields.io/packagist/dt/welp/mailchimp-bundle.svg)](https://packagist.org/packages/welp/mailchimp-bundle) [![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg)](https://raw.githubusercontent.com/welpdev/mailchimp-bundle/master/LICENSE.md) -This bundle will help you synchronise your project's newsletter subscribers into MailChimp throught MailChimp API V3. +This bundle will help you synchronise your project's newsletter subscribers into MailChimp through MailChimp API V3. ## Features diff --git a/src/Resources/doc/list-provider.md b/src/Resources/doc/list-provider.md new file mode 100644 index 0000000..827d726 --- /dev/null +++ b/src/Resources/doc/list-provider.md @@ -0,0 +1,100 @@ +# List Provider + +By default the list configuration is read from the `config.yml` when correctly defined according to [configuring your lists](configuration.md). This is done by the default list provider (`ConfigListProvider`). If you want to use a diffrent source for your list config you can create your own List Provider that should implement `Welp\MailChimpBundle\Provider\ListProviderInterface`. + +```php +em = $entityManager; + $this->listEntity = $listEntity; + $this->userProvider = $userProvider; + } + + /** + * {@inheritdoc} + */ + public function getList($listId) + { + $list = $this->em->getRepository($this->listEntity)->findOneByListId($listId); + $list->setProvider($this->userProvider); + return $list; + } + + /** + * {@inheritdoc} + */ + public function getAll() + { + $lists = $this->em->getRepository($this->listEntity)->findAll(); + foreach($lists as $list) + { + //add the provider to the list + $list->setProvider($this->userProvider); + } + return $lists; + } +} + +``` + +Define your List provider as a service: + +```yaml +doctrine.list.provider: + public: true + arguments: + - '@doctrine.orm.entity_manager' + - 'Welp\MailchimpBundle\Subscriber\SubscriberList' + - '@example_user_provider' +``` + +After this, don't forget to add the service key for your provider into your `config.yml`: + +```yaml + welp_mailchimp: + api_key: ... + list_provider: 'doctrine.list.provider' + ... +``` From 55f2a0204f630cff81077dc441b6b34cf85a45a6 Mon Sep 17 00:00:00 2001 From: Jeroen de Kok Date: Tue, 14 Nov 2017 22:05:20 +0100 Subject: [PATCH 8/8] Updated sample code in the docs. --- src/Resources/doc/list-provider.md | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/src/Resources/doc/list-provider.md b/src/Resources/doc/list-provider.md index 827d726..ef2055c 100644 --- a/src/Resources/doc/list-provider.md +++ b/src/Resources/doc/list-provider.md @@ -36,20 +36,20 @@ namespace YourApp\App\Provider; use Doctrine\ORM\EntityManagerInterface; use Welp\MailchimpBundle\Provider\ListProviderInterface; -use Welp\MailchimpBundle\Subscriber\SubscriberListInterface; +use Welp\MailchimpBundle\Provider\ProviderInterface; class DoctrineListProvider implements ListProviderInterface { private $em; private $listEntity; - private $userProvider; + private $subscriberProvider; - public function __construct(EntityManagerInterface $entityManager, SubscriberListInterface $listEntity, $userProvider) + public function __construct(EntityManagerInterface $entityManager, $listEntity, ProviderInterface $subscriberProvider) { $this->em = $entityManager; $this->listEntity = $listEntity; - $this->userProvider = $userProvider; + $this->subscriberProvider = $subscriberProvider; } /** @@ -58,35 +58,39 @@ class DoctrineListProvider implements ListProviderInterface public function getList($listId) { $list = $this->em->getRepository($this->listEntity)->findOneByListId($listId); - $list->setProvider($this->userProvider); + $list->setProvider($this->subscriberProvider); return $list; } /** * {@inheritdoc} */ - public function getAll() + public function getLists() { $lists = $this->em->getRepository($this->listEntity)->findAll(); foreach($lists as $list) { //add the provider to the list - $list->setProvider($this->userProvider); + $list->setProvider($this->subscriberProvider); } return $lists; } } ``` +*Got your SubscriberProvider service key saved as a string with your list config ?
+Make use of the `ProviderFactory` to get the the service:
+`$provider = $this->providerFactory->create($providerServiceKey);`* Define your List provider as a service: ```yaml doctrine.list.provider: + class: YourApp\App\Provider\DoctrineListProvider public: true arguments: - '@doctrine.orm.entity_manager' - - 'Welp\MailchimpBundle\Subscriber\SubscriberList' + - 'YourApp\App\Entity\SubscriberList' - '@example_user_provider' ```