Completed
Pull Request — develop (#739)
by Robbie
07:34 queued 03:09
created

AbstractCronCommand::getJobs()   B

Complexity

Conditions 4
Paths 8

Size

Total Lines 26
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 26
rs 8.5806
cc 4
eloc 13
nc 8
nop 0
1
<?php
2
3
namespace N98\Magento\Command\System\Cron;
4
5
use N98\Magento\Command\AbstractMagentoCommand;
6
7
abstract class AbstractCronCommand extends AbstractMagentoCommand
8
{
9
    /**
10
     * @return array
11
     */
12
    protected  function getJobs()
13
    {
14
        $table = array();
15
16
        // Get job configuration from XML and database. Expression priority is given to the database.
17
        $config = \Mage::getConfig();
18
19
        $xmlJobConfig = $config->getNode('crontab/jobs');
20
        $dbJobConfig  = $config->getNode('default/crontab/jobs');
21
22
        $xmlJobs      = ($xmlJobConfig) ? $xmlJobConfig->children() : array();
23
        $databaseJobs = ($dbJobConfig) ? $dbJobConfig->children() : array();
24
25
        $jobs = array_merge((array)$xmlJobs, (array)$databaseJobs);
26
27
        foreach ($jobs as $job) {
28
            /* @var $job \Mage_Core_Model_Config_Element */
29
            $table[] = array('Job'  => (string) $job->getName()) + $this->getSchedule($job);
30
        }
31
32
        usort($table, function($a, $b) {
33
            return strcmp($a['Job'], $b['Job']);
34
        });
35
36
        return $table;
37
    }
38
39
    /**
40
     * @param $job
41
     * @return array
42
     */
43
    protected function getSchedule($job)
44
    {
45
        $expr = null;
46
47
        if (isset($job->schedule->config_path)) {
48
            $expr = \Mage::getStoreConfig((string) $job->schedule->config_path);
49
        } elseif (isset($job->schedule->cron_expr)) {
50
            $expr = (string) $job->schedule->cron_expr;
51
        }
52
53
        if ($expr) {
54
            if ($expr == 'always') {
55
                return array('m' => '*', 'h' => '*', 'D' => '*', 'M' => '*', 'WD' => '*');
56
            }
57
58
            $schedule = $this->_getModel('cron/schedule', 'Mage_Cron_Model_Schedule');
59
            $schedule->setCronExpr($expr);
60
            $array = $schedule->getCronExprArr();
61
            return array(
62
                'm'  => $array[0],
63
                'h'  => $array[1],
64
                'D'  => $array[2],
65
                'M'  => $array[3],
66
                'WD' => $array[4]
67
            );
68
        }
69
70
        return array('m' => '  ', 'h' => '  ', 'D' => '  ', 'M' => '  ', 'WD' => '  ');
71
    }
72
}
73