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

Http   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 96
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 20.59%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
lcom 1
cbo 3
dl 0
loc 96
ccs 7
cts 34
cp 0.2059
rs 10
c 1
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A call() 0 7 1
A get() 0 9 1
A post() 0 20 2
A process() 0 10 1
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 array $postFields array<string, string>
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 = '';
88
        if (!empty($postFields)) {
89
            $query = '?'.http_build_query($postFields);
90
        }
91
        $options[CURLOPT_URL] = $url;
92
        $options[CURLOPT_POSTFIELDS] = $query;
93
94
        $additionalData['callback'] = $callback;
95
96
        $curlJob = $this->factory->createCurlJob($url, [$this, 'process'], $additionalData, $options, $postFields);
97
98
        // Start job execution.
99
        $this->factory->startJob($curlJob);
100
    }
101
102
    public function process(HttpRequest $curl)
103
    {
104
        $data = $curl->getData();
105
        $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...
106
        $callback = $additionalData['callback'];
107
        unset($additionalData['callback']);
108
109
        $obj = new HttpResult($data['response'], $data['curlInfo'], $curl->getCurlError(), $additionalData);
110
        call_user_func($callback, $obj);
111
    }
112
113
114
}
115