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
Branch feature/add_container_job_supp... (565db8)
by Marc
04:48
created

isContainerPropertyValid()   D

Complexity

Conditions 9
Paths 7

Size

Total Lines 33
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 9.0117

Importance

Changes 0
Metric Value
dl 0
loc 33
ccs 18
cts 19
cp 0.9474
rs 4.909
c 0
b 0
f 0
cc 9
eloc 13
nc 7
nop 1
crap 9.0117
1
<?php
2
/**
3
 * @package: chapi
4
 *
5
 * @author:  msiebeneicher
6
 * @since:   2015-07-31
7
 *
8
 */
9
10
namespace Chapi\Service\JobRepository;
11
12
use Chapi\Component\DatePeriod\DatePeriodFactoryInterface;
13
use Chapi\Entity\Chronos\JobEntity;
14
use Chapi\Exception\DatePeriodException;
15
16
class JobEntityValidatorService implements JobEntityValidatorServiceInterface
17
{
18
    const REG_EX_VALID_NAME = '/^[a-zA-Z0-9_-]*$/';
19
20
    /**
21
     * @var DatePeriodFactoryInterface
22
     */
23
    private $oDatePeriodFactory;
24
25
    /**
26
     * @param DatePeriodFactoryInterface $oDatePeriodFactory
27
     */
28 12
    public function __construct(
29
        DatePeriodFactoryInterface $oDatePeriodFactory
30
    )
31
    {
32 12
        $this->oDatePeriodFactory = $oDatePeriodFactory;
33 12
    }
34
35
    /**
36
     * @param JobEntity $oJobEntity
37
     * @return bool
38
     */
39 12
    public function isEntityValid(JobEntity $oJobEntity)
40
    {
41 12
        return (!in_array(false, $this->validateJobEntity($oJobEntity)));
42
    }
43
44
    /**
45
     * @param JobEntity $oJobEntity
46
     * @return array
47
     */
48 12
    public function validateJobEntity(JobEntity $oJobEntity)
49
    {
50 12
        $_aValidProperties = [];
51
52 12
        foreach ($oJobEntity as $_sProperty => $mValue)
53
        {
54
            switch ($_sProperty)
55
            {
56 12
                case 'name':
57 12
                    $_aValidProperties[$_sProperty] = $this->isNamePropertyValid($mValue);
58 12
                    break;
59
60 12
                case 'command':
61 12
                case 'description':
62 12
                case 'owner':
63 12
                case 'ownerName':
64 12
                    $_aValidProperties[$_sProperty] = (!empty($oJobEntity->{$_sProperty}));
65 12
                    break;
66
67 12
                case 'epsilon':
68 12
                    $_aValidProperties[$_sProperty] = $this->isEpsilonPropertyValid($oJobEntity);
69 12
                    break;
70
71 12
                case 'async':
72 12
                case 'disabled':
73 12
                case 'softError':
74 12
                case 'highPriority':
75 12
                    $_aValidProperties[$_sProperty] = (is_bool($oJobEntity->{$_sProperty}));
76 12
                    break;
77
78 12
                case 'schedule':
79 12
                    $_aValidProperties[$_sProperty] = $this->isSchedulePropertyValid($oJobEntity);
80 12
                    break;
81
82 12
                case 'parents':
83 12
                    $_aValidProperties[$_sProperty] = (is_array($oJobEntity->{$_sProperty}));
84 12
                    break;
85
86 12
                case 'retries':
87 12
                    $_aValidProperties[$_sProperty] = ($oJobEntity->{$_sProperty} >= 0);
88 12
                    break;
89
90 12
                case 'constraints':
91 12
                    $_aValidProperties[$_sProperty] = $this->isConstraintsPropertyValid($mValue);
92 12
                    break;
93
94 12
                case 'container':
95 12
                    $_aValidProperties[$_sProperty] = $this->isContainerPropertyValid($mValue);
96 12
                    break;
97
            }
98 12
        }
99
100 12
        return $_aValidProperties;
101
    }
102
103
    /**
104
     * @param JobEntity $oJobEntity
105
     * @return array
106
     */
107
    public function getInvalidProperties(JobEntity $oJobEntity)
108
    {
109
        $_aValidationFields = $this->validateJobEntity($oJobEntity);
110
111
        $_aInvalidFields = [];
112
        foreach ($_aValidationFields as $_sProperty => $_bIsValid)
113
        {
114
            if (false == $_bIsValid)
115
            {
116
                $_aInvalidFields[] = $_sProperty;
117
            }
118
        }
119
120
        return $_aInvalidFields;
121
    }
122
123
    /**
124
     * @param string $sName
125
     * @return bool
126
     */
127 12
    private function isNamePropertyValid($sName)
128
    {
129 12
        return (!empty($sName) && preg_match(self::REG_EX_VALID_NAME, $sName));
130
    }
131
132
    /**
133
     * @param JobEntity $oJobEntity
134
     * @return bool
135
     */
136 12
    private function isSchedulePropertyValid(JobEntity $oJobEntity)
137
    {
138 12
        if (empty($oJobEntity->schedule) && !empty($oJobEntity->parents))
139 12
        {
140 1
            return true;
141
        }
142
143 11
        if (!empty($oJobEntity->schedule) && empty($oJobEntity->parents))
144 11
        {
145
            try
146
            {
147 10
                $_oDataPeriod = $this->oDatePeriodFactory->createDatePeriod($oJobEntity->schedule, $oJobEntity->scheduleTimeZone);
148 10
                return (false !== $_oDataPeriod);
149
            }
150 1
            catch (DatePeriodException $oException)
151
            {
152
                // invalid: Iso8601 is not valid and/or DatePeriodFactory is able to create a valid DatePeriod
153
            }
154 1
        }
155
156 2
        return false;
157
    }
158
159
    /**
160
     * @param JobEntity $oJobEntity
161
     * @return bool
162
     */
163 12
    private function isEpsilonPropertyValid(JobEntity $oJobEntity)
164
    {
165 12
        if ($oJobEntity->isSchedulingJob() && !empty($oJobEntity->epsilon))
166 12
        {
167
            try
168
            {
169 10
                $_oDateIntervalEpsilon = new \DateInterval($oJobEntity->epsilon);
170 9
                $_iIntervalEpsilon = (int) $_oDateIntervalEpsilon->format('%Y%M%D%H%I%S');
171
                
172 9
                if ($_iIntervalEpsilon > 30) // if epsilon > "PT30S"
173 9
                {
174 8
                    $_oIso8601Entity = $this->oDatePeriodFactory->createIso8601Entity($oJobEntity->schedule);
175
176 8
                    $_oDateIntervalScheduling = new \DateInterval($_oIso8601Entity->sInterval);
177 8
                    $_iIntervalScheduling = (int) $_oDateIntervalScheduling->format('%Y%M%D%H%I%S');
178
                    
179 8
                    return ($_iIntervalScheduling > $_iIntervalEpsilon);
180
                }
181
182
                // if epsilon is less or equal than 30sec the not empty check is enough
183 1
                return true;
184
            }
185 1
            catch (\Exception $_oException)
186
            {
187
                // can't init \DateInterval instance
188 1
                return false;
189
            }
190
        }
191
192
        // else
193 3
        return (!empty($oJobEntity->epsilon));
194
    }
195
196
    /**
197
     * @param array $aConstraints
198
     * @return bool
199
     */
200 12
    private function isConstraintsPropertyValid(array $aConstraints)
201
    {
202 12
        if (!empty($aConstraints))
203 12
        {
204 1
            foreach ($aConstraints as $_aConstraint)
205
            {
206 1
                if (!is_array($_aConstraint) || count($_aConstraint) != 3)
207 1
                {
208 1
                    return false;
209
                }
210 1
            }
211 1
        }
212
213 12
        return true;
214
    }
215
216
    /**
217
     * @param JobEntity\ContainerEntity $oContainer
218
     * @return bool
219
     *
220
     * @see http://mesos.github.io/chronos/docs/api.html#adding-a-docker-job
221
     * This contains the subfields for the Docker container:
222
     *  type (required), image (required), forcePullImage (optional), network (optional),
223
     *  and volumes (optional)
224
     */
225 12
    private function isContainerPropertyValid($oContainer)
226
    {
227 12
        if (is_null($oContainer))
228 12
        {
229 10
            return true;
230
        }
231
232 2
        if (is_object($oContainer))
233 2
        {
234 2
            if (empty($oContainer->type) || empty($oContainer->image))
235 2
            {
236 1
                return false;
237
            }
238
            
239 2
            if (!is_null($oContainer->volumes) && !is_array($oContainer->volumes))
240 2
            {
241
                return false;
242
            }
243
244 2
            foreach ($oContainer->volumes as $_oVolume)
0 ignored issues
show
Bug introduced by
The expression $oContainer->volumes of type null|array is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
245
            {
246
                /** @var JobEntity\ContainerVolumeEntity $_oVolume  */
247 2
                if (!in_array($_oVolume->mode, ['RO', 'RW']))
248 2
                {
249 1
                    return false;
250
                }
251 2
            }
252
253 2
            return true;
254
        }
255
        
256 1
        return false;
257
    }
258
}