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

MarathonStoreJobBusinessCase::updateAppInRemote()   B

Complexity

Conditions 4
Paths 3

Size

Total Lines 29
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 4.343

Importance

Changes 0
Metric Value
dl 0
loc 29
ccs 13
cts 18
cp 0.7221
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 15
nc 3
nop 1
crap 4.343
1
<?php
2
/**
3
 *
4
 * @package: chapi
5
 *
6
 * @author: bthapaliya
7
 * @since: 2017-01-03
8
 *
9
 */
10
11
namespace Chapi\BusinessCase\JobManagement;
12
13
14
use Chapi\BusinessCase\Comparison\JobComparisonInterface;
15
use Chapi\Entity\JobEntityInterface;
16
use Chapi\Entity\Marathon\MarathonAppEntity;
17
use Chapi\Service\JobIndex\JobIndexServiceInterface;
18
use Chapi\Service\JobRepository\JobRepositoryInterface;
19
use Psr\Log\LoggerInterface;
20
21
class MarathonStoreJobBusinessCase extends AbstractStoreJobBusinessCase implements StoreJobBusinessCaseInterface
22
{
23 12 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...
24
        JobIndexServiceInterface $oJobIndexService,
25
        JobRepositoryInterface $oJobRepositoryRemote,
26
        JobRepositoryInterface $oJobRepositoryLocal,
27
        JobComparisonInterface $oJobComparisonBusinessCase,
28
        LoggerInterface $oLogger
29
30
    )
31
    {
32
33 12
        $this->oJobIndexService = $oJobIndexService;
34 12
        $this->oLogger = $oLogger;
35 12
        $this->oJobComparisonBusinessCase = $oJobComparisonBusinessCase;
36 12
        $this->oJobRepositoryRemote = $oJobRepositoryRemote;
37 12
        $this->oJobRepositoryLocal = $oJobRepositoryLocal;
38 12
    }
39
40
    /**
41
     * @return void
42
     */
43 7 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...
44
    {
45 7
        $_aRemoteMissingApps = $this->oJobComparisonBusinessCase->getRemoteMissingJobs();
46 7
        foreach ($_aRemoteMissingApps as $_sAppId)
47
        {
48 5
            $this->addRemoteMissingApp($_sAppId);
49 7
        }
50
51 7
        $_aLocalMissingApps = $this->oJobComparisonBusinessCase->getLocalMissingJobs();
52 7
        foreach ($_aLocalMissingApps as $_sAppId)
53
        {
54 1
            $this->removeLocalMissingAppInRemote($_sAppId);
55 7
        }
56 7
        $_aLocalUpdates = $this->oJobComparisonBusinessCase->getLocalJobUpdates();
57 7
        foreach ($_aLocalUpdates as $_sAppId)
58
        {
59 1
            $this->updateAppInRemote($_sAppId);
60 7
        }
61 7
    }
62
63 5
    private function addRemoteMissingApp($sAppId)
0 ignored issues
show
Coding Style introduced by
function addRemoteMissingApp() does not seem to conform to the naming convention (^(?:is|has|should|may|supports)).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
64
    {
65 5
        if ($this->oJobIndexService->isJobInIndex($sAppId))
66 5
        {
67
            /** @var MarathonAppEntity $_oJobEntityLocal */
68 5
            $_oJobEntityLocal = $this->oJobRepositoryLocal->getJob($sAppId);
69
70 5
            if (!$_oJobEntityLocal instanceof MarathonAppEntity) {
71
                throw new \RuntimeException('Encountered entity that is not MarathonAppEntity');
72
            }
73
74
            // check if dependency is satisfied
75 5
            if ( $_oJobEntityLocal->isDependencyJob())
76 5
            {
77
                try {
78 4
                    $circular = $this->isDependencyCircular($_oJobEntityLocal, count($_oJobEntityLocal->dependencies));
79
                    if ($circular)
80 3
                    {
81 1
                        $this->oLogger->error(sprintf(
82 1
                            'The dependency for %s is circular. Please fix them.', $sAppId
83 1
                        ));
84 1
                        return false;
85
                    }
86
                }
87 3
                catch(\Exception $e)
88
                {
89 1
                    $this->oLogger->error(sprintf(
90 1
                        'Job %s cannot be added to remote : %s',$sAppId, $e->getMessage()
91 1
                    ));
92 1
                    return false;
93
                }
94
95
96 2
                foreach ($_oJobEntityLocal->dependencies as $_sDependencyKey) {
97 2
                    $_bAdded = $this->addRemoteMissingApp($_sDependencyKey);
98
99 2
                    if (!$_bAdded)
100 2
                    {
101 1
                        $this->oLogger->error(sprintf(
102 1
                            'Job "%s" is dependent on "%s" which is missing. Please add them and try again.',
103 1
                            $sAppId,
104
                            $_sDependencyKey
105 1
                        ));
106 1
                        $this->oJobIndexService->removeJob($_sDependencyKey);
107 1
                        return false;
108
                    }
109 1
                }
110 1
            }
111
112 2
            if ($this->oJobRepositoryRemote->addJob($_oJobEntityLocal))
113 2
            {
114 2
                $this->oJobIndexService->removeJob($_oJobEntityLocal->getKey());
115 2
                $this->oLogger->notice(sprintf(
116 2
                    'Job "%s" successfully added to marathon',
117 2
                    $_oJobEntityLocal->getKey()
118 2
                ));
119
120 2
                return true;
121
            }
122
            $this->oLogger->error(sprintf(
123
                'Failed to add job "%s" to marathon',
124
                $_oJobEntityLocal->getKey()
125
            ));
126
        }
127 2
        return false;
128
129
    }
130
131 4
    private function hasDuplicates($arr)
132
    {
133 4
        return !(count($arr) == count(array_unique($arr)));
134
    }
135
136 4
    private function isDependencyCircular(MarathonAppEntity $oEntity, $iImmediateChildren, &$path=[])
137
    {
138
        // Invariant: path will not have duplicates for acyclic dependency tree
139 4
        if ($this->hasDuplicates($path))
140 4
        {
141 1
            return true;
142
        }
143
144
        // if we hit leaf (emptyarray), and have no
145
        // cycle yet, then remove the leaf and return false
146
        // removing leaf will help maintain a proper path from root to leaf
147
        // For tree : A ---> B ---> D
148
        //                      |-> C
149
        // When we reach node D, path will be [A, B, D]
150
        // so we pop off D so that the next append will properly show [A, B, C] (legit path)
151 4
        if (empty($oEntity->dependencies))
152 4
        {
153 2
            array_pop($path);
154 2
            return false;
155
        }
156
157 4
        foreach ($oEntity->dependencies as $_sDependency)
158
        {
159
            // add this key in path as we will explore its child now
160 4
            $path[] = $oEntity->getKey();
161
162
            /** @var MarathonAppEntity $_oDependEntity */
163 4
            $_oDependEntity = $this->oJobRepositoryLocal->getJob($_sDependency);
164
165 4
            if (!$_oDependEntity)
166 4
            {
167 1
                throw new \Exception(sprintf('Dependency chain on non-existing app "%s"', $_sDependency));
168
            }
169
170 3
            if (!$_oDependEntity instanceof MarathonAppEntity) {
171
                throw new \RuntimeException('Expected MarathonAppEntity. Found something else');
172
            }
173
174
175
            // check if dependency has cycle
176 3
            if ($this->isDependencyCircular($_oDependEntity, count($_oDependEntity->dependencies), $path))
177 3
            {
178 1
                return true;
179
            }
180
181
            // tracking immediateChildren, this helps us with
182
            // removing knowing when to pop off key for intermediary dependency
183
            // For tree: A ---> B ---> D
184
            //              |      |-> C
0 ignored issues
show
Unused Code Comprehensibility introduced by
43% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
185
            //              |->E
186
            // for B intermediate Child will be 2.
187
            // when we process D, it will be reduced to 1 and with C to 0
188
            // then we will pop B to generate path [A, E] when we reach E.
189 2
            $iImmediateChildren = $iImmediateChildren -1;
0 ignored issues
show
Coding Style introduced by
Decrement operators should be used where possible; found "$iImmediateChildren = $iImmediateChildren -1;" but expected "$iImmediateChildren--"
Loading history...
190 2
            if ($iImmediateChildren == 0)
191 2
            {
192 2
                array_pop($path);
193 2
            }
194 2
        }
195 2
    }
196
197
198 1 View Code Duplication
    private function removeLocalMissingAppInRemote($sAppId)
0 ignored issues
show
Coding Style introduced by
function removeLocalMissingAppInRemote() does not seem to conform to the naming convention (^(?:is|has|should|may|supports)).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
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...
199
    {
200 1
        if ($this->oJobIndexService->isJobInIndex($sAppId))
201 1
        {
202 1
            if ($this->oJobRepositoryRemote->removeJob($sAppId))
203 1
            {
204 1
                $this->oJobIndexService->removeJob($sAppId);
205 1
                $this->oLogger->notice(sprintf(
206 1
                    'Job "%s" successfully removed from marathon',
207
                    $sAppId
208 1
                ));
209
210 1
                return true;
211
            }
212
            $this->oLogger->error(sprintf(
213
                'Failed to remove"%s" from marathon',
214
                $sAppId
215
            ));
216
217
        }
218
        return false;
219
    }
220
221 1
    private function updateAppInRemote($sAppId)
0 ignored issues
show
Coding Style introduced by
function updateAppInRemote() does not seem to conform to the naming convention (^(?:is|has|should|may|supports)).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
222
    {
223 1
        if ($this->oJobIndexService->isJobInIndex($sAppId))
224 1
        {
225 1
            $_bRemoved = $this->oJobRepositoryRemote->removeJob($sAppId);
226
227 1
            $_oUpdatedConfig = $this->oJobRepositoryLocal->getJob($sAppId);
228 1
            $_bAddedBack = $this->oJobRepositoryRemote->addJob($_oUpdatedConfig);
229
230
            // updated
231 1
            if ($_bRemoved && $_bAddedBack)
232 1
            {
233 1
                $this->oJobIndexService->removeJob($sAppId);
234 1
                $this->oLogger->notice(sprintf(
235 1
                    'Job "%s" successfully updated in marathon',
236
                    $sAppId
237 1
                ));
238
239 1
                return true;
240
            }
241
242
            $this->oLogger->error(sprintf(
243
                'Failed to update job "%s" in marathon',
244
                $sAppId
245
            ));
246
        }
247
248
        return false;
249
    }
250
}