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 (#158)
by Bernardo Vieira da
12:24
created

JobClass::loadMinimumExecutionTime()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 2
crap 2
1
<?php
2
3
/**
4
 * Gearman Bundle for Symfony2
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 *
9
 * Feel free to edit as you please, and have fun.
10
 *
11
 * @author Marc Morera <[email protected]>
12
 */
13
14
namespace Mmoreram\GearmanBundle\Module;
15
16
use ReflectionMethod;
17
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
18
use Symfony\Component\DependencyInjection\ContainerInterface;
19
20
use Mmoreram\GearmanBundle\Driver\Gearman\Job as JobAnnotation;
21
22
/**
23
 * Job class
24
 *
25
 * This class provide all worker definition.
26
 *
27
 * @since 2.3.1
28
 */
29
class JobClass implements ContainerAwareInterface
30
{
31
    /**
32
     * @var string
33
     *
34
     * Default description when is not defined
35
     */
36
    const DEFAULT_DESCRIPTION = 'No description is defined';
37
38
    /**
39
     * @var string
40
     *
41
     * Callable name for this job
42
     * If is setted on annotations, this value will be used
43
     *  otherwise, natural method name will be used.
44
     */
45
    private $callableName;
46
47
    /**
48
     * @var string
49
     *
50
     * Method name
51
     */
52
    private $methodName;
53
54
    /**
55
     * @var string
56
     *
57
     * RealCallable name for this job without the job prefix
58
     *
59
     */
60
    private $realCallableNameNoPrefix;
61
62
    /**
63
     * @var string
64
     *
65
     * RealCallable name for this job
66
     * natural method name will be used.
67
     */
68
    private $realCallableName;
69
70
    /**
71
     * @var string
72
     *
73
     * Description of Job
74
     */
75
    private $description;
76
77
    /**
78
     * @var integer
79
     *
80
     * Number of iterations this job will be alive before die
81
     */
82
    private $iterations;
83
84
    /**
85
     * @var string
86
     *
87
     * Default method this job will be call into Gearman client
88
     */
89
    private $defaultMethod;
90
91
    /**
92
     * @var int
93
     *
94
     * Job minimum execution time
95
     */
96
    private $minimumExecutionTime;
97
98
    /**
99
     * @var int
100
     *
101
     * Timeout for idle worker
102
     */
103
    private $timeout;
104
105
    /**
106
     * @var array
107
     *
108
     * Collection of servers to connect
109
     */
110
    private $servers;
111
112
    /**
113
     * @var string
114
     *
115
     * The prefix to be prepended to all job callable names.
116
     */
117
    private $jobPrefix;
118
119
    /**
120
     * @var ContainerInterface
121
     */
122
    protected $container;
123
124
    /**
125
     * Construct method
126
     *
127
     * @param JobAnnotation    $jobAnnotation     JobAnnotation class
128
     * @param ReflectionMethod $reflectionMethod  ReflextionMethod class
129
     * @param string           $callableNameClass Callable name class
130
     * @param array            $servers           Array of servers defined for Worker
131
     * @param array            $defaultSettings   Default settings for Worker
132
     */
133 4
    public function __construct(
134
        JobAnnotation $jobAnnotation,
135
        ReflectionMethod $reflectionMethod,
136
        $callableNameClass,
137
        array $servers,
138
        array $defaultSettings
139
    )
140
    {
141
142 4
        $this->callableName = is_null($jobAnnotation->name)
143 3
            ? $reflectionMethod->getName()
144 1
            : $jobAnnotation->name;
145
146 4
        $this->methodName = $reflectionMethod->getName();
147 4
        $this->realCallableNameNoPrefix = str_replace('\\', '', $callableNameClass . '~' . $this->callableName);
148
149 4
        $this->jobPrefix = isset($defaultSettings['jobPrefix'])
150 1
            ? $defaultSettings['jobPrefix']
151 3
            : null;
152
153 4
        $this->realCallableName = $this->jobPrefix . $this->realCallableNameNoPrefix;
154 4
        $this->description = is_null($jobAnnotation->description)
155 3
            ? self::DEFAULT_DESCRIPTION
156 1
            : $jobAnnotation->description;
157
158 4
        $this->servers = $this->loadServers($jobAnnotation, $servers);
159 4
        $this->iterations = $this->loadIterations($jobAnnotation, $defaultSettings);
160 4
        $this->minimumExecutionTime = $this->loadMinimumExecutionTime($jobAnnotation, $defaultSettings);
161 4
        $this->timeout = $this->loadTimeout($jobAnnotation, $defaultSettings);
162 4
        $this->defaultMethod = $this->loadDefaultMethod($jobAnnotation, $defaultSettings);
163 4
    }
164
165
    /**
166
     * Load servers
167
     *
168
     * If any server is defined in JobAnnotation, this one is used.
169
     * Otherwise is used servers set in Class
170
     *
171
     * @param JobAnnotation $jobAnnotation JobAnnotation class
172
     * @param array         $servers       Array of servers defined for Worker
173
     *
174
     * @return array Servers
175
     */
176 4
    private function loadServers(JobAnnotation $jobAnnotation, array $servers)
177
    {
178
179
        /**
180
         * If is configured some servers definition in the worker, overwrites
181
         */
182 4
        if ($jobAnnotation->servers) {
183
184 3
            $servers = (is_array($jobAnnotation->servers) && !isset($jobAnnotation->servers['host']))
185 1
                ? $jobAnnotation->servers
186 3
                : array($jobAnnotation->servers);
187
        }
188
189 4
        return $servers;
190
    }
191
192
    /**
193
     * Load iterations
194
     *
195
     * If iterations is defined in JobAnnotation, this one is used.
196
     * Otherwise is used set in Class
197
     *
198
     * @param JobAnnotation $jobAnnotation   JobAnnotation class
199
     * @param array         $defaultSettings Default settings for Worker
200
     *
201
     * @return integer Iteration
202
     */
203 4
    private function loadIterations(JobAnnotation $jobAnnotation, array $defaultSettings)
204
    {
205 4
        return is_null($jobAnnotation->iterations)
206 3
            ? (int) $defaultSettings['iterations']
207 4
            : (int) $jobAnnotation->iterations;
208
    }
209
210
    /**
211
     * Load defaultMethod
212
     *
213
     * If defaultMethod is defined in JobAnnotation, this one is used.
214
     * Otherwise is used set in Class
215
     *
216
     * @param JobAnnotation $jobAnnotation   JobAnnotation class
217
     * @param array         $defaultSettings Default settings for Worker
218
     *
219
     * @return string Default method
220
     */
221 4
    private function loadDefaultMethod(JobAnnotation $jobAnnotation, array $defaultSettings)
222
    {
223 4
        return is_null($jobAnnotation->defaultMethod)
224 3
            ? $defaultSettings['method']
225 4
            : $jobAnnotation->defaultMethod;
226
    }
227
228
    /**
229
     * Load minimumExecutionTime
230
     *
231
     * If minimumExecutionTime is defined in JobAnnotation, this one is used.
232
     * Otherwise is used set in Class
233
     *
234
     * @param JobAnnotation $jobAnnotation
235
     * @param array $defaultSettings
236
     *
237
     * @return int
238
     */
239 4
    private function loadMinimumExecutionTime(JobAnnotation $jobAnnotation, array $defaultSettings)
240
    {
241 4
        return is_null($jobAnnotation->minimumExecutionTime)
242 4
            ? (int) $defaultSettings['minimumExecutionTime']
243 4
            : (int) $jobAnnotation->minimumExecutionTime;
244
    }
245
246
    /**
247
     * Load timeout
248
     *
249
     * If timeout is defined in JobAnnotation, this one is used.
250
     * Otherwise is used set in Class
251
     *
252
     * @param JobAnnotation $jobAnnotation
253
     * @param array $defaultSettings
254
     *
255
     * @return int
256
     */
257 4
    private function loadTimeout(JobAnnotation $jobAnnotation, array $defaultSettings)
258
    {
259 4
        return is_null($jobAnnotation->timeout)
260 4
            ? (int) $defaultSettings['timeout']
261 4
            : (int) $jobAnnotation->timeout;
262
    }
263
264
    /**
265
     * Retrieve all Job data in cache format
266
     *
267
     * @return array
268
     */
269 4
    public function toArray()
270
    {
271
        return array(
272
273 4
            'callableName'             => $this->callableName,
274 4
            'methodName'               => $this->methodName,
275 4
            'realCallableName'         => $this->realCallableName,
276 4
            'jobPrefix'                => $this->jobPrefix,
277 4
            'realCallableNameNoPrefix' => $this->realCallableNameNoPrefix,
278 4
            'description'              => $this->description,
279 4
            'iterations'               => $this->iterations,
280 4
            'minimumExecutionTime'     => $this->minimumExecutionTime,
281 4
            'timeout'                  => $this->timeout,
282 4
            'servers'                  => $this->servers,
283 4
            'defaultMethod'            => $this->defaultMethod,
284
        );
285
    }
286
287
    /**
288
     * Sets the container.
289
     *
290
     * @param ContainerInterface|null $container A ContainerInterface instance or null
291
     */
292
    public function setContainer(ContainerInterface $container = null)
293
    {
294
        $this->container = $container;
295
    }
296
}
297