/** * Handle the event. * * @param User $user * * @return void */ public function handle(User $user) { if (DatabaseSeeder::isSeeding() || app()->environment('testing')) { return; } $list = $this->mailchimp->mailingList(env('MAILCHIMP_LIST_ID')); // We can't update an email address via the API, so we have to unsubscribe them if ($user->isDirty('email')) { $list->unsubscribe($user->getOriginal('email')); } // need to make sure their interests are up to date $interests = []; foreach (Role::whereNotNull('mailchimp_interest_id')->get() as $role) { $interests[$role->mailchimp_interest_id] = $user->isA($role->name); } // will add or update depending on whether the email addresses $list->updateSubscriber($user->email, $user->first_name, $user->last_name, $interests); // also do this for each group // group members don't get classified by interest foreach ($user->groups()->active()->get() as $group) { if ($group->settings->shouldUpdateSubscribers()) { /** @var Easychimp $mailchimp */ $mailchimp = app(Easychimp::class, [$group->settings->mailchimpKey()]); $list = $mailchimp->mailingList($group->settings->mailchimpListId()); if ($user->isDirty('email')) { $list->unsubscribe($user->getOriginal('email')); } $list->updateSubscriber($user->email, $user->first_name, $user->last_name); } } $this->delete(); }
/** * Get the validation rules that apply to the request. * * @return array */ public function rules() { // validate that the provided API key is accurate Validator::extend('valid_mailchimp_key', function ($attr, $val) { // maintain running tests offline if (app()->environment('testing')) { return true; } $easychimp = new Easychimp(new Mailchimp($val)); try { $easychimp->validateKey(); // we'll reuse this when validating the list id MailchimpIntegrationRequest::$easychimp = $easychimp; } catch (InvalidApiKey $e) { return false; } return true; }); // validate that the provided list id is accurate Validator::extend('valid_mailchimp_list_id', function ($attr, $val) { // maintain running tests offline if (app()->environment('testing')) { return true; } return MailchimpIntegrationRequest::$easychimp->mailingList($val)->exists(); }); return ['mailchimp-key' => 'required_if:mailchimp-enabled,1|valid_mailchimp_key', 'mailchimp-list-id' => 'required_with:mailchimp-enabled|valid_mailchimp_list_id']; }
/** * Handle the event. * * @param User $user * * @return void */ public function handle(Group $group, User $guardian) { if (DatabaseSeeder::isSeeding() || app()->environment('testing')) { return; } if ($group->settings->shouldUpdateSubscribers()) { $mailchimp = new Easychimp(new Mailchimp($group->settings->mailchimpKey())); $list = $mailchimp->mailingList($group->settings->mailchimpListId()); if ($list->isOnList($guardian->email) === false) { $list->subscribe($guardian->email, $guardian->first_name, $guardian->last_name); } } $this->delete(); }
/** * Handle the event. * * @param User $user * @param Role $role * * @return void */ public function handle(User $user, Role $role) { if (DatabaseSeeder::isSeeding() || app()->environment('testing')) { return; } $list = $this->mailchimp->mailingList(env('MAILCHIMP_LIST_ID')); // It's possible this recipient isn't actually subscribed // yet so we'll go ahead and subscribe them. if ($list->isOnList($user->email)) { $subscriberInfo = $list->subscriberInfo($user->email); $interests = $subscriberInfo->get('interests'); $interests->{$role->mailchimp_interest_id} = false; $list->updateSubscriber($user->email, $user->first_name, $user->last_name, $interests); } else { $list->subscribe($user->email, $user->first_name, $user->last_name, [$role->mailchimp_interest_id => false]); } $this->delete(); }
/** * Handle the event. * * @param User $user * * @return void */ public function handle(User $user) { if (DatabaseSeeder::isSeeding() || app()->environment('testing')) { return; } $list = $this->mailchimp->mailingList(env('MAILCHIMP_LIST_ID')); // Allow If we're already subscribed, allow the task // to continue without error so it's removed from the // queue. It's possible a job that adds a user to a // role has beat us to the punch if ($list->isOnList($user->email) === false) { $list->subscribe($user->email, $user->first_name, $user->last_name); } else { // if the recipient was on the mailing list before this software was put // in place, we might need to update their name $list->updateSubscriber($user->email, $user->first_name, $user->last_name); } $this->delete(); }