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 (#44)
by Marc
06:44
created

ListCommand::getOutputLabel()   B

Complexity

Conditions 6
Paths 24

Size

Total Lines 27
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 20
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 27
ccs 20
cts 20
cp 1
rs 8.439
cc 6
eloc 14
nc 24
nop 1
crap 6
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\JobEntity;
13
use Chapi\Service\JobRepository\JobRepositoryInterface;
14
use Symfony\Component\Console\Helper\Table;
15
use Symfony\Component\Console\Input\InputOption;
16
17
class ListCommand extends AbstractCommand
18
{
19
    const DEFAULT_VALUE_JOB_NAME = 'all';
20
21
    /**
22
     * Configures the current command.
23
     */
24 3
    protected function configure()
25
    {
26 3
        $this->setName('list')
27 3
            ->setDescription('Display your jobs and filter they by status')
28 3
            ->addOption('onlyFailed', 'f', InputOption::VALUE_NONE, 'Display only failed jobs')
29 3
            ->addOption('onlyDisabled', 'd', InputOption::VALUE_NONE, 'Display only disabled jobs')
30
        ;
31 3
    }
32
33
    /**
34
     * @return int
35
     * @throws \LogicException
36
     */
37 3
    protected function process()
38
    {
39
        /** @var JobRepositoryInterface  $_oJobRepositoryChronos */
40 3
        $_oJobRepositoryChronos = $this->getContainer()->get(JobRepositoryInterface::DIC_NAME_CHRONOS);
41
42 3
        $_bOnlyFailed = (bool) $this->oInput->getOption('onlyFailed');
43 3
        $_bOnlyDisabled = (bool) $this->oInput->getOption('onlyDisabled');
44
45 3
        $_oTable = new Table($this->oOutput);
46 3
        $_oTable->setHeaders(array(
47 3
            'Job',
48
            'Info'
49 3
        ));
50
51
        /** @var JobEntity $_oJobEntity */
52 3
        foreach ($_oJobRepositoryChronos->getJobs() as $_oJobEntity)
53
        {
54 3
            if ($this->hasJobToPrint($_oJobEntity, $_bOnlyFailed, $_bOnlyDisabled))
55 3
            {
56 3
                $this->printJobTableRow($_oTable, $_oJobEntity);
57 3
            }
58 3
        }
59
60 3
        $_oTable->render();
61
62 3
        return 0;
63
    }
64
65
    /**
66
     * @param JobEntity $oJobEntity
67
     * @param bool $bOnlyFailed
68
     * @param bool $bOnlyDisabled
69
     * @return bool
70
     */
71 3
    private function hasJobToPrint(JobEntity $oJobEntity, $bOnlyFailed, $bOnlyDisabled)
72
    {
73 3
        $_bPrintAllJobs = (false == $bOnlyFailed && false == $bOnlyDisabled);
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
74
        if ($_bPrintAllJobs)
75 3
        {
76 1
            return true;
77
        }
78
79 2
        $_bHasToPrint = false;
80
81 2
        if (true == $bOnlyFailed && $oJobEntity->errorsSinceLastSuccess > 0)
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
82 2
        {
83 1
            $_bHasToPrint = true;
84 1
        }
85
86 2
        if (true == $bOnlyDisabled && true == $oJobEntity->disabled)
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
87 2
        {
88 1
            $_bHasToPrint = true;
89 1
        }
90
91 2
        return $_bHasToPrint;
92
    }
93
94
    /**
95
     * @param Table $oTable
96
     * @param JobEntity $oJobEntity
97
     */
98 3
    private function printJobTableRow(Table $oTable, JobEntity $oJobEntity)
99
    {
100 3
        $oTable->addRow([
101 3
            sprintf(
102 3
                $this->getOutputFormat($oJobEntity),
103 3
                $oJobEntity->name
104 3
            ),
105
106 3
            sprintf(
107 3
                $this->getOutputFormat($oJobEntity),
108 3
                $this->getOutputLabel($oJobEntity)
109 3
            ),
110 3
        ]);
111 3
    }
112
113
    /**
114
     * @param JobEntity $oJobEntity
115
     * @return string
116
     */
117 3
    private function getOutputLabel(JobEntity $oJobEntity)
118
    {
119 3
        $_aJobInfoText = [];
120
121 3
        if ($oJobEntity->disabled)
122 3
        {
123 1
            $_aJobInfoText[] = 'disabled';
124 1
        }
125
126 3
        if ($oJobEntity->errorCount > 0)
127 3
        {
128 1
            $_fErrorRate = ($oJobEntity->successCount > 0)
129 1
                ? 100 / $oJobEntity->successCount * $oJobEntity->errorCount
130 1
                : 100;
131
132 1
            $_aJobInfoText[] = 'errors rate: ' . round($_fErrorRate, 2) . '%';
133 1
        }
134
135 3
        if ($oJobEntity->errorsSinceLastSuccess > 0)
136 3
        {
137 1
            $_aJobInfoText[] = 'errors since last success:' . $oJobEntity->errorsSinceLastSuccess;
138 1
        }
139
140 3
        return (!empty($_aJobInfoText))
141 3
            ? implode(' | ', $_aJobInfoText)
142 3
            : 'ok';
143
    }
144
145
    /**
146
     * @param JobEntity $oJobEntity
147
     * @return string
148
     */
149 3
    private function getOutputFormat(JobEntity $oJobEntity)
150
    {
151 3
        if ($oJobEntity->errorsSinceLastSuccess > 0)
152 3
        {
153 1
            return "<fg=red>%s</>";
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal <fg=red>%s</> does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
154
        }
155
156 2
        if ($oJobEntity->errorCount > 0 || true == $oJobEntity->disabled)
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
157 2
        {
158 1
            return "<comment>%s</comment>";
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal <comment>%s</comment> does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
159
        }
160
161
        // else
162 1
        return "<info>%s</info>";
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal <info>%s</info> does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
163
    }
164
}