AbstractIndexerCommand   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 5
c 0
b 0
f 0
lcom 1
cbo 2
dl 0
loc 57
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getIndexerList() 0 19 2
A getRuntime() 0 11 2
A getRuntimeInSeconds() 0 7 1
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->getObjectManager()->get('Magento\Indexer\Model\Indexer\Collection');
17
        foreach ($indexCollection as $indexer) {
18
            /* @var $indexer \Magento\Indexer\Model\Indexer */
19
            $lastReadbleRuntime = $this->getRuntime($indexer);
20
            $runtimeInSeconds = $this->getRuntimeInSeconds($indexer);
21
            $list[] = array(
22
                'code'            => $indexer->getId(),
23
                'title'           => $indexer->getTitle(),
24
                'status'          => $indexer->getStatus(),
25
                'last_runtime'    => $lastReadbleRuntime, // @TODO Check if this exists in Magento 2
26
                'runtime_seconds' => $runtimeInSeconds, // @TODO Check if this exists in Magento 2
27
            );
28
        }
29
30
        return $list;
31
    }
32
33
    /**
34
     * Returns a readable runtime
35
     *
36
     * @param $indexer
37
     * @return mixed
38
     */
39
    protected function getRuntime($indexer)
40
    {
41
        $dateTimeUtils = new DateTimeUtils();
42
        $startTime = new \DateTime($indexer->getStartedAt());
43
        $endTime = new \DateTime($indexer->getEndedAt());
44
        if ($startTime > $endTime) {
45
            return 'index not finished';
46
        }
47
        $lastRuntime = $dateTimeUtils->getDifferenceAsString($startTime, $endTime);
48
        return $lastRuntime;
49
    }
50
51
    /**
52
     * Returns the runtime in total seconds
53
     *
54
     * @param $indexer
55
     * @return int
56
     */
57
    protected function getRuntimeInSeconds($indexer)
58
    {
59
        $startTimestamp = strtotime($indexer->getStartedAt());
60
        $endTimestamp = strtotime($indexer->getEndedAt());
61
62
        return $endTimestamp - $startTimestamp;
63
    }
64
}
65