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

ChronosStoreJobBusinessCase::storeIndexedJobs()   B

Complexity

Conditions 4
Paths 8

Size

Total Lines 23
Code Lines 10

Duplication

Lines 23
Ratio 100 %

Code Coverage

Tests 14
CRAP Score 4

Importance

Changes 0
Metric Value
dl 23
loc 23
ccs 14
cts 14
cp 1
rs 8.7972
c 0
b 0
f 0
cc 4
eloc 10
nc 8
nop 0
crap 4
1
<?php
2
/**
3
 * @package: Chapi
4
 *
5
 * @author:  msiebeneicher
6
 * @since:   2015-07-31
7
 *
8
 */
9
10
namespace Chapi\BusinessCase\JobManagement;
11
12
use Chapi\BusinessCase\Comparison\JobComparisonInterface;
13
use Chapi\Entity\Chronos\ChronosJobEntity;
14
use Chapi\Service\JobDependencies\JobDependencyServiceInterface;
15
use Chapi\Service\JobIndex\JobIndexServiceInterface;
16
use Chapi\Service\JobRepository\JobRepositoryInterface;
17
use Psr\Log\LoggerInterface;
18
19
class ChronosStoreJobBusinessCase extends AbstractStoreJobBusinessCase implements StoreJobBusinessCaseInterface
20
{
21
22
    /**
23
     * @var JobDependencyServiceInterface
24
     */
25
    private $oJobDependencyService;
26
27
28 13 View Code Duplication
    public function __construct(
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
29
        JobIndexServiceInterface $oJobIndexService,
30
        JobRepositoryInterface $oJobRepositoryRemote,
31
        JobRepositoryInterface $oJobRepositoryLocal,
32
        JobComparisonInterface  $oJobComparisonBusinessCase,
33
        JobDependencyServiceInterface $oJobDependencyService,
34
        LoggerInterface $oLogger
35
    )
36
    {
37 13
        $this->oJobIndexService = $oJobIndexService;
38 13
        $this->oJobRepositoryRemote = $oJobRepositoryRemote;
39 13
        $this->oJobRepositoryLocal = $oJobRepositoryLocal;
40 13
        $this->oJobComparisonBusinessCase = $oJobComparisonBusinessCase;
41 13
        $this->oJobDependencyService = $oJobDependencyService;
42 13
        $this->oLogger = $oLogger;
43 13
    }
44
45
    /**
46
     * @inheritdoc
47
     */
48 11 View Code Duplication
    public function storeIndexedJobs()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
49
    {
50
        // add new jobs to chronos
51 11
        $_aNewJobs = $this->oJobComparisonBusinessCase->getRemoteMissingJobs();
52 11
        foreach ($_aNewJobs as $_sJobName)
53
        {
54 6
            $this->hasAddedJob($_sJobName);
55 11
        }
56
57
        // delete missing jobs from chronos
58 11
        $_aMissingJobs = $this->oJobComparisonBusinessCase->getLocalMissingJobs();
59 11
        foreach ($_aMissingJobs as $_sJobName)
60
        {
61 5
            $this->hasRemovedJob($_sJobName);
62 11
        }
63
64
        // update jobs on chronos
65 11
        $_aLocalJobUpdates = $this->oJobComparisonBusinessCase->getLocalJobUpdates();
66 11
        foreach ($_aLocalJobUpdates as $_sJobName)
67
        {
68 4
            $this->hasUpdatedJob($_sJobName);
69 11
        }
70 11
    }
71
72
    /**
73
     * @param string $sJobName
74
     * @return bool
75
     */
76 6
    private function hasAddedJob($sJobName)
77
    {
78 6
        $_oJobEntityLocal = $this->oJobRepositoryLocal->getJob($sJobName);
79
80 6
        if (!$_oJobEntityLocal instanceof ChronosJobEntity) {
81
            throw new \RuntimeException('Expected ChronosJobEntity. Received something else.');
82
        }
83
84 6
        if ($this->isAbleToStoreEntity($_oJobEntityLocal))
85 6
        {
86 4
            if ($this->oJobRepositoryRemote->addJob($_oJobEntityLocal))
87 4
            {
88 3
                $this->oJobIndexService->removeJob($_oJobEntityLocal->getKey());
89 3
                $this->oLogger->notice(sprintf(
90 3
                    'Job "%s" successfully added to chronos',
91 3
                    $_oJobEntityLocal->getKey()
92 3
                ));
93
94 3
                return true;
95
            }
96
97 1
            $this->oLogger->error(sprintf(
98 1
                'Failed to add job "%s" to chronos',
99 1
                $_oJobEntityLocal->getKey()
100 1
            ));
101 1
        }
102
103 3
        return false;
104
    }
105
106
    /**
107
     * @param $sJobName
108
     * @return bool
109
     */
110 5 View Code Duplication
    private function hasRemovedJob($sJobName)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
111
    {
112 5
        if ($this->isAbleToDeleteJob($sJobName))
113 5
        {
114 3
            if ($this->oJobRepositoryRemote->removeJob($sJobName))
115 3
            {
116 3
                $this->oJobIndexService->removeJob($sJobName);
117 3
                $this->oLogger->notice(sprintf(
118 3
                    'Job "%s" successfully removed from chronos',
119
                    $sJobName
120 3
                ));
121
122 3
                return true;
123
            }
124
125
            $this->oLogger->error(sprintf(
126
                'Failed to remove job "%s" from chronos',
127
                $sJobName
128
            ));
129
        }
130
131 2
        return false;
132
    }
133
134
    /**
135
     * @param string $sJobName
136
     * @return bool
137
     */
138 4
    private function hasUpdatedJob($sJobName)
139
    {
140 4
        $_oJobEntityLocal = $this->oJobRepositoryLocal->getJob($sJobName);
141
142 4
        if (!$_oJobEntityLocal instanceof ChronosJobEntity) {
143
            throw new \RuntimeException('Expected ChronosJobEntity. Received something else.');
144
        }
145
146 4
        if ($this->isAbleToStoreEntity($_oJobEntityLocal))
147 4
        {
148 3
            $_oJobEntityChronos = $this->oJobRepositoryRemote->getJob($sJobName);
149
150
            // handle job update
151 3
            if ($this->oJobComparisonBusinessCase->hasSameJobType($_oJobEntityLocal, $_oJobEntityChronos))
152 3
            {
153 2
                $_bHasUpdatedJob = $this->oJobRepositoryRemote->updateJob($_oJobEntityLocal);
154 2
            }
155
            else
156
            {
157
                $_bHasUpdatedJob = (
158 1
                    $this->oJobRepositoryRemote->removeJob($_oJobEntityChronos->getKey())
159 1
                    && $this->oJobRepositoryRemote->addJob($_oJobEntityLocal)
160 1
                );
161
            }
162
163
            // handle update result
164
            if ($_bHasUpdatedJob)
165 3
            {
166 3
                $this->oJobIndexService->removeJob($_oJobEntityLocal->getKey());
167 3
                $this->oLogger->notice(sprintf(
168 3
                    'Job "%s" successfully updated in chronos',
169 3
                    $_oJobEntityLocal->getKey()
170 3
                ));
171
172 3
                return true;
173
            }
174
175
            // in case of an error
176
            $this->oLogger->error(sprintf(
177
                'Failed to update job "%s" in chronos',
178
                $_oJobEntityLocal->getKey()
179
            ));
180
        }
181
182 1
        return false;
183
    }
184
185
    /**
186
     * @param ChronosJobEntity $oEntity
187
     * @return bool
188
     */
189 8
    private function isAbleToStoreEntity(ChronosJobEntity $oEntity)
190
    {
191 8
        if ($this->oJobIndexService->isJobInIndex($oEntity->getKey()))
192 8
        {
193 7
            if ($oEntity->isSchedulingJob())
194 7
            {
195 5
                return true;
196
            }
197
198
            //else :: are all parents available?
199 2
            foreach ($oEntity->parents as $_sParentJobName)
200
            {
201 2
                if (false === $this->oJobRepositoryRemote->hasJob($_sParentJobName))
202 2
                {
203 1
                    $this->oLogger->warning(sprintf(
204 1
                        'Parent job is not available for "%s" on chronos. Please add parent "%s" first.',
205 1
                        $oEntity->name,
206
                        $_sParentJobName
207 1
                    ));
208
209 1
                    return false;
210
                }
211 1
            }
212
213 1
            return true;
214
        }
215
216 2
        return false;
217
    }
218
219
    /**
220
     * @param string $sJobName
221
     * @return bool
222
     */
223 5
    private function isAbleToDeleteJob($sJobName)
224
    {
225 5
        if ($this->oJobIndexService->isJobInIndex($sJobName))
226 5
        {
227 4
            $_aChildJobs = $this->oJobDependencyService->getChildJobs($sJobName, JobDependencyServiceInterface::REPOSITORY_CHRONOS);
228 4
            if (empty($_aChildJobs))
229 4
            {
230 3
                return true;
231
            }
232
233
            // else :: are child also in index to delete?
234 1
            foreach ($_aChildJobs as $_sChildJobName)
235
            {
236 1
                if (false === $this->oJobIndexService->isJobInIndex($_sChildJobName))
237 1
                {
238 1
                    $this->oLogger->warning(sprintf(
239 1
                        'Child job is still available for "%s" on chronos. Please remove child "%s" first.',
240 1
                        $sJobName,
241
                        $_sChildJobName
242 1
                    ));
243
244 1
                    return false;
245
                }
246
            }
247
248
            // child job are also in index
249
            return true;
250
        }
251
252 1
        return false;
253
    }
254
}