Skip to content

Commit

Permalink
- Added mergeFields as attribute in SubscriberList.
Browse files Browse the repository at this point in the history
- Modified  Sync Merge Fields command to use listProvider.
  • Loading branch information
Jeroen de Kok committed Nov 15, 2017
1 parent efcd8da commit 06fb7e0
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 9 deletions.
22 changes: 16 additions & 6 deletions src/Command/SynchronizeMergeFieldsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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('<info>%s</info>', $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());
;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Provider/ConfigListProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
22 changes: 20 additions & 2 deletions src/Subscriber/SubscriberList.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,28 @@ 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;

//If the provider implements DynamicProviderInterface, set the list id in the provider
if ($this->provider instanceof DynamicProviderInterface) {
$this->provider->setListId($this->listId);
}
}
}

/**
Expand All @@ -54,4 +63,13 @@ public function getProvider()
{
return $this->provider;
}

/**
* Get the list merge fields
* @return array
*/
public function getMergeFields()
{
return $this->mergeFields;
}
}

0 comments on commit 06fb7e0

Please sign in to comment.