JobOptionsExtractor   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
wmc 3
lcom 0
cbo 2
dl 0
loc 44
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A extractOptions() 0 9 1
A hydrateOptions() 0 20 2
1
<?php
2
namespace Workana\AsyncJobs\Normalizer;
3
4
use InvalidArgumentException;
5
use Workana\AsyncJobs\Job;
6
7
/**
8
 * Extracts and hydrates options of base Job, to be used on normalizers
9
 *
10
 * @author Carlos Frutos <[email protected]>
11
 */
12
trait JobOptionsExtractor
13
{
14
    /**
15
     * @param Job $job
16
     *
17
     * @return array Options
18
     */
19
    public function extractOptions(Job $job)
20
    {
21
        return [
22
            'delay' => $job->getDelay(),
23
            'retries' => $job->getRetries(),
24
            'maxRetries' => $job->getMaxRetries(),
25
            'preferredQueueName' => $job->getPreferredQueueName(),
26
        ];
27
    }
28
29
    /**
30
     * @param Job $job
31
     * @param array $options
32
     *
33
     * @return void
34
     */
35
    public function hydrateOptions(Job $job, array $options)
36
    {
37
        $missingOptions = array_diff([
38
            'delay',
39
            'retries',
40
            'maxRetries',
41
            'preferredQueueName'
42
        ], array_keys($options));
43
44
        if (!empty($missingOptions)) {
45
            throw new InvalidArgumentException(strtr('The following options are missing: :missingOptions', [
46
                ':missingOptions' => implode(', ', $missingOptions)
47
            ]));
48
        }
49
50
        $job->withDelay($options['delay']);
51
        Accesor::set($job, 'retries', $options['retries']);
52
        $job->withMaxRetries($options['maxRetries']);
53
        $job->withPreferredQueueName($options['preferredQueueName']);
54
    }
55
}