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 (#94)
by Patrick
02:44
created

DiffCommand::printJobDiff()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 15
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 15
rs 9.4285
cc 3
eloc 7
nc 4
nop 1
1
<?php
2
/**
3
 * @package: chapi
4
 *
5
 * @author:  msiebeneicher
6
 * @since:   2015-07-30
7
 *
8
 */
9
10
namespace Chapi\Commands;
11
12
use Chapi\BusinessCase\Comparison\JobComparisonInterface;
13
use Chapi\Service\JobRepository\JobRepository;
14
use Symfony\Component\Console\Input\InputArgument;
15
16
class DiffCommand extends AbstractCommand
17
{
18
    /**
19
     * Configures the current command.
20
     */
21
    protected function configure()
22
    {
23
        $this->setName('diff')
24
            ->setDescription('Show changes between jobs and working tree, etc')
25
            ->addArgument('jobName', InputArgument::OPTIONAL, 'Show changes for specific job')
26
        ;
27
    }
28
29
    /**
30
     * @return int
31
     */
32
    protected function process()
33
    {
34
        /** @var JobComparisonInterface  $jobComparisonBusinessCase */
35
        $jobComparisonBusinessCase = $this->getContainer()->get(JobComparisonInterface::DIC_NAME);
36
        $jobName = $this->input->getArgument('jobName');
37
38
        if (!empty($jobName)) {
39
            $this->printJobDiff($jobName);
40
        } else {
41
            $localJobUpdates = $jobComparisonBusinessCase->getLocalJobUpdates();
42
            if (!empty($localJobUpdates)) {
43
                foreach ($localJobUpdates as $jobName) {
44
                    $this->printJobDiff($jobName);
45
                }
46
            }
47
        }
48
49
        return 0;
50
    }
51
52
    /**
53
     * @param string $jobName
54
     */
55
    private function printJobDiff($jobName)
56
    {
57
        /** @var JobComparisonInterface  $jobComparisonBusinessCase */
58
        $jobComparisonBusinessCase = $this->getContainer()->get(JobComparisonInterface::DIC_NAME);
59
60
        $jobs = [ $jobName ];
61
62
        if (strpos($jobName, '*') !== false) {
63
            $jobs = $this->getJobsMatchingWildcard($jobName);
64
        }
65
66
        foreach ($jobs as $jobName) {
67
            $this->printSingleJobDiff($jobComparisonBusinessCase, $jobName);
68
        }
69
    }
70
71
    /**
72
     * @param JobComparisonInterface $jobComparisonBusinessCase
73
     * @param string $jobName
74
     */
75
    private function printSingleJobDiff(JobComparisonInterface $jobComparisonBusinessCase, $jobName)
76
    {
77
        $this->output->writeln(sprintf("\n<comment>diff %s</comment>", $jobName));
78
79
        $jobDiff = $jobComparisonBusinessCase->getJobDiff($jobName);
80
81
        foreach ($jobDiff as $property => $diff) {
82
            $diffLines = array_reverse(explode(PHP_EOL, $diff));
83
84
            foreach ($diffLines as $diffLine) {
85
                $diffSign = substr($diffLine, 0, 1);
86
87
                if ($diffSign == '+') {
88
                    $this->output->writeln(sprintf("<info>%s\t%s: %s</info>", $diffSign, $property, substr($diffLine, 1)));
89
                } elseif ($diffSign == '-') {
90
                    $this->output->writeln(sprintf("<fg=red>%s\t%s: %s</>", $diffSign, $property, substr($diffLine, 1)));
91
                } else {
92
                    $this->output->writeln(sprintf("\t%s: %s", $property, $diffLine));
93
                }
94
            }
95
        }
96
97
        $this->output->writeln("\n");
98
    }
99
100
    /**
101
     * @param string $jobName
102
     * @return string[]
103
     */
104
    private function getJobsMatchingWildcard($jobName)
105
    {
106
        /** @var JobRepository[] $jobRepositories */
107
        $jobRepositories = [
108
            $this->getContainer()->get(JobRepository::DIC_NAME_CHRONOS),
109
            $this->getContainer()->get(JobRepository::DIC_NAME_FILESYSTEM_CHRONOS),
110
            $this->getContainer()->get(JobRepository::DIC_NAME_FILESYSTEM_MARATHON),
111
            $this->getContainer()->get(JobRepository::DIC_NAME_MARATHON)
112
        ];
113
114
        $jobNames = [];
115
116
        foreach ($jobRepositories as $jobRepository) {
117
            foreach ($jobRepository->getJobs() as $job) {
118
                if (fnmatch($jobName, $job->getKey())) {
119
                    $jobNames[] = $job->getKey();
120
                }
121
            }
122
        }
123
124
        sort($jobNames);
125
126
        return array_values(array_unique($jobNames));
127
    }
128
}
129