/** * Handle the subscribe subscriber command. * * @param \CachetHQ\Cachet\Bus\Commands\Subscriber\UpdateSubscriberSubscriptionCommand $command * * @return \CachetHQ\Cachet\Models\Subscriber */ public function handle(UpdateSubscriberSubscriptionCommand $command) { $subscriber = $command->subscriber; $subscriptions = $command->subscriptions ?: []; $components = Component::all(); $updateSubscriptions = $components->filter(function ($item) use($subscriptions) { return in_array($item->id, $subscriptions); }); $subscriber->global = $updateSubscriptions->count() === $components->count(); $subscriber->subscriptions()->delete(); if (!$updateSubscriptions->isEmpty()) { foreach ($updateSubscriptions as $subscription) { Subscription::firstOrCreate(['subscriber_id' => $subscriber->id, 'component_id' => $subscription->id]); } } $subscriber->save(); event(new SubscriberHasUpdatedSubscriptionsEvent($subscriber)); return $subscriber; }
/** * Handle the subscribe subscriber command. * * @param \CachetHQ\Cachet\Bus\Commands\Subscriber\SubscribeSubscriberCommand $command * * @throws \CachetHQ\Cachet\Exceptions\AlreadySubscribedException * * @return \CachetHQ\Cachet\Models\Subscriber */ public function handle(SubscribeSubscriberCommand $command) { if (Subscriber::where('email', $command->email)->first() && $command->subscriptions === null) { throw new AlreadySubscribedException("Cannot subscribe {$command->email} because they're already subscribed."); } $subscriber = Subscriber::firstOrCreate(['email' => $command->email]); if ($subscriptions = $command->subscriptions) { foreach ($subscriptions as $subscription => $subscriptionValue) { Subscription::firstOrCreate(['subscriber_id' => $subscriber->id, $subscription => $subscriptionValue]); } } if ($subscriber->is_verified === false) { if ($command->verified) { dispatch(new VerifySubscriberCommand($subscriber)); } else { event(new SubscriberHasSubscribedEvent($subscriber)); } } else { event(new SubscriberHasUpdatedSubscriptionsEvent($subscriber)); } return $subscriber; }