welpdev / mailchimp-bundle

MailChimp integration with Symfony and MailChimp API V3
https://welpdev.github.io/mailchimp-bundle/
MIT License
44 stars 36 forks source link

Dynamically set api_key param #26

Closed andyts93 closed 5 years ago

andyts93 commented 5 years ago

Is there a way to dynamically change the api_key param? I have a list of user with a mailchimp_api_key setting and I should operate on every single user's mailchimp account. Thanks

jeroendk commented 5 years ago

This is currently not possible, however, I had a similar use case where every list (Welp\MailchimpBundle\Subscriber\SubscriberList) needed his own api_key.

To make this work I created our own MailchimpListRepository, not the most elegant solution but it works:

<?php

namespace AppBundle\Repository;

use Welp\MailchimpBundle\Subscriber\Subscriber;
use Welp\MailchimpBundle\Subscriber\ListRepository;
use DrewM\MailChimp\MailChimp;
use Welp\MailchimpBundle\Provider\ListProviderInterface;

/*
    Override all the functions to support diffrent api keys per list basis
*/
class MailchimpListRepository extends ListRepository{

    private $mailchimpListProvider;
    private $currentApiKey;

    public function __construct(ListProviderInterface $mailchimpListProvider)
    {
       $this->mailchimpListProvider = $mailchimpListProvider;
    }

    /**
    * {@inheritdoc}
    */
    public function findById($listId)
    {
        $this->reInitializeMailchimp($listId);        
        return parent::findById($listId);
    }

    /**
    * {@inheritdoc}
    */
    protected function putSubscriberInList($listId, Subscriber $subscriber, $status)
    {
        $this->reInitializeMailchimp($listId);        
        return parent::putSubscriberInList($listId, $subscriber, $status);
    }    

    /**
    * {@inheritdoc}
    */
    public function update($listId, Subscriber $subscriber)
    {
        $this->reInitializeMailchimp($listId);        
        return parent::update($listId, $subscriber);
    }

    /**
    * {@inheritdoc}
    */
    public function changeEmailAddress($listId, Subscriber $newSubscriber, $oldEmailAddress)
    {
        $this->reInitializeMailchimp($listId);        
        return parent::changeEmailAddress($listId, $newSubscriber, $oldEmailAddress);
    }

    /**
    * {@inheritdoc}
    */
    public function delete($listId, Subscriber $subscriber)
    {
        $this->reInitializeMailchimp($listId);        
        return parent::delete($listId, $subscriber);
    }

    /**
    * {@inheritdoc}
    */
    public function batchSubscribe($listId, array $subscribers)
    {                
        $this->reInitializeMailchimp($listId);        
        return parent::batchSubscribe($listId, $subscribers);
    }

    /**
    * {@inheritdoc}
    */
    public function batchUnsubscribe($listId, array $emails)
    {
        $this->reInitializeMailchimp($listId);        
        return parent::batchUnsubscribe($listId, $emails);
    }

    /**
    * {@inheritdoc}
    */
    public function batchDelete($listId, array $emails)
    {
        $this->reInitializeMailchimp($listId);        
        return parent::batchDelete($listId, $emails);
    }

    /**
    * {@inheritdoc}
    */
    public function getMembers($listId)
    {
        $this->reInitializeMailchimp($listId);        
        return parent::getMembers($listId);
    }

    /**
    * {@inheritdoc}
    */
    public function getSubscriberEmails($listId)
    {
        $this->reInitializeMailchimp($listId);        
        return parent::getSubscriberEmails($listId);
    }

    /**
    * {@inheritdoc}
    */
    public function getMergeFields($listId)
    {
        $this->reInitializeMailchimp($listId);        
        return parent::getMergeFields($listId);
    }

   /**
    * {@inheritdoc}
    */
    public function addMergeField($listId, array $mergeData)
    {
        $this->reInitializeMailchimp($listId);        
        return parent::addMergeField($listId, $mergeData);
    }

    /**
    * {@inheritdoc}
    */
    public function updateMergeField($listId, $mergeId, $mergeData)
    {
        $this->reInitializeMailchimp($listId);        
        return parent::updateMergeField($listId, $mergeId, $mergeData);
    }

    /**
    * {@inheritdoc}
    */
    public function deleteMergeField($listId, $mergeId)
    {
        $this->reInitializeMailchimp($listId);        
        return parent::deleteMergeField($listId, $mergeId);
    }

    /**
    * {@inheritdoc}
    */
    public function addWebhook($listId, array $webhookData)
    {
        $this->reInitializeMailchimp($listId);        
        return parent::addWebhook($listId, $webhookData);
    }

    /**
    * {@inheritdoc}
    */
    public function getWebhooks($listId)
    {
        $this->reInitializeMailchimp($listId);        
        return parent::getWebhooks($listId);
    }

    /**
     * Because we support diffrent apiKeys per list we might need to reinitialize the Mailchimp wrapper class
     */
    public function reInitializeMailchimp($listId)
    {   

        $list = $this->mailchimpListProvider->getList($listId);
        if($list != null && $this->currentApiKey != $list->getApiKey())
        {
            $this->mailchimp = new Mailchimp($list->getApiKey());
        }       

    }
}

The custom (persisted) list entity can hold an api key which is used before every action to create a new Mailchimp instance.

Note that the "follow-sync" options does not work with multiple api keys.

There might be a better solution, but for us this worked at the time...

andyts93 commented 5 years ago

Thanks! I will try this solution, it should be fine for me

jeroendk commented 5 years ago

@Nightbr Think you can close this one!