Completed
Push — master ( 202f3c...15bdf1 )
by Tom
09:14 queued 04:46
created

AbstractCronCommand::getJobConfigElements()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 14
rs 9.4285
cc 3
eloc 7
nc 3
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
use Traversable;
12
13
abstract class AbstractCronCommand extends AbstractMagentoCommand
14
{
15
    /**
16
     * @return array
17
     */
18
    protected function getJobs()
19
    {
20
        $table = array();
21
22
        $jobs = $this->getJobConfigElements();
23
24
        foreach ($jobs as $name => $job) {
25
            $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...
26
        }
27
28
        ksort($table, SORT_STRING);
29
30
        return $table;
31
    }
32
33
    /**
34
     * @param  Mage_Core_Model_Config_Element $job
35
     * @return array of five cron values,keyed by 'm', 'h', 'D', 'M' and 'WD'
36
     */
37
    protected function getSchedule(Mage_Core_Model_Config_Element $job)
38
    {
39
        $keys = array('m', 'h', 'D', 'M', 'WD');
40
        $expr = null;
41
42
        if (isset($job->schedule->config_path)) {
43
            $expr = Mage::getStoreConfig((string)$job->schedule->config_path);
44
        } elseif (isset($job->schedule->cron_expr)) {
45
            $expr = $job->schedule->cron_expr;
46
        }
47
48
        if ($cronExpressions = $this->parseCronExpression($expr)) {
49
            return array_combine($keys, $cronExpressions);
50
        }
51
52
        return array_combine($keys, array_fill(0, 5, '  '));
53
    }
54
55
    /**
56
     * Get job configuration from XML and database. Expression priority is given to the database.
57
     *
58
     * @return Traversable|Mage_Core_Model_Config_Element[]
59
     */
60
    private function getJobConfigElements()
61
    {
62
        $jobs = new AppendIterator();
63
64
        $paths = array('crontab/jobs', 'default/crontab/jobs');
65
66
        foreach ($paths as $path) {
67
            if ($jobConfig = Mage::getConfig()->getNode($path)) {
68
                $jobs->append(new \IteratorIterator($jobConfig->children()));
69
            };
70
        }
71
72
        return $jobs;
73
    }
74
75
    /**
76
     * parse a cron expression into an array, false-ly if unable to handle
77
     *
78
     * uses magento 1 internal parser of cron expressions
79
     *
80
     * @return array with five values (zero-indexed) or FALSE in case it does not exists.
81
     */
82
    private function parseCronExpression($expr)
83
    {
84
        if ($expr === 'always') {
85
            return array_fill(0, 5, '*');
86
        }
87
88
        /** @var $schedule Mage_Cron_Model_Schedule */
89
        $schedule = Mage::getModel('cron/schedule');
90
91
        try {
92
            $schedule->setCronExpr($expr);
93
        } 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...
94
            return false;
95
        }
96
97
        $array = $schedule->getData('cron_expr_arr');
98
99
        $array = array_slice($array, 0, 5); // year is optional and never parsed
100
101
        // parse each entry
102
        foreach ($array as $expression) {
103
            try {
104
                $schedule->matchCronExpression($expression, 1);
105
            } 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...
106
                return false;
107
            }
108
        }
109
110
        return $array;
111
    }
112
}
113