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

isArrayHalfEqual()   C

Complexity

Conditions 7
Paths 6

Size

Total Lines 27
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 7

Importance

Changes 0
Metric Value
dl 0
loc 27
ccs 16
cts 16
cp 1
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 11
nc 6
nop 2
crap 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
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
        {
54 1
            $oRemoteJob->portDefinitions = [];
55 1
        }
56 1
    }
57
58
    /**
59
     * @return JobEntityInterface
60
     */
61
    protected function getEntitySetWithDefaults()
62
    {
63
        return new MarathonAppEntity();
64
    }
65
66
    /**
67
     * @param JobEntityInterface|ChronosJobEntity $oJobEntityA
68
     * @param JobEntityInterface|ChronosJobEntity $oJobEntityB
69
     * @return bool
70
     */
71
    public function hasSameJobType(JobEntityInterface $oJobEntityA, JobEntityInterface $oJobEntityB)
72
    {
73
        // for now we don't have a concrete seperation
74
        // of types for marathon.
75
        return true;
76
    }
77
78
    /**
79
     * @param $sProperty
80
     * @param $oJobEntityA
81
     * @param $oJobEntityB
82
     * @return bool
83
     */
84 16
    protected function isEntityEqual($sProperty, JobEntityInterface $oJobEntityA, JobEntityInterface $oJobEntityB)
85
    {
86
        if (
87 16
            !$oJobEntityA instanceof MarathonAppEntity ||
88
            !$oJobEntityB instanceof MarathonAppEntity
89 16
        )
90 16
        {
91
            throw new \RuntimeException('Required MarathonAppEntity. Something else encountered.');
92
        }
93
94 16
        return $this->isEqual($oJobEntityA->{$sProperty}, $oJobEntityB->{$sProperty});
95
    }
96
97
    /**
98
     * @param mixed $mValueA
99
     * @param mixed $mValueB
100
     * @return bool
101
     */
102 16
    private function isEqual($mValueA, $mValueB)
103
    {
104 16
        if (is_array($mValueA) && is_array($mValueB))
105 16
        {
106 7
            return $this->isArrayEqual($mValueA, $mValueB);
107
        }
108 10
        elseif (is_object($mValueA) && is_object($mValueB))
109
        {
110 3
            return $this->isArrayEqual(get_object_vars($mValueA), get_object_vars($mValueB));
111
        }
112 9
        elseif ((is_scalar($mValueA) && is_scalar($mValueB)) || (is_null($mValueA) && is_null($mValueB)))
113
        {
114 6
            return $mValueA == $mValueB;
115
        }
116
117 4
        return false;
118
    }
119
120
    /**
121
     * @param array $aValuesA
122
     * @param array $aValuesB
123
     * @return bool
124
     */
125 10
    private function isArrayEqual(array $aValuesA, array $aValuesB)
126
    {
127 10
        return $this->isArrayHalfEqual($aValuesA, $aValuesB) && $this->isArrayHalfEqual($aValuesB, $aValuesA);
128
    }
129
130
    /**
131
     * @param array $aValuesA
132
     * @param array $aValuesB
133
     * @return bool
134
     */
135 10
    private function isArrayHalfEqual(array $aValuesA, array $aValuesB)
136
    {
137 10
        foreach ($aValuesA as $_mKeyA => $_mValueA)
138
        {
139 9
            if (is_string($_mKeyA))
140 9
            {
141 3
                if (!array_key_exists($_mKeyA, $aValuesB) || !$this->isEqual($_mValueA, $aValuesB[$_mKeyA]))
142 3
                {
143 1
                    return false;
144
                }
145 2
            }
146
            else
147
            {
148 6
                foreach ($aValuesB as $_mValueB)
149
                {
150 6
                    if ($_mValueA == $_mValueB)
151 6
                    {
152 4
                        continue 2;
153
                    }
154 3
                }
155
156 3
                return false;
157
            }
158 7
        }
159
160 7
        return true;
161
    }
162
}
163