Worker   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 128
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Test Coverage

Coverage 93.75%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 9
c 1
b 0
f 0
lcom 0
cbo 5
dl 0
loc 128
ccs 30
cts 32
cp 0.9375
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
B analiseGateway() 0 28 5
A buildResultDataDefault() 0 13 1
A doJob() 0 20 3
1
<?php
2
3
namespace Messenger\Sms;
4
5
use Cronario\AbstractJob;
6
use Cronario\AbstractWorker;
7
use Cronario\Exception\WorkerException;
8
9
class Worker extends AbstractWorker
10
{
11
12
    //region Config ***********************************************************
13
14
    protected static $config
15
        = [
16
            AbstractWorker::CONFIG_P_MANAGERS_LIMIT         => 3,
17
            AbstractWorker::CONFIG_P_MANAGER_POOL_SIZE      => 5,
18
            AbstractWorker::CONFIG_P_MANAGER_IDLE_DIE_DELAY => 5,
19
            self::GATEWAY_CLASS_SET                         => [
20
                '\Messenger\Sms\Alpha\Worker' => [
21
                    self::GATEWAY_CLASS_P_REGEX_RECIPIENT => '/^(38)/'
22
                ],
23
                '\Messenger\Sms\Test\Worker' => [
24
                    self::GATEWAY_CLASS_P_REGEX_RECIPIENT => '/^(38)/'
25
                ],
26
            ],
27
        ];
28
29
    //endregion ****************************************************************
30
31
    //region GATEWAY ***********************************************************
32
33
    const GATEWAY_DISPATCH_CLASS = 'gatewayDispatchClass';
34
    const GATEWAY_CLASS_SET = 'gatewayClassSet';
35
    const GATEWAY_CLASS_P_REGEX_RECIPIENT = 'regexRecipient';
36
37
    /**
38
     * @param Job $job
39
     *
40
     * @return array
41
     * @throws WorkerException
42
     */
43 3
    protected function analiseGateway(Job $job)
44
    {
45 3
        $gatewayClassSet = self::getConfig(self::GATEWAY_CLASS_SET);
46
47 3
        if (count($gatewayClassSet) === 0) {
48
            throw new WorkerException('Empty gateway class set!');
49
        }
50
51 3
        $gatewayClassScore = [];
52 3
        foreach ($gatewayClassSet as $gatewayClass => $gatewayOptions) {
53 3
            $gatewayClassScore[$gatewayClass] = 0;
54
55 3
            if (preg_match($gatewayOptions[self::GATEWAY_CLASS_P_REGEX_RECIPIENT],
56 3
                $job->getRecipient())) {
57 3
                $gatewayClassScore[$gatewayClass]++;
58 3
            }
59
60
            // ......
61 3
        }
62
63 3
        $maxClass = array_keys($gatewayClassScore, max($gatewayClassScore))[0];
64
65 3
        if ($gatewayClassScore[$maxClass] === 0) {
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
66
            // Transport is bad : $maxClass = 0 , and we have no ideas what return :(
67
        }
68
69 3
        return $maxClass;
70
    }
71
72
    //endregion ***************************************************************
73
74
    // region Default Result Data**************************************************************
75
76
    const P_RESULT_DATA_VENDOR_NAME = 'vendor';
77
    const P_RESULT_DATA_VENDOR_ID = 'vendor_id';
78
    const P_RESULT_DATA_SUCCESS = 'success';
79
    const P_RESULT_DATA_ERRORS = 'errors';
80
81
    /**
82
     * @param null $vendor_name
83
     * @param null $vendor_id
84
     * @param null $success
85
     * @param null $errors
86
     *
87
     * @return array
88
     */
89 5
    public static function buildResultDataDefault(
90
        $vendor_name = null,
91
        $vendor_id = null,
92
        $success = null,
93
        $errors = null
94
    ) {
95
        return [
96 5
            self::P_RESULT_DATA_VENDOR_NAME => $vendor_name,
97 5
            self::P_RESULT_DATA_VENDOR_ID   => $vendor_id,
98 5
            self::P_RESULT_DATA_SUCCESS     => $success,
99 5
            self::P_RESULT_DATA_ERRORS      => $errors,
100 5
        ];
101
    }
102
103
    //endregion ***************************************************************
104
105
    /**
106
     * if job is sync then we get worker and try execute job
107
     * else if job is Async then redirect to other queue
108
     *
109
     * REMEMBER: Queue name is equal to Worker class name (through all "Cronario")
110
     *
111
     * @param AbstractJob|Job $job
112
     *
113
     * @throws ResultException
114
     * @throws WorkerException
115
     */
116 3
    protected function doJob(AbstractJob $job)
117
    {
118 3
        $gatewayClass = $this->analiseGateway($job);
0 ignored issues
show
Compatibility introduced by
$job of type object<Cronario\AbstractJob> is not a sub-type of object<Messenger\Sms\Job>. It seems like you assume a child class of the class Cronario\AbstractJob to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
119
120 3
        if ($job->isSync()) {
121 1
            $worker = AbstractWorker::factory($gatewayClass);
122
123 1
            return $worker($job);
124
        }
125
126 2
        if (!$job->hasAttempt()) {
127 1
            throw new ResultException(ResultException::FAILURE_MAX_ATTEMPTS);
128
        }
129
130
        // redirect result for new gateway class
131 1
        $job->addDebug(['set_gateway' => $gatewayClass]);
132 1
        $job->setWorkerClass($gatewayClass)->save();
133
134 1
        throw new ResultException(ResultException::REDIRECT_GATEWAY_CLASS);
135
    }
136
}