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

preCompareModifications()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 16
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 4.0218

Importance

Changes 0
Metric Value
dl 0
loc 16
ccs 8
cts 9
cp 0.8889
rs 9.2
c 0
b 0
f 0
cc 4
eloc 7
nc 3
nop 2
crap 4.0218
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
14
use Chapi\Component\Comparison\DiffCompareInterface;
15
use Chapi\Entity\Chronos\ChronosJobEntity;
16
use Chapi\Entity\Chronos\JobCollection;
17
use Chapi\Entity\JobEntityInterface;
18
use Chapi\Entity\Marathon\AppEntity\PortDefinition;
19
use Chapi\Entity\Marathon\MarathonAppEntity;
20
use Chapi\Service\JobRepository\JobRepositoryInterface;
21
22
class MarathonJobComparisonBusinessCase extends AbstractJobComparisionBusinessCase
23
{
24
    /**
25
     * @param JobRepositoryInterface $oLocalRepository
26
     * @param JobRepositoryInterface $oRemoteRepository
27
     * @param DiffCompareInterface $oDiffCompare
28
     */
29 19
    public function __construct(
30
        JobRepositoryInterface $oLocalRepository,
31
        JobRepositoryInterface $oRemoteRepository,
32
        DiffCompareInterface $oDiffCompare
33
    )
34
    {
35 19
        $this->oRemoteRepository = $oRemoteRepository;
36 19
        $this->oLocalRepository = $oLocalRepository;
37 19
        $this->oDiffCompare = $oDiffCompare;
38 19
    }
39
40 1
    protected function preCompareModifications(JobEntityInterface &$oLocalJob, JobEntityInterface &$oRemoteJob)
41
    {
42
        if (
43 1
            !$oLocalJob instanceof MarathonAppEntity ||
44
            !$oRemoteJob instanceof MarathonAppEntity
45 1
        )
46 1
        {
47
            throw new \RuntimeException('Required MarathonAppEntity. Something else encountered.');
48
        }
49
        // marathon returns portDefinitions values for auto configured port as well
50
        // we want to only check if the port is defined in local file.
51
        // otherwise we ignore the remote values.
52 1
        if (empty($oLocalJob->portDefinitions)) {
53 1
            $oRemoteJob->portDefinitions = [];
54 1
        }
55 1
    }
56
57
    /**
58
     * @return JobEntityInterface
59
     */
60
    protected function getEntitySetWithDefaults() {
61
        return new MarathonAppEntity();
62
    }
63
64
    /**
65
     * @param JobEntityInterface|ChronosJobEntity $oJobEntityA
66
     * @param JobEntityInterface|ChronosJobEntity $oJobEntityB
67
     * @return bool
68
     */
69
    public function hasSameJobType(JobEntityInterface $oJobEntityA, JobEntityInterface $oJobEntityB)
70
    {
71
        // for now we don't have a concrete seperation
72
        // of types for marathon.
73
        return true;
74
    }
75
76
    /**
77
     * @param $sProperty
78
     * @param $oJobEntityA
79
     * @param $oJobEntityB
80
     * @return bool
81
     */
82 16
    protected function isEntityEqual($sProperty, JobEntityInterface $oJobEntityA, JobEntityInterface $oJobEntityB)
83
    {
84
        if (
85 16
            !$oJobEntityA instanceof MarathonAppEntity ||
86
            !$oJobEntityB instanceof MarathonAppEntity
87 16
        )
88 16
        {
89
            throw new \RuntimeException('Required MarathonAppEntity. Something else encountered.');
90
        }
91
92 16
        return $this->isEqual($oJobEntityA->{$sProperty}, $oJobEntityB->{$sProperty});
93
    }
94
95
    /**
96
     * @param mixed $mValueA
97
     * @param mixed $mValueB
98
     * @return bool
99
     */
100 16
    private function isEqual($mValueA, $mValueB)
101
    {
102 16
        if (is_array($mValueA) && is_array($mValueB)) {
103 7
            return $this->isArrayEqual($mValueA, $mValueB);
104
        }
105 10
        elseif (is_object($mValueA) && is_object($mValueB))
106
        {
107 3
            return $this->isArrayEqual(get_object_vars($mValueA), get_object_vars($mValueB));
108
        }
109 9
        elseif ((is_scalar($mValueA) && is_scalar($mValueB)) || (is_null($mValueA) && is_null($mValueB)))
110
        {
111 6
            return $mValueA == $mValueB;
112
        }
113
114 4
        return false;
115
    }
116
117
    /**
118
     * @param array $aValuesA
119
     * @param array $aValuesB
120
     * @return bool
121
     */
122 10
    private function isArrayEqual(array $aValuesA, array $aValuesB)
123
    {
124 10
        return $this->isArrayHalfEqual($aValuesA, $aValuesB) && $this->isArrayHalfEqual($aValuesB, $aValuesA);
125
    }
126
127
    /**
128
     * @param array $aValuesA
129
     * @param array $aValuesB
130
     * @return bool
131
     */
132 10
    private function isArrayHalfEqual(array $aValuesA, array $aValuesB)
133
    {
134 10
        foreach ($aValuesA as $_mKeyA => $_mValueA)
135
        {
136 9
            if (is_string($_mKeyA))
137 9
            {
138 3
                if (!array_key_exists($_mKeyA, $aValuesB) || !$this->isEqual($_mValueA, $aValuesB[$_mKeyA]))
139 3
                {
140 1
                    return false;
141
                }
142 2
            }
143
            else
144
            {
145 6
                foreach ($aValuesB as $_mValueB)
146
                {
147 6
                    if ($_mValueA == $_mValueB)
148 6
                    {
149 4
                        continue 2;
150
                    }
151 3
                }
152
153 3
                return false;
154
            }
155 7
        }
156
157 7
        return true;
158
    }
159
}
160