GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Pull Request — master (#68)
by Bidesh
03:32
created

ListCommand::process()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 35
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 22
CRAP Score 3

Importance

Changes 2
Bugs 0 Features 1
Metric Value
dl 0
loc 35
ccs 22
cts 22
cp 1
rs 8.8571
c 2
b 0
f 1
cc 3
eloc 18
nc 3
nop 0
crap 3
1
<?php
2
/**
3
 * @package: chapi
4
 *
5
 * @author:  msiebeneicher
6
 * @since:   2015-07-28
7
 *
8
 */
9
10
namespace Chapi\Commands;
11
12
use Chapi\Entity\Chronos\ChronosJobEntity;
13
use Chapi\Entity\JobEntityInterface;
14
use Chapi\Entity\Marathon\MarathonAppEntity;
15
use Chapi\Service\JobRepository\JobRepositoryInterface;
16
use Symfony\Component\Console\Helper\Table;
17
use Symfony\Component\Console\Input\InputOption;
18
19
class ListCommand extends AbstractCommand
20
{
21
    const DEFAULT_VALUE_JOB_NAME = 'all';
22
23
    /**
24
     * Configures the current command.
25
     */
26 3
    protected function configure()
27
    {
28 3
        $this->setName('list')
29 3
            ->setDescription('Display your jobs and filter them by status')
30 3
            ->addOption('onlyFailed', 'f', InputOption::VALUE_NONE, 'Display only failed jobs')
31 3
            ->addOption('onlyDisabled', 'd', InputOption::VALUE_NONE, 'Display only disabled jobs')
32
        ;
33 3
    }
34
35
    /**
36
     * @return int
37
     * @throws \LogicException
38
     */
39 3
    protected function process()
40
    {
41
        /** @var JobRepositoryInterface  $_oJobRepositoryChronos */
42 3
        $_oJobRepositoryChronos = $this->getContainer()->get(JobRepositoryInterface::DIC_NAME_CHRONOS);
43
        /** @var  JobRepositoryInterface $_oJobRepositoryMarathon */
44 3
        $_oJobRepositoryMarathon = $this->getContainer()->get(JobRepositoryInterface::DIC_NAME_MARATHON);
45
46 3
        $_bOnlyFailed = (bool) $this->oInput->getOption('onlyFailed');
47 3
        $_bOnlyDisabled = (bool) $this->oInput->getOption('onlyDisabled');
48
49 3
        $_oTable = new Table($this->oOutput);
50 3
        $_oTable->setHeaders(array(
51 3
            'Job',
52 3
            'Info',
53
            'Type'
54 3
        ));
55
56 3
        $_aAllEntities = array_merge(
57 3
            $_oJobRepositoryChronos->getJobs()->getArrayCopy(),
58 3
            $_oJobRepositoryMarathon->getJobs()->getArrayCopy()
59 3
        );
60
61
        /** @var ChronosJobEntity $_oJobEntity */
62 3
        foreach ($_aAllEntities as $_oJobEntity)
63
        {
64 3
            if ($this->hasJobToPrint($_oJobEntity, $_bOnlyFailed, $_bOnlyDisabled))
65 3
            {
66 3
                $this->printJobTableRow($_oTable, $_oJobEntity);
67 3
            }
68 3
        }
69
70 3
        $_oTable->render();
71
72 3
        return 0;
73
    }
74
75
    /**
76
     * @param JobEntityInterface $oJobEntity
77
     * @param bool $bOnlyFailed
78
     * @param bool $bOnlyDisabled
79
     * @return bool
80
     */
81 3
    private function hasJobToPrint(JobEntityInterface $oJobEntity, $bOnlyFailed, $bOnlyDisabled)
82
    {
83 3
        if ($oJobEntity->getEntityType() == JobEntityInterface::MARATHON_TYPE)
84 3
        {
85 3
            return true;
86
        }
87
88 3
        if (!$oJobEntity instanceof ChronosJobEntity)
89 3
        {
90
            throw new \RuntimeException('Entity not of type ChronosJobEntity');
91
        }
92
93 3
        $_bPrintAllJobs = (false === $bOnlyFailed && false === $bOnlyDisabled);
94
        if ($_bPrintAllJobs)
95 3
        {
96 1
            return true;
97
        }
98
99 2
        $_bHasToPrint = false;
100
101 2
        if (true === $bOnlyFailed && $oJobEntity->errorsSinceLastSuccess > 0)
102 2
        {
103 1
            $_bHasToPrint = true;
104 1
        }
105
106 2
        if (true === $bOnlyDisabled && true === $oJobEntity->disabled)
107 2
        {
108 1
            $_bHasToPrint = true;
109 1
        }
110
111 2
        return $_bHasToPrint;
112
    }
113
114
    /**
115
     * @param Table $oTable
116
     * @param JobEntityInterface $oJobEntity
117
     */
118 3
    private function printJobTableRow(Table $oTable, JobEntityInterface $oJobEntity)
119
    {
120 3
        $oTable->addRow([
121 3
            sprintf(
122 3
                $this->getOutputFormat($oJobEntity),
123 3
                $oJobEntity->getKey()
124 3
            ),
125
126 3
            sprintf(
127 3
                $this->getOutputFormat($oJobEntity),
128 3
                $this->getOutputLabel($oJobEntity)
129 3
            ),
130 3
            sprintf(
131 3
                $this->getOutputFormat($oJobEntity),
132 3
                $oJobEntity->getEntityType()
133 3
            )
134 3
        ]);
135 3
    }
136
137
    /**
138
     * @param JobEntityInterface $oJobEntity
139
     * @return string
140
     */
141 3
    private function getOutputLabel(JobEntityInterface $oJobEntity)
142
    {
143
144 3
        if ($oJobEntity->getEntityType() == JobEntityInterface::MARATHON_TYPE)
145 3
        {
146 3
            return 'ok';
147
        }
148
149 3
        if (!$oJobEntity instanceof ChronosJobEntity)
150 3
        {
151
            throw new \RuntimeException('Entity not of type ChronosJobEntity');
152
        }
153
154 3
        $_aJobInfoText = [];
155
156 3
        if ($oJobEntity->disabled)
157 3
        {
158 1
            $_aJobInfoText[] = 'disabled';
159 1
        }
160
161 3
        if ($oJobEntity->errorCount > 0)
162 3
        {
163 1
            $_fErrorRate = ($oJobEntity->successCount > 0)
164 1
                ? 100 / $oJobEntity->successCount * $oJobEntity->errorCount
165 1
                : 100;
166
167 1
            $_aJobInfoText[] = 'errors rate: ' . round($_fErrorRate, 2) . '%';
168 1
        }
169
170 3
        if ($oJobEntity->errorsSinceLastSuccess > 0)
171 3
        {
172 1
            $_aJobInfoText[] = 'errors since last success:' . $oJobEntity->errorsSinceLastSuccess;
173 1
        }
174
175 3
        return (!empty($_aJobInfoText))
176 3
            ? implode(' | ', $_aJobInfoText)
177 3
            : 'ok';
178
    }
179
180
    /**
181
     * @param JobEntityInterface $oJobEntity
182
     * @return string
183
     */
184 3
    private function getOutputFormat(JobEntityInterface $oJobEntity)
185
    {
186 3
        if ($oJobEntity->getEntityType() == JobEntityInterface::MARATHON_TYPE)
187 3
        {
188 3
            return '<info>%s</info>';
189
        }
190
191 3
        if (!$oJobEntity instanceof ChronosJobEntity)
192 3
        {
193
            throw new \RuntimeException('Entity not of type ChronosJobEntity');
194
        }
195
196 3
        if ($oJobEntity->errorsSinceLastSuccess > 0)
197 3
        {
198 1
            return '<fg=red>%s</>';
199
        }
200
201 2
        if ($oJobEntity->errorCount > 0 || true === $oJobEntity->disabled)
202 2
        {
203 1
            return '<comment>%s</comment>';
204
        }
205
206
        // else
207 1
        return '<info>%s</info>';
208
    }
209
}