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

ChronosJobComparisonBusinessCase::isEntityEqual()   C

Complexity

Conditions 14
Paths 13

Size

Total Lines 40
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 17.8236

Importance

Changes 0
Metric Value
dl 0
loc 40
ccs 19
cts 26
cp 0.7308
rs 5.0864
c 0
b 0
f 0
cc 14
eloc 26
nc 13
nop 3
crap 17.8236

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * @package: chapi
4
 *
5
 * @author:  msiebeneicher
6
 * @since:   2015-07-29
7
 *
8
 */
9
10
namespace Chapi\BusinessCase\Comparison;
11
12
use Chapi\Component\Comparison\DiffCompareInterface;
13
use Chapi\Component\DatePeriod\DatePeriodFactoryInterface;
14
use Chapi\Entity\Chronos\JobCollection;
15
use Chapi\Entity\Chronos\ChronosJobEntity;
16
use Chapi\Entity\JobEntityInterface;
17
use Chapi\Service\JobRepository\JobRepositoryInterface;
18
use Psr\Log\LoggerInterface;
19
use Symfony\Component\Config\Definition\Exception\Exception;
20
21
class ChronosJobComparisonBusinessCase extends AbstractJobComparisionBusinessCase
22
{
23
    /**
24
     * @var DatePeriodFactoryInterface
25
     */
26
    private $oDatePeriodFactory;
27
28
    /**
29
     * @var LoggerInterface
30
     */
31
    private $oLogger;
32
33
34
    /**
35
     * @param JobRepositoryInterface $oJobRepositoryLocalChronos
36
     * @param JobRepositoryInterface $oJobRepositoryChronos
37
     * @param DiffCompareInterface $oDiffCompare
38
     * @param DatePeriodFactoryInterface $oDatePeriodFactory
39
     * @param LoggerInterface $oLogger
40
     */
41 7
    public function __construct(
42
        JobRepositoryInterface $oJobRepositoryLocalChronos,
43
        JobRepositoryInterface $oJobRepositoryChronos,
44
        DiffCompareInterface $oDiffCompare,
45
        DatePeriodFactoryInterface $oDatePeriodFactory,
46
        LoggerInterface $oLogger
47
    )
48
    {
49 7
        $this->oLocalRepository = $oJobRepositoryLocalChronos;
50 7
        $this->oRemoteRepository = $oJobRepositoryChronos;
51 7
        $this->oDiffCompare = $oDiffCompare;
52 7
        $this->oDatePeriodFactory = $oDatePeriodFactory;
53 7
        $this->oLogger = $oLogger;
54 7
    }
55
56
57 5
    protected function preCompareModifications(JobEntityInterface &$oLocalJob, JobEntityInterface &$oRemoteJob)
58
    {
59
        // no modification needed
60 5
        return;
61
    }
62
63
64
    protected function getEntitySetWithDefaults()
65
    {
66
        return new ChronosJobEntity();
67
    }
68
69
    /**
70
     * @param JobEntityInterface|ChronosJobEntity $oJobEntityA
71
     * @param JobEntityInterface|ChronosJobEntity $oJobEntityB
72
     * @return bool
73
     */
74 2
    public function hasSameJobType(JobEntityInterface $oJobEntityA, JobEntityInterface $oJobEntityB)
75
    {
76
        return (
77 2
            ($oJobEntityA->isSchedulingJob() && $oJobEntityB->isSchedulingJob())
78 2
            || ($oJobEntityA->isDependencyJob() && $oJobEntityB->isDependencyJob())
79 2
        );
80
    }
81
82
    /**
83
     * @param string $sProperty
84
     * @param JobEntityInterface $oJobEntityA
85
     * @param JobEntityInterface $oJobEntityB
86
     * @return bool
87
     */
88 5
    protected function isEntityEqual($sProperty, JobEntityInterface $oJobEntityA, JobEntityInterface $oJobEntityB)
89
    {
90
        if (
91 5
            !$oJobEntityA instanceof ChronosJobEntity ||
92
            !$oJobEntityB instanceof ChronosJobEntity
93 5
        )
94 5
        {
95
            throw new \RuntimeException('Required ChronosJobEntity. Something else encountered');
96
        }
97
98 5
        $mValueA = $oJobEntityA->{$sProperty};
99 5
        $mValueB = $oJobEntityB->{$sProperty};
100
101
        switch ($sProperty)
102
        {
103 5
            case 'schedule':
104 1
                return $this->isSchedulePropertyIdentical($oJobEntityA, $oJobEntityB);
105
106 5
            case 'scheduleTimeZone':
107 1
                return $this->isScheduleTimeZonePropertyIdentical($oJobEntityA, $oJobEntityB);
108
109 4
            case 'parents':
110
                return (
111
                    is_array($mValueA)
112
                    && is_array($mValueB)
113
                    && count(array_diff($mValueA, $mValueB)) == 0
114
                    && count(array_diff($mValueB, $mValueA)) == 0
115
                );
116
117 4
            case 'successCount':
118 4
            case 'lastSuccess':
119 4
            case 'errorCount':
120 4
            case 'errorsSinceLastSuccess':
121 4
            case 'lastError':
122
                return true;
123
124 4
            default:
125 4
                return ($mValueA == $mValueB);
126 4
        }
127
    }
128
129
    /**
130
     * @param ChronosJobEntity $oJobEntityA
131
     * @param ChronosJobEntity $oJobEntityB
132
     * @return bool
133
     */
134 1
    private function isScheduleTimeZonePropertyIdentical(ChronosJobEntity $oJobEntityA, ChronosJobEntity $oJobEntityB)
135
    {
136 1
        if ($oJobEntityA->scheduleTimeZone == $oJobEntityB->scheduleTimeZone)
137 1
        {
138
            return true;
139
        }
140
141 1
        if (!empty($oJobEntityA->schedule) && !empty($oJobEntityB->schedule))
142 1
        {
143 1
            $_oDateA = $this->createDateTimeObj($oJobEntityA->schedule, $oJobEntityA->scheduleTimeZone);
144 1
            $_oDateB = $this->createDateTimeObj($oJobEntityB->schedule, $oJobEntityB->scheduleTimeZone);
145
146 1
            return ($_oDateA->getOffset() == $_oDateB->getOffset());
147
        }
148
149
        return false;
150
    }
151
152
    /**
153
     * @param ChronosJobEntity $oJobEntityA
154
     * @param ChronosJobEntity $oJobEntityB
155
     * @return bool
156
     */
157 1
    private function isSchedulePropertyIdentical(ChronosJobEntity $oJobEntityA, ChronosJobEntity $oJobEntityB)
158
    {
159
        // if values are exact the same
160 1
        if ($oJobEntityA->schedule === $oJobEntityB->schedule)
161 1
        {
162
            $this->oLogger->debug(sprintf('%s::EXCACT INTERVAL FOR "%s"', 'ScheduleComparison', $oJobEntityA->name));
163
            return true;
164
        }
165
166
        // if one value is empty and not both, compare the time periods
167 1
        if (!empty($oJobEntityA->schedule) && !empty($oJobEntityB->schedule))
168 1
        {
169 1
            $_oIso8601EntityA = $this->oDatePeriodFactory->createIso8601Entity($oJobEntityA->schedule);
170 1
            $_oIso8601EntityB = $this->oDatePeriodFactory->createIso8601Entity($oJobEntityB->schedule);
171
172
            // if the clean interval is different return directly false (P1D != P1M)
173 1
            if ($_oIso8601EntityA->sInterval != $_oIso8601EntityB->sInterval)
174 1
            {
175 1
                $this->oLogger->debug(sprintf('%s::DIFFERENT INTERVAL FOR "%s"', 'ScheduleComparison', $oJobEntityA->name));
176 1
                return false;
177
            }
178
179
            // else if the interval is <= 1Min return directly true (performance)
180 1
            if ($_oIso8601EntityA->sInterval == 'PT1M' || $_oIso8601EntityA->sInterval == 'PT1S')
181 1
            {
182 1
                $this->oLogger->debug(sprintf('%s::PT1M|PT1S INTERVAL FOR "%s" - Job execution should be equal', 'ScheduleComparison', $oJobEntityA->name));
183 1
                return true;
184
            }
185
186
            // start to check by DatePeriods
187 1
            $_oLastDateTimeA = null;
188 1
            $_oLastDateTimeB = null;
189
190
            /** @var \DatePeriod $_oPeriodB */
191 1
            $_oPeriodA = $this->oDatePeriodFactory->createDatePeriod($oJobEntityA->schedule, $oJobEntityA->scheduleTimeZone);
192
193
            /** @var \DateTime $_oDateTime */
194 1
            foreach ($_oPeriodA as $_oDateTime)
195
            {
196 1
                $_oLastDateTimeA = $_oDateTime;
197 1
            }
198
199
            /** @var \DatePeriod $_oPeriodB */
200 1
            $_oPeriodB = $this->oDatePeriodFactory->createDatePeriod($oJobEntityB->schedule, $oJobEntityB->scheduleTimeZone);
201
202
            /** @var \DateTime $_oDateTime */
203 1
            foreach ($_oPeriodB as $_oDateTime)
204
            {
205 1
                $_oLastDateTimeB = $_oDateTime;
206 1
            }
207
208
            // $_oLastDateTimeA !== false happen if no dates are in the period
209 1
            if ($_oLastDateTimeA !== null && $_oLastDateTimeB !== null)
210 1
            {
211 1
                $_oDiffInterval = $_oLastDateTimeA->diff($_oLastDateTimeB);
212 1
                $_iDiffInterval = (int) $_oDiffInterval->format('%Y%M%D%H%I');
213
214 1
                $this->oLogger->debug(sprintf('%s::INTERVAL DIFF OF "%d" FOR "%s"', 'ScheduleComparison', $_iDiffInterval, $oJobEntityA->name));
215 1
                return ($_iDiffInterval == 0);
216
            }
217
        }
218
219
        $this->oLogger->warning(sprintf('%s::CAN\'T COMPARE INTERVAL FOR "%s"', 'ScheduleComparison', $oJobEntityA->name));
220
        return false;
221
    }
222
223
    /**
224
     * @param string $sIso8601String
225
     * @param string $sTimeZone
226
     * @return \DateTime
227
     */
228 1
    private function createDateTimeObj($sIso8601String, $sTimeZone = '')
229
    {
230 1
        $_oIso8601Entity = $this->oDatePeriodFactory->createIso8601Entity($sIso8601String);
231
232 1 View Code Duplication
        if (!empty($sTimeZone))
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
233 1
        {
234 1
            $_oDateTime = new \DateTime(str_replace('Z', '', $_oIso8601Entity->sStartTime));
235 1
            $_oDateTime->setTimezone(new \DateTimeZone($sTimeZone));
236 1
        }
237
        else
238
        {
239 1
            $_oDateTime = new \DateTime($_oIso8601Entity->sStartTime);
240
        }
241
242 1
        return $_oDateTime;
243
    }
244
}