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
Push — master ( 0c6bcd...b3f485 )
by Andy
11s
created

MarathonJobComparisonBusinessCase   B

Complexity

Total Complexity 37

Size/Duplication

Total Lines 158
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 88.52%

Importance

Changes 0
Metric Value
wmc 37
lcom 1
cbo 5
dl 0
loc 158
ccs 54
cts 61
cp 0.8852
rs 8.6
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
C preCompareModifications() 0 49 13
A getEntitySetWithDefaults() 0 4 1
A hasSameJobType() 0 6 1
A isEntityEqual() 0 10 3
B isEqual() 0 12 9
A isArrayEqual() 0 4 2
B isArrayHalfEqual() 0 20 7
1
<?php
2
/**
3
 *
4
 * @package: chapi
5
 *
6
 * @author: bthapaliya
7
 * @since: 2016-12-14
8
 *
9
 */
10
11
namespace Chapi\BusinessCase\Comparison;
12
13
use Chapi\Component\Comparison\DiffCompareInterface;
14
use Chapi\Entity\Chronos\ChronosJobEntity;
15
use Chapi\Entity\JobEntityInterface;
16
use Chapi\Entity\Marathon\AppEntity\DockerPortMapping;
17
use Chapi\Entity\Marathon\MarathonAppEntity;
18
use Chapi\Service\JobRepository\JobRepositoryInterface;
19
20
class MarathonJobComparisonBusinessCase extends AbstractJobComparisionBusinessCase
21
{
22
    /**
23
     * @param JobRepositoryInterface $localRepository
24
     * @param JobRepositoryInterface $remoteRepository
25
     * @param DiffCompareInterface $diffCompare
26
     */
27 25
    public function __construct(
28
        JobRepositoryInterface $localRepository,
29
        JobRepositoryInterface $remoteRepository,
30
        DiffCompareInterface $diffCompare
31
    ) {
32 25
        $this->remoteRepository = $remoteRepository;
33 25
        $this->localRepository = $localRepository;
34 25
        $this->diffCompare = $diffCompare;
35 25
    }
36
37 5
    protected function preCompareModifications(JobEntityInterface &$localJob, JobEntityInterface &$remoteJob)
38
    {
39 5
        if (!$localJob instanceof MarathonAppEntity ||
40 5
            !$remoteJob instanceof MarathonAppEntity
41
        ) {
42
            throw new \RuntimeException('Required MarathonAppEntity. Something else encountered.');
43
        }
44
        // marathon returns portDefinitions values for auto configured port as well
45
        // we want to only check if the port is defined in local file.
46
        // otherwise we ignore the remote values.
47 5
        if (!$localJob->portDefinitions) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $localJob->portDefinitions of type Chapi\Entity\Marathon\AppEntity\PortDefinition[] is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
48 5
            $remoteJob->portDefinitions = null;
0 ignored issues
show
Documentation Bug introduced by
It seems like null of type null is incompatible with the declared type array<integer,object<Cha...Entity\PortDefinition>> of property $portDefinitions.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
49
        }
50
51 5
        if ($localJob->container && $localJob->container->docker &&
52 5
            $remoteJob->container && $remoteJob->container->docker) {
53
54 2
            $localPortMappings = $localJob->container->docker->portMappings;
55 2
            $remotePortMappings = $remoteJob->container->docker->portMappings;
56
57 2
            usort($localPortMappings, DockerPortMapping::class . '::less');
58 2
            usort($remotePortMappings, DockerPortMapping::class . '::less');
59
60 2
            foreach ($localPortMappings as $index => $localPortMapping) {
61 2
                if ($localPortMapping->servicePort !== 0) {
62
                    continue;
63
                }
64
65 2
                if (!isset($remotePortMappings[$index])) {
66 1
                    continue;
67
                }
68
69 1
                $remotePortMapping = $remotePortMappings[$index];
70
71 1
                if (DockerPortMapping::less($remotePortMapping, $localPortMapping) != 0) {
72 1
                    $fixedPortMapping = clone $remotePortMapping;
73 1
                    $fixedPortMapping->servicePort = 0;
74
75 1
                    if (DockerPortMapping::less($fixedPortMapping, $localPortMapping) == 0) {
76 1
                        unset($localPortMappings[$index]);
77 1
                        unset($remotePortMappings[$index]);
78
                    }
79
                }
80
            }
81
82 2
            $localJob->container->docker->portMappings = array_values($localPortMappings);
0 ignored issues
show
Documentation Bug introduced by
It seems like array_values($localPortMappings) of type array<integer,?> is incompatible with the declared type array<integer,object<Cha...ity\DockerPortMapping>> of property $portMappings.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
83 2
            $remoteJob->container->docker->portMappings = array_values($remotePortMappings);
0 ignored issues
show
Documentation Bug introduced by
It seems like array_values($remotePortMappings) of type array<integer,?> is incompatible with the declared type array<integer,object<Cha...ity\DockerPortMapping>> of property $portMappings.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
84
        }
85 5
    }
86
87
    /**
88
     * @return JobEntityInterface
89
     */
90
    protected function getEntitySetWithDefaults()
91
    {
92
        return new MarathonAppEntity();
93
    }
94
95
    /**
96
     * @param JobEntityInterface|ChronosJobEntity $jobEntityA
97
     * @param JobEntityInterface|ChronosJobEntity $jobEntityB
98
     * @return bool
99
     */
100
    public function hasSameJobType(JobEntityInterface $jobEntityA, JobEntityInterface $jobEntityB)
101
    {
102
        // for now we don't have a concrete seperation
103
        // of types for marathon.
104
        return true;
105
    }
106
107
    /**
108
     * @param $property
109
     * @param $jobEntityA
110
     * @param $jobEntityB
111
     * @return bool
112
     */
113 19
    protected function isEntityEqual($property, JobEntityInterface $jobEntityA, JobEntityInterface $jobEntityB)
114
    {
115 19
        if (!$jobEntityA instanceof MarathonAppEntity ||
116 19
            !$jobEntityB instanceof MarathonAppEntity
117
        ) {
118
            throw new \RuntimeException('Required MarathonAppEntity. Something else encountered.');
119
        }
120
121 19
        return $this->isEqual($jobEntityA->{$property}, $jobEntityB->{$property});
122
    }
123
124
    /**
125
     * @param mixed $valueA
126
     * @param mixed $valueB
127
     * @return bool
128
     */
129 20
    private function isEqual($valueA, $valueB)
130
    {
131 20
        if (is_array($valueA) && is_array($valueB)) {
132 8
            return $this->isArrayEqual($valueA, $valueB);
133 19
        } elseif (is_object($valueA) && is_object($valueB)) {
134 9
            return $this->isArrayEqual(get_object_vars($valueA), get_object_vars($valueB));
135 17
        } elseif ((is_scalar($valueA) && is_scalar($valueB)) || (is_null($valueA) && is_null($valueB))) {
136 13
            return $valueA == $valueB;
137
        }
138
139 5
        return false;
140
    }
141
142
    /**
143
     * @param array $valuesA
144
     * @param array $valuesB
145
     * @return bool
146
     */
147 13
    private function isArrayEqual(array $valuesA, array $valuesB)
148
    {
149 13
        return $this->isArrayHalfEqual($valuesA, $valuesB) && $this->isArrayHalfEqual($valuesB, $valuesA);
150
    }
151
152
    /**
153
     * @param array $valuesA
154
     * @param array $valuesB
155
     * @return bool
156
     */
157 13
    private function isArrayHalfEqual(array $valuesA, array $valuesB)
158
    {
159 13
        foreach ($valuesA as $keyA => $valueA) {
160 12
            if (is_string($keyA)) {
161 9
                if (!array_key_exists($keyA, $valuesB) || !$this->isEqual($valueA, $valuesB[$keyA])) {
162 9
                    return false;
163
                }
164
            } else {
165 7
                foreach ($valuesB as $valueB) {
166 6
                    if ($this->isEqual($valueA, $valueB)) {
167 6
                        continue 2;
168
                    }
169
                }
170
171 9
                return false;
172
            }
173
        }
174
175 9
        return true;
176
    }
177
}
178