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

ChronosStoreJobBusinessCase::isAbleToStoreEntity()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 29
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 5

Importance

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