Worker::isContain()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 4.8438

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 16
ccs 5
cts 8
cp 0.625
rs 9.2
cc 4
eloc 8
nc 4
nop 2
crap 4.8438
1
<?php
2
3
namespace Messenger\Curl;
4
5
use Cronario\AbstractJob;
6
use Cronario\AbstractWorker;
7
use Cronario\Logger;
8
use Messenger\CurlWrapper;
9
use Messenger\CurlWrapperCurlException;
10
use Messenger\CurlWrapperException;
11
12
/**
13
 * Class Worker
14
 *
15
 * @package Messenger\Curl
16
 */
17
class Worker extends AbstractWorker
18
{
19
20
21
    // region HELPERS ********************************************************
22
23
    /**
24
     * @param $what
25
     * @param $where
26
     *
27
     * @return bool
28
     */
29 1
    protected function isContain($what, $where)
30
    {
31 1
        if (empty($what)) {
32
            return true;
33
        }
34
35 1
        if (empty($where)) {
36
            return false;
37
        }
38
39 1
        if (strpos((string) $where, (string) $what) !== false) {
40
            return true;
41
        }
42
43 1
        return false;
44
    }
45
46
    // endregion *************************************************************
47
48
    /**
49
     * @param AbstractJob $job
50
     *
51
     * @throws ResultException
52
     */
53 5
    protected function doJob(AbstractJob $job)
54
    {
55
        /** @var $job Job */
56 5
        $resultData = null;
57
58
        try {
59 5
            $curl = new CurlWrapper();
60 5
            $curl->request($job->getUrl(), $job->getMethod(), $job->getRequestParams());
0 ignored issues
show
Bug introduced by
It seems like $job->getRequestParams() targeting Messenger\Curl\Job::getRequestParams() can also be of type integer or string; however, CurlWrapper::request() does only seem to accept array|null, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
61 3
            $response['content'] = $curl->getResponse();
0 ignored issues
show
Coding Style Comprehensibility introduced by
$response was never initialized. Although not strictly required by PHP, it is generally a good practice to add $response = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
62 3
            $response['info'] = $curl->getTransferInfo();
63
64 5
        } catch (CurlWrapperCurlException $a) {
65
            $job->addDebug(['CurlWrapperCurlException' => $a->getMessage()]);
66
            throw new ResultException(ResultException::ERROR_CURL);
67 2
        } catch (CurlWrapperException $a) {
68
            $job->addDebug(['CurlWrapperCurlException' => $a->getMessage()]);
69
            throw new ResultException(ResultException::ERROR_CURL);
70 2
        } catch (\Exception $a) {
71 2
            $job->addDebug(['CurlWrapperCurlException' => $a->getMessage()]);
72 2
            throw new ResultException(ResultException::ERROR_CURL);
73
        }
74
75
        /**
76
         * saving data
77
         */
78 3
        if ($job->getSaveContent()) {
79 1
            $resultData['content'] = $response['content'];
80 1
        }
81
82 3
        if ($job->getSaveInfo()) {
83 1
            $resultData['info'] = $response['info'];
84 1
        }
85
86
        /**
87
         * analise response
88
         */
89 3
        if ($job->getExpectCode() && $job->getExpectCode() != $response['info']['http_code']) {
90 1
            throw new ResultException(ResultException::RETRY_EXPECTED_HTTP_CODE, $resultData);
91
92 2
        } elseif ($job->getExpectContent() && !$this->isContain($job->getExpectContent(), $response['content'])) {
93 1
            throw new ResultException(ResultException::RETRY_EXPECTED_CONTENT, $resultData);
94
        }
95
96
        /**
97
         * success result
98
         */
99 1
        throw new ResultException(ResultException::R_SUCCESS, $resultData);
100
    }
101
102
}