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

createDateTimeObj()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 8

Duplication

Lines 9
Ratio 56.25 %

Code Coverage

Tests 9
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 0
Metric Value
dl 9
loc 16
ccs 9
cts 9
cp 1
rs 9.4285
c 2
b 0
f 0
cc 2
eloc 8
nc 2
nop 2
crap 2
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
        return new ChronosJobEntity();
66
    }
67
68
    /**
69
     * @param JobEntityInterface|ChronosJobEntity $oJobEntityA
70
     * @param JobEntityInterface|ChronosJobEntity $oJobEntityB
71
     * @return bool
72
     */
73 2
    public function hasSameJobType(JobEntityInterface $oJobEntityA, JobEntityInterface $oJobEntityB)
74
    {
75
        return (
76 2
            ($oJobEntityA->isSchedulingJob() && $oJobEntityB->isSchedulingJob())
77 2
            || ($oJobEntityA->isDependencyJob() && $oJobEntityB->isDependencyJob())
78 2
        );
79
    }
80
81
    /**
82
     * @param string $sProperty
83
     * @param JobEntityInterface $oJobEntityA
84
     * @param JobEntityInterface $oJobEntityB
85
     * @return bool
86
     */
87 5
    protected function isEntityEqual($sProperty, JobEntityInterface $oJobEntityA, JobEntityInterface $oJobEntityB)
88
    {
89
        if (
90 5
            !$oJobEntityA instanceof ChronosJobEntity ||
91
            !$oJobEntityB instanceof ChronosJobEntity
92 5
        )
93 5
        {
94
            throw new \RuntimeException('Required ChronosJobEntity. Something else encountered');
95
        }
96
97 5
        $mValueA = $oJobEntityA->{$sProperty};
98 5
        $mValueB = $oJobEntityB->{$sProperty};
99
100
        switch ($sProperty)
101
        {
102 5
            case 'schedule':
103 1
                return $this->isSchedulePropertyIdentical($oJobEntityA, $oJobEntityB);
104
105 5
            case 'scheduleTimeZone':
106 1
                return $this->isScheduleTimeZonePropertyIdentical($oJobEntityA, $oJobEntityB);
107
108 4
            case 'parents':
109
                return (
110
                    is_array($mValueA)
111
                    && is_array($mValueB)
112
                    && count(array_diff($mValueA, $mValueB)) == 0
113
                    && count(array_diff($mValueB, $mValueA)) == 0
114
                );
115
116 4
            case 'successCount':
117 4
            case 'lastSuccess':
118 4
            case 'errorCount':
119 4
            case 'errorsSinceLastSuccess':
120 4
            case 'lastError':
121
                return true;
122
123 4
            default:
124 4
                return ($mValueA == $mValueB);
125 4
        }
126
    }
127
128
    /**
129
     * @param ChronosJobEntity $oJobEntityA
130
     * @param ChronosJobEntity $oJobEntityB
131
     * @return bool
132
     */
133 1
    private function isScheduleTimeZonePropertyIdentical(ChronosJobEntity $oJobEntityA, ChronosJobEntity $oJobEntityB)
134
    {
135 1
        if ($oJobEntityA->scheduleTimeZone == $oJobEntityB->scheduleTimeZone)
136 1
        {
137
            return true;
138
        }
139
140 1
        if (!empty($oJobEntityA->schedule) && !empty($oJobEntityB->schedule))
141 1
        {
142 1
            $_oDateA = $this->createDateTimeObj($oJobEntityA->schedule, $oJobEntityA->scheduleTimeZone);
143 1
            $_oDateB = $this->createDateTimeObj($oJobEntityB->schedule, $oJobEntityB->scheduleTimeZone);
144
145 1
            return ($_oDateA->getOffset() == $_oDateB->getOffset());
146
        }
147
148
        return false;
149
    }
150
151
    /**
152
     * @param ChronosJobEntity $oJobEntityA
153
     * @param ChronosJobEntity $oJobEntityB
154
     * @return bool
155
     */
156 1
    private function isSchedulePropertyIdentical(ChronosJobEntity $oJobEntityA, ChronosJobEntity $oJobEntityB)
157
    {
158
        // if values are exact the same
159 1
        if ($oJobEntityA->schedule === $oJobEntityB->schedule)
160 1
        {
161
            $this->oLogger->debug(sprintf('%s::EXCACT INTERVAL FOR "%s"', 'ScheduleComparison', $oJobEntityA->name));
162
            return true;
163
        }
164
165
        // if one value is empty and not both, compare the time periods
166 1
        if (!empty($oJobEntityA->schedule) && !empty($oJobEntityB->schedule))
167 1
        {
168 1
            $_oIso8601EntityA = $this->oDatePeriodFactory->createIso8601Entity($oJobEntityA->schedule);
169 1
            $_oIso8601EntityB = $this->oDatePeriodFactory->createIso8601Entity($oJobEntityB->schedule);
170
171
            // if the clean interval is different return directly false (P1D != P1M)
172 1
            if ($_oIso8601EntityA->sInterval != $_oIso8601EntityB->sInterval)
173 1
            {
174 1
                $this->oLogger->debug(sprintf('%s::DIFFERENT INTERVAL FOR "%s"', 'ScheduleComparison', $oJobEntityA->name));
175 1
                return false;
176
            }
177
178
            // else if the interval is <= 1Min return directly true (performance)
179 1
            if ($_oIso8601EntityA->sInterval == 'PT1M' || $_oIso8601EntityA->sInterval == 'PT1S')
180 1
            {
181 1
                $this->oLogger->debug(sprintf('%s::PT1M|PT1S INTERVAL FOR "%s" - Job execution should be equal', 'ScheduleComparison', $oJobEntityA->name));
182 1
                return true;
183
            }
184
185
            // start to check by DatePeriods
186 1
            $_oLastDateTimeA = null;
187 1
            $_oLastDateTimeB = null;
188
189
            /** @var \DatePeriod $_oPeriodB */
190 1
            $_oPeriodA = $this->oDatePeriodFactory->createDatePeriod($oJobEntityA->schedule, $oJobEntityA->scheduleTimeZone);
191
192
            /** @var \DateTime $_oDateTime */
193 1
            foreach ($_oPeriodA as $_oDateTime) {
194 1
                $_oLastDateTimeA = $_oDateTime;
195 1
            }
196
197
            /** @var \DatePeriod $_oPeriodB */
198 1
            $_oPeriodB = $this->oDatePeriodFactory->createDatePeriod($oJobEntityB->schedule, $oJobEntityB->scheduleTimeZone);
199
200
            /** @var \DateTime $_oDateTime */
201 1
            foreach ($_oPeriodB as $_oDateTime) {
202 1
                $_oLastDateTimeB = $_oDateTime;
203 1
            }
204
205
            // $_oLastDateTimeA !== false happen if no dates are in the period
206 1
            if ($_oLastDateTimeA !== null && $_oLastDateTimeB !== null)
207 1
            {
208 1
                $_oDiffInterval = $_oLastDateTimeA->diff($_oLastDateTimeB);
209 1
                $_iDiffInterval = (int) $_oDiffInterval->format('%Y%M%D%H%I');
210
211 1
                $this->oLogger->debug(sprintf('%s::INTERVAL DIFF OF "%d" FOR "%s"', 'ScheduleComparison', $_iDiffInterval, $oJobEntityA->name));
212 1
                return ($_iDiffInterval == 0);
213
            }
214
        }
215
216
        $this->oLogger->warning(sprintf('%s::CAN\'T COMPARE INTERVAL FOR "%s"', 'ScheduleComparison', $oJobEntityA->name));
217
        return false;
218
    }
219
220
    /**
221
     * @param string $sIso8601String
222
     * @param string $sTimeZone
223
     * @return \DateTime
224
     */
225 1
    private function createDateTimeObj($sIso8601String, $sTimeZone = '')
226
    {
227 1
        $_oIso8601Entity = $this->oDatePeriodFactory->createIso8601Entity($sIso8601String);
228
229 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...
230 1
        {
231 1
            $_oDateTime = new \DateTime(str_replace('Z', '', $_oIso8601Entity->sStartTime));
232 1
            $_oDateTime->setTimezone(new \DateTimeZone($sTimeZone));
233 1
        }
234
        else
235
        {
236 1
            $_oDateTime = new \DateTime($_oIso8601Entity->sStartTime);
237
        }
238
239 1
        return $_oDateTime;
240
    }
241
}