Completed
Push — develop ( ba0504...5f081f )
by Tom
03:40
created

RunCommand::askJobCode()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 22
Code Lines 13

Duplication

Lines 3
Ratio 13.64 %

Importance

Changes 0
Metric Value
dl 3
loc 22
rs 9.2
c 0
b 0
f 0
cc 3
eloc 13
nc 2
nop 3
1
<?php
2
3
namespace N98\Magento\Command\System\Cron;
4
5
use Magento\Cron\Model\Schedule;
6
use Symfony\Component\Console\Input\InputArgument;
7
use Symfony\Component\Console\Input\InputInterface;
8
use Symfony\Component\Console\Output\OutputInterface;
9
10
class RunCommand extends AbstractCronCommand
11
{
12
    protected function configure()
13
    {
14
        $this
15
            ->setName('sys:cron:run')
16
            ->addArgument('job', InputArgument::OPTIONAL, 'Job code')
17
            ->setDescription('Runs a cronjob by job code');
18
        $help = <<<HELP
19
If no `job` argument is passed you can select a job from a list.
20
See it in action: http://www.youtube.com/watch?v=QkzkLgrfNaM
21
HELP;
22
        $this->setHelp($help);
23
    }
24
25
    /**
26
     * @param InputInterface $input
27
     * @param OutputInterface $output
28
     * @throws \Exception
29
     * @return int|void
30
     */
31
    protected function execute(InputInterface $input, OutputInterface $output)
32
    {
33
        $jobCode = $input->getArgument('job');
34
        $jobs = $this->getJobs();
35
36
        if (!$jobCode) {
37
            $this->writeSection($output, 'Cronjob');
38
            $jobCode = $this->askJobCode($input, $output, $jobs);
39
        }
40
41
        $jobConfig = $this->getJobConfig($jobCode);
42
43
        if (empty($jobCode) || !isset($jobConfig['instance'])) {
44
            throw new \InvalidArgumentException('No job config found!');
45
        }
46
47
        $model = $this->getObjectManager()->get($jobConfig['instance']);
48
49 View Code Duplication
        if (!$model || !is_callable(array($model, $jobConfig['method']))) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
50
            throw new \RuntimeException(
51
                sprintf(
52
                    'Invalid callback: %s::%s does not exist',
53
                    $jobConfig['instance'],
54
                    $jobConfig['method']
55
                )
56
            );
57
        }
58
59
        $callback = array($model, $jobConfig['method']);
60
61
        $output->write(
62
            '<info>Run </info><comment>' . $jobConfig['instance'] . '::' . $jobConfig['method'] . '</comment> '
63
        );
64
65
        try {
66
            $schedule = $this->cronScheduleCollection->getNewEmptyItem();
67
            $schedule
68
                ->setJobCode($jobCode)
69
                ->setStatus(Schedule::STATUS_RUNNING)
70
                ->setExecutedAt(strftime('%Y-%m-%d %H:%M:%S', $this->timezone->scopeTimeStamp()))
71
                ->save();
72
73
            $this->state->emulateAreaCode('crontab', $callback, array($schedule));
74
75
            $schedule
76
                ->setStatus(Schedule::STATUS_SUCCESS)
77
                ->setFinishedAt(strftime('%Y-%m-%d %H:%M:%S', $this->timezone->scopeTimeStamp()))
78
                ->save();
79
        } catch (Exception $e) {
0 ignored issues
show
Bug introduced by
The class N98\Magento\Command\System\Cron\Exception does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
80
            $schedule
81
                ->setStatus(Schedule::STATUS_ERROR)
82
                ->setMessages($e->getMessage())
83
                ->setFinishedAt(strftime('%Y-%m-%d %H:%M:%S', $this->timezone->scopeTimeStamp()))
84
                ->save();
85
        }
86
87
        $output->writeln('<info>done</info>');
88
    }
89
}
90