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
02:54
created

ListCommand::getOutputLabel()   C

Complexity

Conditions 8
Paths 26

Size

Total Lines 37
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 24
CRAP Score 8.004

Importance

Changes 2
Bugs 0 Features 1
Metric Value
dl 0
loc 37
ccs 24
cts 25
cp 0.96
rs 5.3846
c 2
b 0
f 1
cc 8
eloc 18
nc 26
nop 1
crap 8.004
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
            throw new \RuntimeException('Entity not of type ChronosJobEntity');
90
        }
91
92 3
        $_bPrintAllJobs = (false === $bOnlyFailed && false === $bOnlyDisabled);
93
        if ($_bPrintAllJobs)
94 3
        {
95 1
            return true;
96
        }
97
98 2
        $_bHasToPrint = false;
99
100 2
        if (true === $bOnlyFailed && $oJobEntity->errorsSinceLastSuccess > 0)
101 2
        {
102 1
            $_bHasToPrint = true;
103 1
        }
104
105 2
        if (true === $bOnlyDisabled && true === $oJobEntity->disabled)
106 2
        {
107 1
            $_bHasToPrint = true;
108 1
        }
109
110 2
        return $_bHasToPrint;
111
    }
112
113
    /**
114
     * @param Table $oTable
115
     * @param JobEntityInterface $oJobEntity
116
     */
117 3
    private function printJobTableRow(Table $oTable, JobEntityInterface $oJobEntity)
118
    {
119 3
        $oTable->addRow([
120 3
            sprintf(
121 3
                $this->getOutputFormat($oJobEntity),
122 3
                $oJobEntity->getKey()
123 3
            ),
124
125 3
            sprintf(
126 3
                $this->getOutputFormat($oJobEntity),
127 3
                $this->getOutputLabel($oJobEntity)
128 3
            ),
129 3
            sprintf(
130 3
                $this->getOutputFormat($oJobEntity),
131 3
                $oJobEntity->getEntityType()
132 3
            )
133 3
        ]);
134 3
    }
135
136
    /**
137
     * @param JobEntityInterface $oJobEntity
138
     * @return string
139
     */
140 3
    private function getOutputLabel(JobEntityInterface $oJobEntity)
141
    {
142
143 3
        if ($oJobEntity->getEntityType() == JobEntityInterface::MARATHON_TYPE)
144 3
        {
145 3
            return 'ok';
146
        }
147
148 3
        if (!$oJobEntity instanceof ChronosJobEntity) {
149
            throw new \RuntimeException('Entity not of type ChronosJobEntity');
150
        }
151
152 3
        $_aJobInfoText = [];
153
154 3
        if ($oJobEntity->disabled)
155 3
        {
156 1
            $_aJobInfoText[] = 'disabled';
157 1
        }
158
159 3
        if ($oJobEntity->errorCount > 0)
160 3
        {
161 1
            $_fErrorRate = ($oJobEntity->successCount > 0)
162 1
                ? 100 / $oJobEntity->successCount * $oJobEntity->errorCount
163 1
                : 100;
164
165 1
            $_aJobInfoText[] = 'errors rate: ' . round($_fErrorRate, 2) . '%';
166 1
        }
167
168 3
        if ($oJobEntity->errorsSinceLastSuccess > 0)
169 3
        {
170 1
            $_aJobInfoText[] = 'errors since last success:' . $oJobEntity->errorsSinceLastSuccess;
171 1
        }
172
173 3
        return (!empty($_aJobInfoText))
174 3
            ? implode(' | ', $_aJobInfoText)
175 3
            : 'ok';
176
    }
177
178
    /**
179
     * @param JobEntityInterface $oJobEntity
180
     * @return string
181
     */
182 3
    private function getOutputFormat(JobEntityInterface $oJobEntity)
183
    {
184 3
        if ($oJobEntity->getEntityType() == JobEntityInterface::MARATHON_TYPE)
185 3
        {
186 3
            return '<info>%s</info>';
187
        }
188
189 3
        if (!$oJobEntity instanceof ChronosJobEntity) {
190
            throw new \RuntimeException('Entity not of type ChronosJobEntity');
191
        }
192
193 3
        if ($oJobEntity->errorsSinceLastSuccess > 0)
194 3
        {
195 1
            return '<fg=red>%s</>';
196
        }
197
198 2
        if ($oJobEntity->errorCount > 0 || true === $oJobEntity->disabled)
199 2
        {
200 1
            return '<comment>%s</comment>';
201
        }
202
203
        // else
204 1
        return '<info>%s</info>';
205
    }
206
}