Completed
Push — develop ( e4a933...b1fd58 )
by Tom
03:38
created

AbstractIndexerCommand::_getIndexerModel()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
3
namespace N98\Magento\Command\Indexer;
4
5
use Exception;
6
use Mage;
7
use Mage_Index_Model_Indexer;
8
use Mage_Index_Model_Process;
9
use N98\Magento\Command\AbstractMagentoCommand;
10
use N98\Util\DateTime as DateTimeUtils;
11
use Symfony\Component\Console\Output\OutputInterface;
12
13
/**
14
 * Class AbstractIndexerCommand
15
 *
16
 * @package N98\Magento\Command\Indexer
17
 */
18
class AbstractIndexerCommand extends AbstractMagentoCommand
19
{
20
    /**
21
     * @return Mage_Index_Model_Indexer
22
     */
23
    protected function getIndexerModel()
24
    {
25
        /* @var $indexer Mage_Index_Model_Indexer */
26
        $indexer = Mage::getModel('index/indexer');
27
        if (!$indexer instanceof Mage_Index_Model_Indexer) {
0 ignored issues
show
Bug introduced by
The class Mage_Index_Model_Indexer 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...
28
            throw new \UnexpectedValueException('Failure getting indexer model');
29
        }
30
31
        return $indexer;
32
    }
33
34
    /**
35
     * @return Mage_Index_Model_Indexer
36
     * @deprecated since 1.97.28
37
     */
38
    protected function _getIndexerModel()
39
    {
40
        trigger_error(__METHOD__ . ' moved, use ->getIndexerModel() instead', E_USER_DEPRECATED);
41
        return $this->getIndexerModel();
42
    }
43
44
    /**
45
     * @return array
46
     */
47
    protected function getIndexerList()
48
    {
49
        $list = array();
50
        $indexCollection = $this->getIndexerModel()->getProcessesCollection();
51
        foreach ($indexCollection as $indexer) {
52
            $lastReadbleRuntime = $this->getRuntime($indexer);
53
            $runtimeInSeconds = $this->getRuntimeInSeconds($indexer);
54
            $list[] = array(
55
                'code'            => $indexer->getIndexerCode(),
56
                'status'          => $indexer->getStatus(),
57
                'last_runtime'    => $lastReadbleRuntime,
58
                'runtime_seconds' => $runtimeInSeconds,
59
            );
60
        }
61
62
        return $list;
63
    }
64
65
    /**
66
     * Returns a readable runtime
67
     *
68
     * @param Mage_Index_Model_Process $indexer
69
     * @return string
70
     */
71
    protected function getRuntime(Mage_Index_Model_Process $indexer)
72
    {
73
        $dateTimeUtils = new DateTimeUtils();
74
        $startTime = new \DateTime($indexer->getStartedAt());
75
        $endTime = new \DateTime($indexer->getEndedAt());
76
        if ($startTime > $endTime) {
77
            return 'index not finished';
78
        }
79
        $lastRuntime = $dateTimeUtils->getDifferenceAsString($startTime, $endTime);
80
        return $lastRuntime;
81
    }
82
83
    /**
84
     * Disable observer which try to create adminhtml session on CLI
85
     */
86
    protected function disableObservers()
87
    {
88
        $node = Mage::app()->getConfig()->getNode('adminhtml/events/core_locale_set_locale/observers/bind_locale');
89
        if ($node) {
90
            $node->appendChild(new \Varien_Simplexml_Element('<type>disabled</type>'));
91
        }
92
    }
93
94
    /**
95
     * Returns the runtime in total seconds
96
     *
97
     * @param Mage_Index_Model_Process $indexer
98
     * @return int
99
     */
100
    protected function getRuntimeInSeconds(Mage_Index_Model_Process $indexer)
101
    {
102
        $startTimestamp = strtotime($indexer->getStartedAt());
103
        $endTimestamp = strtotime($indexer->getEndedAt());
104
105
        return $endTimestamp - $startTimestamp;
106
    }
107
108
    /**
109
     * @param OutputInterface $output
110
     * @param Mage_Index_Model_Process $process
111
     */
112
    protected function writeEstimatedEnd(OutputInterface $output, Mage_Index_Model_Process $process)
113
    {
114
        $runtimeInSeconds = $this->getRuntimeInSeconds($process);
115
116
        /**
117
         * Try to estimate runtime. If index was aborted or never created we have a timestamp < 0
118
         */
119
        if ($runtimeInSeconds <= 0) {
120
            return;
121
        }
122
123
        $estimatedEnd = new \DateTime('now', new \DateTimeZone('UTC'));
124
        $estimatedEnd->add(new \DateInterval('PT' . $runtimeInSeconds . 'S'));
125
        $output->writeln(
126
            sprintf('<info>Estimated end: <comment>%s</comment></info>', $estimatedEnd->format('Y-m-d H:i:s T'))
127
        );
128
    }
129
130
    /**
131
     * @param OutputInterface $output
132
     * @param Mage_Index_Model_Process $process
133
     * @param \DateTime $startTime
134
     * @param \DateTime $endTime
135
     */
136
    protected function writeSuccessResult(
137
        OutputInterface $output, Mage_Index_Model_Process $process, \DateTime $startTime, \DateTime $endTime
138
    ) {
139
        $output->writeln(
140
            sprintf(
141
                '<info>Successfully reindexed <comment>%s</comment> (Runtime: <comment>%s</comment>)</info>',
142
                $process->getIndexerCode(),
143
                DateTimeUtils::difference($startTime, $endTime)
144
            )
145
        );
146
    }
147
148
    /**
149
     * @param OutputInterface $output
150
     * @param array $processes
151
     */
152
    protected function executeProcesses(OutputInterface $output, array $processes)
153
    {
154
        try {
155
            \Mage::dispatchEvent('shell_reindex_init_process');
156
            foreach ($processes as $process) {
157
                $this->executeProcess($output, $process);
158
            }
159
            \Mage::dispatchEvent('shell_reindex_finalize_process');
160
        } catch (Exception $e) {
161
            \Mage::dispatchEvent('shell_reindex_finalize_process');
162
        }
163
    }
164
165
    /**
166
     * @param OutputInterface $output
167
     * @param $process
168
     */
169
    private function executeProcess(OutputInterface $output, Mage_Index_Model_Process $process)
170
    {
171
        $output->writeln(
172
            sprintf('<info>Started reindex of: <comment>%s</comment></info>', $process->getIndexerCode())
173
        );
174
        $this->writeEstimatedEnd($output, $process);
175
176
        $startTime = new \DateTime('now');
177
        $process->reindexEverything();
178
        \Mage::dispatchEvent($process->getIndexerCode() . '_shell_reindex_after');
179
        $endTime = new \DateTime('now');
180
        $this->writeSuccessResult($output, $process, $startTime, $endTime);
181
    }
182
}
183