* @author Joseph Cohen * @author Graham Campbell */ class SubscribeSubscriberCommandHandler { /** * Handle the subscribe subscriber command. * * @param \CachetHQ\Cachet\Bus\Commands\Subscriber\SubscribeSubscriberCommand $command * * @return \CachetHQ\Cachet\Models\Subscriber */ public function handle(SubscribeSubscriberCommand $command) { if ($subscriber = Subscriber::where('email', '=', $command->email)->first()) { return $subscriber; } $subscriber = Subscriber::firstOrCreate(['email' => $command->email]); // Decide what to subscribe the subscriber to. if ($subscriptions = $command->subscriptions) { $components = Component::whereIn('id', $subscriptions)->get(); } else { $components = Component::all(); } $components->each(function ($component) use ($subscriber) { Subscription::create([ 'subscriber_id' => $subscriber->id, 'component_id' => $component->id, ]); }); if ($command->verified) { execute(new VerifySubscriberCommand($subscriber)); } else { $subscriber->notify(new VerifySubscriptionNotification()); } event(new SubscriberHasSubscribedEvent($subscriber)); $subscriber->load('subscriptions'); return $subscriber; } }