Completed
Pull Request — master (#104)
by
unknown
03:28
created

Http::post()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 23
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 23
c 0
b 0
f 0
rs 9.0856
ccs 0
cts 14
cp 0
cc 2
eloc 14
nc 2
nop 5
crap 6
1
<?php
2
3
namespace eXpansion\Framework\Core\Helpers;
4
5
use eXpansion\Framework\Core\Helpers\JobRunner\Factory;
6
use eXpansion\Framework\Core\Helpers\Structures\HttpRequest;
7
use eXpansion\Framework\Core\Helpers\Structures\HttpResult;
8
use eXpansion\Framework\Core\Services\Application\AbstractApplication;
9
use oliverde8\AsynchronousJobs\Job\CallbackCurl;
10
use oliverde8\AsynchronousJobs\JobRunner;
11
12
/**
13
 * Class Http
14
 *
15
 * @author    de Cramer Oliver<[email protected]>
16
 * @copyright 2017 Smile
17
 * @package eXpansion\Framework\Core\Helpers
18
 */
19
class Http
20
{
21
22
    /** @var Factory */
23
    protected $factory;
24
25
    /**
26
     * Http constructor.
27
     *
28
     * @param Factory $factory
29
     */
30 1
    public function __construct(Factory $factory)
31
    {
32 1
        $this->factory = $factory;
33 1
    }
34
35
    /**
36
     * Make a http query.
37
     *
38
     * @param string $url
39
     * @param callable $callback
40
     * @param null|mixed $additionalData If you need to pass additional metadata.
41
     *                                   You will get this back in the callback.
42
     * @param array $options curl options array
43
     */
44 1
    public function call($url, $callback, $additionalData = null, $options = [])
45
    {
46 1
        $curlJob = $this->factory->createCurlJob($url, $callback, $additionalData, $options);
47
48
        // Start job execution.
49 1
        $this->factory->startJob($curlJob);
50 1
    }
51
52
    /**
53
     * Make a get http query.
54
     *
55
     * @param string $url address
56
     * @param callable $callback callback
57
     * @param null|mixed $additionalData If you need to pass additional metadata.
58
     *                                          You will get this back in the callback.
59
     * @param array $options Single dimensional array of curl_setopt key->values
60
     */
61
    public function get($url, callable $callback, $additionalData = null, $options = [])
62
    {
63
        $options[CURLOPT_FOLLOWLOCATION] = true;
64
        $options[CURLOPT_USERAGENT] = "eXpansionPluginPack v ".AbstractApplication::EXPANSION_VERSION;
65
66
        $additionalData['callback'] = $callback;
67
68
        $this->call($url, [$this, 'process'], $additionalData, $options);
69
    }
70
71
    /**
72
     * Make a post http query.
73
     *
74
     * @param string $url address
75
     * @param string|array $postFields
76
     * @param callable $callback callback with returning datas
77
     * @param null|mixed $additionalData If you need to pass additional metadata.
78
     *                                   You will get this back in the callback.
79
     * @param array $options Single dimensional array of curl_setopt key->values
80
     */
81
    public function post($url, $postFields, callable $callback, $additionalData = null, $options = [])
82
    {
83
        $options[CURLOPT_POST] = true;
84
        $options[CURLOPT_FOLLOWLOCATION] = true;
85
        $options[CURLOPT_USERAGENT] = "eXpansionPluginPack v ".AbstractApplication::EXPANSION_VERSION;
86
87
        $query = '';
0 ignored issues
show
Unused Code introduced by
$query is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
88
        if (is_array($postFields)) {
89
            $query = http_build_query($postFields, '', '&');
90
        } else {
91
            $query = $postFields;
92
        }
93
94
        $options[CURLOPT_URL] = $url;
95
        $options[CURLOPT_POSTFIELDS] = $query;
96
97
        $additionalData['callback'] = $callback;
98
99
        $curlJob = $this->factory->createCurlJob($url, [$this, 'process'], $additionalData, $options);
100
101
        // Start job execution.
102
        $this->factory->startJob($curlJob);
103
    }
104
105
    public function process(HttpRequest $curl)
106
    {
107
        $data = $curl->getData();
108
        $additionalData = $curl->getAdditionalData();
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $additionalData is correct as $curl->getAdditionalData() (which targets eXpansion\Framework\Core...st::getAdditionalData()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
109
        $callback = $additionalData['callback'];
110
        unset($additionalData['callback']);
111
112
        $obj = new HttpResult($data['response'], $data['curlInfo'], $curl->getCurlError(), $additionalData);
113
        call_user_func($callback, $obj);
114
    }
115
116
117
}
118