Completed
Push — master ( 561e2a...f3a4fe )
by Christian
04:31
created

AbstractIndexerCommand::getIndexerCollection()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace N98\Magento\Command\Indexer;
4
5
use N98\Magento\Command\AbstractMagentoCommand;
6
use N98\Util\DateTime as DateTimeUtils;
7
8
class AbstractIndexerCommand extends AbstractMagentoCommand
9
{
10
    /**
11
     * @return array
12
     */
13
    protected function getIndexerList()
14
    {
15
        $list = array();
16
        $indexCollection = $this->getIndexerCollection();
17
18
        foreach ($indexCollection as $indexer) {
19
            /* @var $indexer \Magento\Indexer\Model\Indexer */
20
            $lastReadbleRuntime = $this->getRuntime($indexer);
21
            $runtimeInSeconds = $this->getRuntimeInSeconds($indexer);
22
            $list[] = array(
23
                'code'            => $indexer->getId(),
24
                'title'           => $indexer->getTitle(),
25
                'status'          => $indexer->getStatus(),
26
                'last_runtime'    => $lastReadbleRuntime, // @TODO Check if this exists in Magento 2
27
                'runtime_seconds' => $runtimeInSeconds, // @TODO Check if this exists in Magento 2
28
            );
29
        }
30
31
        return $list;
32
    }
33
34
    /**
35
     * @return \Magento\Indexer\Model\Indexer\Collection
36
     */
37
    protected function getIndexerCollection()
38
    {
39
        return $this->getObjectManager()->get('Magento\Indexer\Model\Indexer\Collection');
40
    }
41
42
    /**
43
     * Returns a readable runtime
44
     *
45
     * @param $indexer
46
     * @return mixed
47
     */
48
    protected function getRuntime($indexer)
49
    {
50
        $dateTimeUtils = new DateTimeUtils();
51
        $startTime = new \DateTime($indexer->getStartedAt());
52
        $endTime = new \DateTime($indexer->getEndedAt());
53
        if ($startTime > $endTime) {
54
            return 'index not finished';
55
        }
56
        $lastRuntime = $dateTimeUtils->getDifferenceAsString($startTime, $endTime);
57
58
        return $lastRuntime;
59
    }
60
61
    /**
62
     * Returns the runtime in total seconds
63
     *
64
     * @param $indexer
65
     * @return int
66
     */
67
    protected function getRuntimeInSeconds($indexer)
68
    {
69
        $startTimestamp = strtotime($indexer->getStartedAt());
70
        $endTimestamp = strtotime($indexer->getEndedAt());
71
72
        return $endTimestamp - $startTimestamp;
73
    }
74
}
75