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
Push — master ( 525114...c68331 )
by Patrick
11s
created

StatusCommand   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 167
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 5

Test Coverage

Coverage 0%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 20
c 3
b 0
f 0
lcom 2
cbo 5
dl 0
loc 167
ccs 0
cts 66
cp 0
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A initialize() 0 6 1
A configure() 0 12 1
B process() 0 25 3
A containsChangedAppJobs() 0 11 4
B getChangedAppJobs() 0 24 1
A printStatusView() 0 15 3
A filterJobListWithIndex() 0 14 4
A printJobList() 0 14 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\BusinessCase\Comparison\JobComparisonInterface;
13
use Chapi\Service\JobIndex\JobIndexServiceInterface;
14
use Symfony\Component\Console\Input\InputInterface;
15
use Symfony\Component\Console\Input\InputOption;
16
use Symfony\Component\Console\Output\OutputInterface;
17
18
class StatusCommand extends AbstractCommand
19
{
20
    const LABEL_CHRONOS  = 'chronos';
21
    const LABEL_MARATHON = 'marathon';
22
23
    /** @var JobIndexServiceInterface  */
24
    private $jobIndexService;
25
26
    /**
27
     * Configures the current command.
28
     */
29
    protected function configure()
30
    {
31
        $this->setName('status')
32
            ->setDescription('Show the working tree status')
33
            ->addOption(
34
                'strict',
35
                null,
36
                InputOption::VALUE_NONE,
37
                "Return a non-zero exit code when there are changes",
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal Return a non-zero exit code when there are changes 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...
38
                null
39
            );
40
    }
41
42
    /**
43
     * @inheritdoc
44
     */
45
    protected function initialize(InputInterface $input, OutputInterface $output)
46
    {
47
        parent::initialize($input, $output);
48
49
        $this->jobIndexService = $this->getContainer()->get(JobIndexServiceInterface::DIC_NAME);
50
    }
51
52
    /**
53
     * @return int
54
     */
55
    protected function process()
56
    {
57
        $changedJobs = $this->getChangedAppJobs();
58
59
        // tracked jobs
60
        $this->output->writeln("\nChanges to be committed");
61
        $this->output->writeln("  (use 'chapi reset <job>...' to unstage)");
62
        $this->output->writeln('');
63
64
        $this->printStatusView($changedJobs, true);
65
66
        // untracked jobs
67
        $this->output->writeln("\nChanges not staged for commit");
68
        $this->output->writeln("  (use 'chapi add <job>...' to update what will be committed)");
69
        $this->output->writeln("  (use 'chapi checkout <job>...' to discard changes in local repository)");
70
        $this->output->writeln('');
71
72
        $this->printStatusView($changedJobs, false);
73
74
        if ($this->input->getOption('strict') && $this->containsChangedAppJobs($changedJobs)) {
75
            return 1;
76
        }
77
78
        return 0;
79
    }
80
81
    /**
82
     * @param array<string,array<string,array>> $jobs
83
     * @return bool
84
     */
85
    private function containsChangedAppJobs(array $jobs) {
86
        foreach (['new', 'missing', 'updates'] as $category) {
87
            foreach ([self::LABEL_CHRONOS, self::LABEL_MARATHON] as $framework) {
88
                if (!empty($jobs[$category][$framework])) {
89
                    return true;
90
                }
91
            }
92
        }
93
94
        return false;
95
    }
96
97
    /**
98
     * @return array<string,array<string,array>>
99
     */
100
    private function getChangedAppJobs()
101
    {
102
        /** @var JobComparisonInterface $jobComparisonBusinessCaseChronos */
103
        /** @var JobComparisonInterface $jobComparisonBusinessCaseMarathon */
104
        $jobComparisonBusinessCaseChronos  = $this->getContainer()->get(JobComparisonInterface::DIC_NAME_CHRONOS);
105
        $jobComparisonBusinessCaseMarathon = $this->getContainer()->get(JobComparisonInterface::DIC_NAME_MARATHON);
106
107
        $result = [
108
            'new' => [
109
                self::LABEL_CHRONOS => $jobComparisonBusinessCaseChronos->getRemoteMissingJobs(),
110
                self::LABEL_MARATHON => $jobComparisonBusinessCaseMarathon->getRemoteMissingJobs(),
111
            ],
112
            'missing' => [
113
                self::LABEL_CHRONOS => $jobComparisonBusinessCaseChronos->getLocalMissingJobs(),
114
                self::LABEL_MARATHON => $jobComparisonBusinessCaseMarathon->getLocalMissingJobs(),
115
            ],
116
            'updates' => [
117
                self::LABEL_CHRONOS => $jobComparisonBusinessCaseChronos->getLocalJobUpdates(),
118
                self::LABEL_MARATHON => $jobComparisonBusinessCaseMarathon->getLocalJobUpdates(),
119
            ],
120
        ];
121
122
        return $result;
123
    }
124
125
    /**
126
     * @param array $changedJobs
127
     * @param bool $filterIsInIndex
128
     */
129
    private function printStatusView($changedJobs, $filterIsInIndex)
130
    {
131
        $formatMap = [
132
            'new' => ['title' => 'New jobs in local repository', 'format' => "\t<comment>new %s job:\t%s</comment>"],
133
            'missing' => ['title' => 'Missing jobs in local repository', 'format' => "\t<fg=red>delete %s job:\t%s</>"],
134
            'updates' => ['title' => 'Updated jobs in local repository', 'format' => "\t<info>modified %s job:\t%s</info>"]
135
        ];
136
137
        foreach ($changedJobs as $jobStatus => $jobList) {
138
            $filteredJobList = $this->filterJobListWithIndex($jobList, $filterIsInIndex);
139
            if (!empty($filteredJobList)) {
140
                $this->printJobList($formatMap[$jobStatus]['title'], $filteredJobList, $formatMap[$jobStatus]['format']);
141
            }
142
        }
143
    }
144
145
    /**
146
     * @param array $jobLists
147
     * @param bool $filterIsInIndex
148
     * @return array
149
     */
150
    private function filterJobListWithIndex($jobLists, $filterIsInIndex)
151
    {
152
        $filteredJobList = [];
153
154
        foreach ($jobLists as $appLabel => $jobList) {
155
            foreach ($jobList as $jobName) {
156
                if ($filterIsInIndex == $this->jobIndexService->isJobInIndex($jobName)) {
157
                    $filteredJobList[$appLabel][] = $jobName;
158
                }
159
            }
160
        }
161
162
        return $filteredJobList;
163
    }
164
165
    /**
166
     * @param string $title
167
     * @param array $jobLists
168
     * @param string $listFormat
169
     */
170
    private function printJobList($title, $jobLists, $listFormat)
171
    {
172
        $this->output->writeln(sprintf('  %s:', $title));
173
174
        foreach ($jobLists as $label => $jobList) {
175
            foreach ($jobList as $jobName) {
176
                $this->output->writeln(
177
                    sprintf($listFormat, $label, $jobName)
178
                );
179
            }
180
        }
181
182
        $this->output->writeln("\n");
183
    }
184
}
185