Completed
Pull Request — develop (#788)
by Tom
04:25
created

AbstractCronCommand::getJobs()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 1 Features 0
Metric Value
c 3
b 1
f 0
dl 0
loc 14
rs 9.4285
cc 2
eloc 7
nc 2
nop 0
1
<?php
2
3
namespace N98\Magento\Command\System\Cron;
4
5
use AppendIterator;
6
use Mage;
7
use Mage_Core_Model_Config_Element;
8
use Mage_Cron_Exception;
9
use Mage_Cron_Model_Schedule;
10
use N98\Magento\Command\AbstractMagentoCommand;
11
12
abstract class AbstractCronCommand extends AbstractMagentoCommand
13
{
14
    /**
15
     * @return array
16
     */
17
    protected function getJobs()
18
    {
19
        $table = array();
20
21
        $jobs = $this->getJobConfigElements();
22
23
        foreach ($jobs as $name => $job) {
24
            $table[$name] = array('Job' => $name) + $this->getSchedule($job);
0 ignored issues
show
Documentation introduced by
$job is of type null, but the function expects a object<Mage_Core_Model_Config_Element>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
25
        }
26
27
        ksort($table, SORT_STRING);
28
29
        return $table;
30
    }
31
32
    /**
33
     * @param  Mage_Core_Model_Config_Element $job
34
     * @return array of five cron values,keyed by 'm', 'h', 'D', 'M' and 'WD'
35
     */
36
    protected function getSchedule(Mage_Core_Model_Config_Element $job)
37
    {
38
        $keys = array('m', 'h', 'D', 'M', 'WD');
39
        $expr = null;
40
41
        if (isset($job->schedule->config_path)) {
42
            $expr = Mage::getStoreConfig((string) $job->schedule->config_path);
43
        } elseif (isset($job->schedule->cron_expr)) {
44
            $expr = $job->schedule->cron_expr;
45
        }
46
47
        if ($cronExpressions = $this->parseCronExpression($expr)) {
48
            return array_combine($keys, $cronExpressions);
49
        }
50
51
        return array_combine($keys, array_fill(0, 5, '  '));
52
    }
53
54
    /**
55
     * Get job configuration from XML and database. Expression priority is given to the database.
56
     *
57
     * @return Mage_Core_Model_Config_Element[]
58
     */
59
    private function getJobConfigElements()
60
    {
61
        $jobs = new AppendIterator();
62
63
        $paths = array('crontab/jobs', 'default/crontab/jobs');
64
65
        foreach ($paths as $path) {
66
            if ($jobConfig = Mage::getConfig()->getNode($path)) {
67
                $jobs->append(new \IteratorIterator($jobConfig->children()));
68
            };
69
        }
70
71
        return $jobs;
72
    }
73
74
    /**
75
     * parse a cron expression into an array, false-ly if unable to handle
76
     *
77
     * uses magento 1 internal parser of cron expressions
78
     *
79
     * @return array with five values (zero-indexed) or FALSE in case it does not exists.
80
     */
81
    private function parseCronExpression($expr)
82
    {
83
        if ($expr === 'always') {
84
            return array_fill(0, 5, '*');
85
        }
86
87
        /** @var $schedule Mage_Cron_Model_Schedule */
88
        $schedule = Mage::getModel('cron/schedule');
89
90
        try {
91
            $schedule->setCronExpr($expr);
92
        } catch (Mage_Cron_Exception $e) {
0 ignored issues
show
Bug introduced by
The class Mage_Cron_Exception does not exist. Is this class maybe located in a folder that is not analyzed, or in a newer version of your dependencies than listed in your composer.lock/composer.json?
Loading history...
93
            return false;
94
        }
95
96
        $array = $schedule->getData('cron_expr_arr');
97
        if (!is_array($array)) {
98
            return false;
99
        }
100
101
        $array = array_slice($array, 0, 5); // year is optional and never parsed
102
103
        foreach ($array as $expression) {
104
            // parse each entry
105
            try {
106
                $schedule->matchCronExpression($expression, 1);
107
            } catch (Mage_Cron_Exception $e) {
0 ignored issues
show
Bug introduced by
The class Mage_Cron_Exception does not exist. Is this class maybe located in a folder that is not analyzed, or in a newer version of your dependencies than listed in your composer.lock/composer.json?
Loading history...
108
                return false;
109
            }
110
        }
111
112
        return $array;
113
    }
114
}
115