Passed
Push — master ( 3252a3...420639 )
by Andreas
23:53
created

cron::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 2
dl 0
loc 5
ccs 4
cts 4
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * @package midcom.console
4
 * @author CONTENT CONTROL http://www.contentcontrol-berlin.de/
5
 * @copyright CONTENT CONTROL http://www.contentcontrol-berlin.de/
6
 * @license http://www.gnu.org/licenses/gpl.html GNU General Public License
7
 */
8
9
namespace midcom\console\command;
10
11
use Symfony\Component\Console\Command\Command;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Console\Command\Command was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
12
use Symfony\Component\Console\Input\InputInterface;
13
use Symfony\Component\Console\Output\OutputInterface;
14
use midcom_services_auth;
15
use midcom_helper__componentloader;
16
use midcom_error;
17
use midcom_services_cron;
18
use Symfony\Component\Console\Input\InputArgument;
19
use Symfony\Component\Console\Input\InputOption;
20
21
/**
22
 * Cron command
23
 *
24
 * When executed, it checks all component manifests for cron jobs and runs them sequentially.
25
 * The components are processed in the order they are returned by the component loader, the
26
 * jobs are run in the order they are listed in the manifest.
27
 *
28
 * <b>Launching MidCOM Cron from a System Cron</b>
29
 *
30
 * Add something like this to your crontab:
31
 *
32
 * <pre>
33
 * * * * * *  www-data {PROJECT PATH}vendor/bin/midcom --servername=example.com midcom:cron minute
34
 * 1 * * * *  www-data {PROJECT PATH}vendor/bin/midcom --servername=example.com midcom:cron hour
35
 * 1 1 * * *  www-data {PROJECT PATH}vendor/bin/midcom --servername=example.com midcom:cron day
36
 * </pre>
37
 *
38
 * Make sure to pass the correct hostname, this is used e.g. when generating links in mails sent by cron jobs.
39
 * In case you're running more than one site on the same server, this is also needed to get the same cache
40
 * prefix as the website the job belongs to.
41
 *
42
 * Also take care to run the cron command with the same user the website is running, otherwise RCS entries or
43
 * attachments generated by cron jobs could end up unreadable.
44
 *
45
 * @see midcom_services_cron
46
 * @package midcom.console
47
 */
48
class cron extends Command
49
{
50
    private midcom_services_auth $auth;
51
52
    private midcom_helper__componentloader $loader;
53
54 1
    public function __construct(midcom_services_auth $auth, midcom_helper__componentloader $loader)
55
    {
56 1
        $this->auth = $auth;
57 1
        $this->loader = $loader;
58 1
        parent::__construct();
59
    }
60
61 1
    protected function configure()
62
    {
63 1
        $this->setName('midcom:cron')
64 1
            ->setAliases(['cron'])
65 1
            ->setDescription('Checks all component manifests for cron jobs and runs them sequentially')
66 1
            ->addArgument('type', InputArgument::OPTIONAL, 'Recurrence (minute, hour, or day)', 'minute')
67 1
            ->addOption('job', 'j', InputOption::VALUE_REQUIRED, 'Run only this job');
68
    }
69
70 1
    protected function execute(InputInterface $input, OutputInterface $output) : int
71
    {
72 1
        if (!$this->auth->request_sudo('midcom.services.cron')) {
73
            throw new midcom_error('Failed to get sudo');
74
        }
75
76
        // compat for old-style calls (type=minute)
77 1
        $type = str_replace('type=', '', $input->getArgument('type'));
78
79 1
        $recurrence = 'MIDCOM_CRON_' . strtoupper($type);
80 1
        if (!defined($recurrence)) {
81
            throw new midcom_error('Unsupported type ' . $type);
82
        }
83
84 1
        if ($job = $input->getOption('job')) {
85
            $this->run_job($job, $output);
86
        } else {
87
            // Instantiate cron service and run
88 1
            $cron = new midcom_services_cron(constant($recurrence));
89 1
            $data = $this->loader->get_all_manifest_customdata('midcom.services.cron');
90
91 1
            foreach ($cron->load_jobs($data) as $job) {
92 1
                $this->run_job($job['handler'], $output);
93
            }
94
        }
95 1
        $this->auth->drop_sudo();
96 1
        return 0;
97
    }
98
99 1
    private function run_job(string $classname, OutputInterface $output)
100
    {
101 1
        $handler = new $classname;
102 1
        if ($handler->initialize($output)) {
103 1
            $output->writeln("Executing job <info>{$classname}</info>", OutputInterface::VERBOSITY_VERBOSE);
104 1
            $handler->execute();
105
        } else {
106
            $output->writeln("Skipping job <info>{$classname}</info>", OutputInterface::VERBOSITY_VERBOSE);
107
        }
108
    }
109
}
110