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 ( 42287f...fba610 )
by Andy
03:33
created

MarathonJobComparisonBusinessCase   A

Complexity

Total Complexity 28

Size/Duplication

Total Lines 123
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 28
lcom 1
cbo 2
dl 0
loc 123
ccs 35
cts 35
cp 1
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A preCompareModifications() 0 14 4
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\MarathonAppEntity;
17
use Chapi\Service\JobRepository\JobRepositoryInterface;
18
19
class MarathonJobComparisonBusinessCase extends AbstractJobComparisionBusinessCase
20
{
21
    /**
22
     * @param JobRepositoryInterface $localRepository
23
     * @param JobRepositoryInterface $remoteRepository
24
     * @param DiffCompareInterface $diffCompare
25
     */
26 22
    public function __construct(
27
        JobRepositoryInterface $localRepository,
28
        JobRepositoryInterface $remoteRepository,
29
        DiffCompareInterface $diffCompare
30
    ) {
31 22
        $this->remoteRepository = $remoteRepository;
32 22
        $this->localRepository = $localRepository;
33 22
        $this->diffCompare = $diffCompare;
34 22
    }
35
36 4
    protected function preCompareModifications(JobEntityInterface &$localJob, JobEntityInterface &$remoteJob)
37
    {
38 4
        if (!$localJob instanceof MarathonAppEntity ||
39 4
            !$remoteJob instanceof MarathonAppEntity
40
        ) {
41
            throw new \RuntimeException('Required MarathonAppEntity. Something else encountered.');
42
        }
43
        // marathon returns portDefinitions values for auto configured port as well
44
        // we want to only check if the port is defined in local file.
45
        // otherwise we ignore the remote values.
46 4
        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...
47 4
            $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...
48
        }
49 4
    }
50
51
    /**
52
     * @return JobEntityInterface
53
     */
54
    protected function getEntitySetWithDefaults()
55
    {
56
        return new MarathonAppEntity();
57
    }
58
59
    /**
60
     * @param JobEntityInterface|ChronosJobEntity $jobEntityA
61
     * @param JobEntityInterface|ChronosJobEntity $jobEntityB
62
     * @return bool
63
     */
64
    public function hasSameJobType(JobEntityInterface $jobEntityA, JobEntityInterface $jobEntityB)
65
    {
66
        // for now we don't have a concrete seperation
67
        // of types for marathon.
68
        return true;
69
    }
70
71
    /**
72
     * @param $property
73
     * @param $jobEntityA
74
     * @param $jobEntityB
75
     * @return bool
76
     */
77 16
    protected function isEntityEqual($property, JobEntityInterface $jobEntityA, JobEntityInterface $jobEntityB)
78
    {
79 16
        if (!$jobEntityA instanceof MarathonAppEntity ||
80 16
            !$jobEntityB instanceof MarathonAppEntity
81
        ) {
82
            throw new \RuntimeException('Required MarathonAppEntity. Something else encountered.');
83
        }
84
85 16
        return $this->isEqual($jobEntityA->{$property}, $jobEntityB->{$property});
86
    }
87
88
    /**
89
     * @param mixed $valueA
90
     * @param mixed $valueB
91
     * @return bool
92
     */
93 17
    private function isEqual($valueA, $valueB)
94
    {
95 17
        if (is_array($valueA) && is_array($valueB)) {
96 7
            return $this->isArrayEqual($valueA, $valueB);
97 16
        } elseif (is_object($valueA) && is_object($valueB)) {
98 6
            return $this->isArrayEqual(get_object_vars($valueA), get_object_vars($valueB));
99 14
        } elseif ((is_scalar($valueA) && is_scalar($valueB)) || (is_null($valueA) && is_null($valueB))) {
100 10
            return $valueA == $valueB;
101
        }
102
103 5
        return false;
104
    }
105
106
    /**
107
     * @param array $valuesA
108
     * @param array $valuesB
109
     * @return bool
110
     */
111 10
    private function isArrayEqual(array $valuesA, array $valuesB)
112
    {
113 10
        return $this->isArrayHalfEqual($valuesA, $valuesB) && $this->isArrayHalfEqual($valuesB, $valuesA);
114
    }
115
116
    /**
117
     * @param array $valuesA
118
     * @param array $valuesB
119
     * @return bool
120
     */
121 10
    private function isArrayHalfEqual(array $valuesA, array $valuesB)
122
    {
123 10
        foreach ($valuesA as $keyA => $valueA) {
124 9
            if (is_string($keyA)) {
125 6
                if (!array_key_exists($keyA, $valuesB) || !$this->isEqual($valueA, $valuesB[$keyA])) {
126 6
                    return false;
127
                }
128
            } else {
129 6
                foreach ($valuesB as $valueB) {
130 6
                    if ($this->isEqual($valueA, $valueB)) {
131 6
                        continue 2;
132
                    }
133
                }
134
135 6
                return false;
136
            }
137
        }
138
139 7
        return true;
140
    }
141
}
142