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:39
created

ValidationCommand::getLocalJobs()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.7462

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 15
ccs 3
cts 7
cp 0.4286
rs 9.4285
cc 2
eloc 6
nc 2
nop 0
crap 2.7462
1
<?php
2
/**
3
 * @package: chapi
4
 *
5
 * @author:  msiebeneicher
6
 * @since:   2015-09-20
7
 *
8
 * @link:    http://
9
 */
10
11
namespace Chapi\Commands;
12
13
use Chapi\Commands\AbstractCommand;
14
use Chapi\Component\Command\JobUtils;
15
use Chapi\Entity\Chronos\ChronosJobEntity;
16
use Chapi\Service\JobRepository\JobRepositoryInterface;
17
use Chapi\Service\JobValidator\JobValidatorServiceInterface;
18
19
class ValidationCommand extends AbstractCommand
20
{
21
22
    /**
23
     * @var array[]
24
     */
25
    private $aInvalidJobs = [];
26
27
    /**
28
     * Configures the current command.
29
     */
30 3
    protected function configure()
31
    {
32 3
        $this->setName('validate')
33 3
            ->setDescription('Validate local jobs')
34
        ;
35
36 3
        JobUtils::configureJobNamesArgument($this, 'Jobs to validate');
37 3
    }
38
39
    /**
40
     * @inheritDoc
41
     */
42 3
    protected function process()
43
    {
44 3
        $_aJobNames = JobUtils::getJobNames($this->oInput, $this);
45 3
        $_aJobsToValidate = (JobUtils::isWildcard($_aJobNames))
46 3
            ? $this->getLocalJobs()
47
            : $_aJobNames
48 1
        ;
49
50 1
        if ($this->hasInvalidJobs($_aJobsToValidate))
51
        {
52
            $this->oOutput->writeln("<comment>Found invalid jobs:</comment>\n");
53
54
            foreach ($this->getInvalidJobsByJobNames($_aJobsToValidate) as $_sJobName => $_aInvalidProperties)
55
            {
56
                $this->printInvalidJobProperties($_sJobName, $_aInvalidProperties);
57
            }
58
59
            return 1;
60
        }
61
62
        //else
63
        $this->oOutput->writeln('<info>All checked jobs look valid</info>');
64
        return 0;
65
    }
66
67
    /**
68
     * @param string[] $aJobs
69
     * @return bool
70
     */
71 1
    private function hasInvalidJobs(array $aJobs)
72
    {
73 1
        $_aInvalidJobs = $this->getInvalidJobsByJobNames($aJobs);
74
        return (count($_aInvalidJobs) > 0);
75
    }
76
77
    /**
78
     * @param array $aJobs
79
     * @return array
80
     */
81 1
    private function getInvalidJobsByJobNames(array $aJobs)
82
    {
83 1
        $_sKey = md5(implode('.', $aJobs));
84
85 1
        if (isset($this->aInvalidJobs[$_sKey]))
86 1
        {
87
            return $this->aInvalidJobs[$_sKey];
88
        }
89
90 1
        $_aInvalidJobs = [];
91
92
        /** @var JobValidatorServiceInterface $_oJobEntityValidationService */
93 1
        $_oJobEntityValidationService = $this->getContainer()->get(JobValidatorServiceInterface::DIC_NAME);
94
95
        /** @var JobRepositoryInterface  $_oJobRepositoryLocale */
96 1
        $_oJobRepositoryLocale = $this->getContainer()->get(JobRepositoryInterface::DIC_NAME_CHRONOS);
97
98
        foreach ($aJobs as $_sJobName)
99
        {
100
            $_oJobEntity = $_oJobRepositoryLocale->getJob($_sJobName);
101
102
            if (false === $_oJobEntityValidationService->isEntityValid($_oJobEntity))
103
            {
104
                $_aInvalidJobs[$_sJobName] = $_oJobEntityValidationService->getInvalidProperties($_oJobEntity);
105
            }
106
        }
107
108
        return $this->aInvalidJobs[$_sKey] = $_aInvalidJobs;
109
    }
110
111
    /**
112
     * @param string $sJobName
113
     * @param string[] $aInvalidProperties
114
     */
115
    private function printInvalidJobProperties($sJobName, array $aInvalidProperties)
116
    {
117
        $_sFormatJobName = "\t<fg=red>%s:</>";
118
        $_sFormatErrMsg = "\t\t<fg=red>%s</>";
119
120
        $this->oOutput->writeln(sprintf($_sFormatJobName, $sJobName));
121
        foreach ($aInvalidProperties as $_sErrorMessage)
122
        {
123
            $this->oOutput->writeln(sprintf($_sFormatErrMsg, $_sErrorMessage));
124
        }
125
    }
126
127
    /**
128
     * @return string[]
129
     */
130 2
    private function getLocalJobs()
131
    {
132 2
        $_aJobNames = [];
133
134
        /** @var JobRepositoryInterface  $_oJobRepositoryLocale */
135 2
        $_oJobRepositoryLocale = $this->getContainer()->get(JobRepositoryInterface::DIC_NAME_CHRONOS);
136
137
        /** @var ChronosJobEntity $_oJobEntity */
138
        foreach ($_oJobRepositoryLocale->getJobs() as $_oJobEntity)
139
        {
140
            $_aJobNames[] = $_oJobEntity->name;
141
        }
142
143
        return $_aJobNames;
144
    }
145
}