Handle the update incident command.

@param \CachetHQ\Cachet\Bus\Commands\Incident\UpdateIncidentCommand $command

@return \CachetHQ\Cachet\Models\Incident
775666
public function handle(UpdateIncidentCommand $command)
    {
        if ($template = IncidentTemplate::where('slug', '=', $command->template)->first()) {
            $command->message = $this->parseTemplate($template, $command);
        }

        $incident = $command->incident;
        $incident->fill($this->filter($command));

        // The incident occurred at a different time.
        if ($occurredAt = $command->occurred_at) {
            if ($date = $this->dates->create('Y-m-d H:i', $occurredAt)) {
                $incident->fill(['occurred_at' => $date]);
            } else {
                throw new InvalidIncidentTimestampException("Unable to pass timestamp {$occurredAt}");
            }
        }

        // Rather than making lots of updates, just fill and save.
        $incident->save();

        // Store any meta?
        if ($meta = $command->meta) {
            $this->storeMeta($command->meta, 'incidents', $incident->id);
        }

        // Update the component.
        if ($component = Component::find($command->component_id)) {
            execute(new UpdateComponentCommand(
                Component::find($command->component_id),
                null,
                null,
                $command->component_status,
                null,
                null,
                null,
                null,
                null,
                false
            ));
        }

        event(new IncidentWasUpdatedEvent($this->auth->user(), $incident));

        return $incident;
    }
530396
public function fire(Command $command)
    {
        if ($command->option('verbose')) {
            $command->call('key:generate');
            $command->call('api:key-generate');

            return;
        }
        $command->callSilent('key:generate');
        $command->callSilent('api:key-generate');
    }
700619
public static function onCommand($command, ConstraintViolationListInterface $violations)
    {
        $exception = new static(
            'Validation failed for ' . get_class($command) .
            ' with ' . $violations->count() . ' violation(s).'
        );

        $exception->command    = $command;
        $exception->violations = $violations;

        return $exception;
    }
263931
public function handle(CommandInterface $command)
    {
        $object = $command->getParameter('data');

        if ($command->hasParameter('class')) {
            $class = $command->getParameter('class');
            if (!($object instanceof $class)) {
                return CommandResult::error('invalid_instance');
            }
        }

        try {
            $this->objectManager->flush();
        } catch (ForeignKeyConstraintViolationException $e) {
            return CommandResult::error('constraint_violation');
        }
    }
805130
public function handle(CommandInterface $command)
    {
        $object = $command->getParameter('data');

        if ($command->hasParameter('class')) {
            $class = $command->getParameter('class');
            if (!($object instanceof $class)) {
                return CommandResult::error('invalid_instance');
            }
        }

        $this->objectManager->persist($object);
        $this->objectManager->flush();
    }
91747
public function fire(Command $command)
    {
        if ($command->option('verbose')) {
            $command->blockMessage('Migrations', 'Starting the module migrations ...', 'comment');
        }

        foreach ($this->modules as $module) {
            if ($command->option('verbose')) {
                $command->call('module:migrate', ['module' => $module]);
                continue;
            }
            $command->callSilent('module:migrate', ['module' => $module]);
        }
    }
1208855
public function handle(UploadImage $command)
    {
        // check if the user can upload images, otherwise return
        $this->assertCan(
            $command->actor,
            'flagrow.image.upload'
        );

        $tmpFile = tempnam($this->app->storagePath() . '/tmp', 'image');
        $command->file->moveTo($tmpFile);

        $file = new UploadedFile(
            $tmpFile,
            $command->file->getClientFilename(),
            $command->file->getClientMediaType(),
            $command->file->getSize(),
            $command->file->getError(),
            true
        );

        // validate the file
        $this->validator->maxFileSize = $this->settings->get('flagrow.image-upload.maxFileSize', 2048);
        $this->validator->assertValid(['image' => $file]);

        // resize if enabled
        if ($this->settings->get('flagrow.image-upload.mustResize')) {
            $manager = new ImageManager;
            $manager->make($tmpFile)->fit(
                $this->settings->get('flagrow.image-upload.resizeMaxWidth', 100),
                $this->settings->get('flagrow.image-upload.resizeMaxHeight', 100)
            )->save();
        }

        $image = (new Image())->forceFill([
            'user_id'       => $command->actor->id,
            'upload_method' => $this->settings->get('flagrow.image-upload.uploadMethod', 'local'),
            'created_at'    => Carbon::now(),
            'file_name'     => sprintf(
                '%d-%s.%s',
                $command->actor->id,
                Str::quickRandom(),
                $file->guessExtension() ?: 'jpg'
            ),
            'file_size'     => $file->getSize()
        ]);

        // fire the Event ImageWillBeSaved, which can be extended and/or modified elsewhere
        $this->events->fire(
            new ImageWillBeSaved($command->actor, $image, $file)
        );

        $tmpFilesystem = new Filesystem(new Local(pathinfo($tmpFile, PATHINFO_DIRNAME)));

        $meta = $this->upload->uploadContents(
            $image->file_name,
            $tmpFilesystem->readAndDelete(pathinfo($tmpFile, PATHINFO_BASENAME))
        );

        if ($meta) {
            $image->file_url = array_get($meta, 'url');

            if ($image->isDirty()) {
                $image->save();
            }

            return $image;
        }

        return false;
    }
1236718
protected function handle(CommandInterface $command)
	{
		if($command instanceof SelfHandlingCommandInterface)
		{
			return $command->handle();
		}

		$handler = $this->resolveCommandHandler($command);

		return $this->executeCommandHandler($handler, $command);
	}
1348634
public function handle(UnsubscribeSubscriptionCommand $command)
    {
        $subscription = $command->subscription;

        event(new SubscriberHasUnsubscribedEvent($subscription->subscriber));

        $subscription->delete();
    }
1262293
public function execute($command)
    {
        $validator = $this->translator->toValidator($command);

        if (class_exists($validator)) {
            $this->app->make($validator)->validate($command);
        }

        return $this->bus->execute($command);
    }
1156799
public function handle($command)
    {
        $user = new User();
        $user
            ->setEmail($command->getEmail())
            ->setPassword($command->getPassword())
            ->setRoles($this->registeredUserRoles)
            ->setEnabled(true);

        // validate user
        $errors = $this->validator->validate($user);
        if (count($errors) > 0) {
            $exception = new InvalidCommandException("User is invalid");
            $exception->setConstraintViolationList($errors);
            throw $exception;
        }

        // persist user
        $this->entityManager->persist($user);
        $this->entityManager->flush();

        return $user;
    }
39388
protected function triggerCommand($command, $arguments = null)
    {
        return $this->getCommandBus()->execute($command, $arguments ?: $this->arguments, $this->update);
    }
537119
public function handle(UpdateScheduleCommand $command)
    {
        $schedule = $command->schedule;

        $schedule->update($this->filter($command));

        event(new ScheduleWasUpdatedEvent($this->auth->user(), $schedule));

        return $schedule;
    }
1210664
protected function parseTemplate(IncidentTemplate $template, UpdateIncidentCommand $command)
    {
        $env = new Twig_Environment(new Twig_Loader_Array([]));
        $template = $env->createTemplate($template->template);

        $vars = array_merge($command->template_vars, [
            'incident' => [
                'name'             => $command->name,
                'status'           => $command->status,
                'message'          => $command->message,
                'visible'          => $command->visible,
                'notify'           => $command->notify,
                'stickied'         => $command->stickied,
                'occurred_at'      => $command->occurred_at,
                'component'        => Component::find($command->component_id) ?: null,
                'component_status' => $command->component_status,
            ],
        ]);

        return $template->render($vars);
    }
352305
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;
    }
1265314
public function handle(IncidentUpdateWasReportedEvent $event)
    {
        $update = $event->update;
        $incident = $update->incident;

        // Only send emails for public incidents while the system is not under scheduled maintenance.
        if (!$incident->visible || !$this->system->canNotifySubscribers()) {
            return;
        }

        // First notify all global subscribers.
        $globalSubscribers = $this->subscriber->isVerified()->isGlobal()->get();

        $globalSubscribers->each(function ($subscriber) use ($update) {
            $subscriber->notify(new IncidentUpdatedNotification($update));
        });

        if (!$incident->component) {
            return;
        }

        $notified = $globalSubscribers->pluck('id')->all();

        // Notify the remaining component specific subscribers.
        $componentSubscribers = $this->subscriber
            ->isVerified()
            ->forComponent($incident->component->id)
            ->get()
            ->reject(function ($subscriber) use ($notified) {
                return in_array($subscriber->id, $notified);
            })->each(function ($subscriber) use ($update) {
                $subscriber->notify(new IncidentUpdatedNotification($update));
            });
    }
1097278
private function handleDelete(DeleteCommand $command)
    {
        $this->state = ORMInterface::STATE_DELETED;
        $this->dispatch('deleted', new RecordEvent($this, $command));
    }