/** * 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) { $subscriptions = Component::whereIn('id', $subscriptions); } else { $subscriptions = Component::all(); } foreach ($subscriptions as $component) { Subscription::create(['subscriber_id' => $subscriber->id, 'component_id' => $component->id]); } if ($command->verified) { dispatch(new VerifySubscriberCommand($subscriber)); } else { event(new SubscriberHasSubscribedEvent($subscriber)); } return $subscriber; }