BeesWaxRequest::doRequest()   B
last analyzed

Complexity

Conditions 8
Paths 28

Size

Total Lines 58
Code Lines 37

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 37
nc 28
nop 0
dl 0
loc 58
rs 8.0835
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
declare(strict_types=1);
3
4
namespace Audiens\BeesWax;
5
6
use Audiens\BeesWax\Exception\BeesWaxGenericException;
7
use Audiens\BeesWax\Exception\BeesWaxResponseException;
8
9
class BeesWaxRequest
10
{
11
    public const METHOD_GET  = 'GET';
12
    public const METHOD_POST = 'POST';
13
    public const METHOD_PUT  = 'PUT';
14
15
    protected const BEESWAX_ENDPOINT = 'https://%s.api.beeswax.com%s';
16
    protected const BEESWAX_UA       = 'Audiens / BeesWax SDK';
17
18
    /** @var string */
19
    protected $path;
20
21
    /** @var string[] */
22
    protected $queryParams;
23
24
    /** @var string */
25
    protected $method;
26
27
    /** @var BeesWaxSession */
28
    protected $session;
29
30
    /** @var ?string */
31
    protected $payload;
32
33
    /**
34
     * BeesWaxRequest constructor.
35
     *
36
     * @param BeesWaxSession       $session
37
     * @param string               $path
38
     * @param array                $queryParams
39
     * @param string               $method
40
     * @param null|string|mixed    $payload
41
     */
42
    public function __construct(
43
        BeesWaxSession $session,
44
        string $path,
45
        array $queryParams,
46
        string $method,
47
        $payload = null
48
    ) {
49
        $this->path = $path;
50
        $this->queryParams = $queryParams;
51
        $this->method = $method;
52
        $this->session = $session;
53
        $this->payload = $payload;
54
    }
55
56
    /**
57
     * @throws BeesWaxResponseException
58
     * @throws BeesWaxGenericException
59
     */
60
    public function doRequest(): BeesWaxResponse
61
    {
62
        $curlUrl = sprintf(static::BEESWAX_ENDPOINT, $this->session->getBuzzKey(), $this->path);
63
        if (!empty($this->queryParams)) {
64
            $query = \http_build_query($this->queryParams);
65
            $curlUrl .= '?'.$query;
66
        }
67
        $curlHandler = curl_init($curlUrl);
68
        $cookieFileHandler = tmpfile();
69
70
        if ($cookieFileHandler === false) {
71
            throw new BeesWaxGenericException(
72
                'Impossible to create a temporary cookie file',
73
                BeesWaxGenericException::CODE_CANT_CREATE_COOKIE_FILE
74
            );
75
        }
76
77
        $cookieFilePath = stream_get_meta_data($cookieFileHandler)['uri'];
78
79
        if (!$cookieFilePath) {
80
            throw new BeesWaxGenericException(
81
                'An error occurred creating a temporary cookie file',
82
                BeesWaxGenericException::CODE_CANT_CREATE_COOKIE_FILE
83
            );
84
        }
85
86
        $cookieFilePath = realpath($cookieFilePath);
87
        fwrite($cookieFileHandler, $this->session->getSessionCookies());
88
89
        curl_setopt_array($curlHandler, [
90
            CURLOPT_RETURNTRANSFER => 1,
91
            CURLOPT_USERAGENT      => static::BEESWAX_UA,
92
            CURLOPT_COOKIEFILE     => $cookieFilePath,
93
            CURLOPT_COOKIEJAR      => $cookieFilePath,
94
        ]);
95
96
        if ($this->method === static::METHOD_POST) {
97
            curl_setopt($curlHandler, CURLOPT_POST, 1);
98
        } elseif ($this->method === static::METHOD_PUT) {
99
            curl_setopt($curlHandler, CURLOPT_CUSTOMREQUEST, static::METHOD_PUT);
100
        }
101
102
        if ($this->payload) {
103
            curl_setopt($curlHandler, CURLOPT_POSTFIELDS, $this->payload);
104
        }
105
106
        $responsePayload = curl_exec($curlHandler);
107
        if ($responsePayload === false) {
108
            throw new BeesWaxResponseException($curlHandler, 'An error occurred attempting to log into BeesWax');
109
        }
110
        $statusCode = (int) curl_getinfo($curlHandler, CURLINFO_HTTP_CODE);
111
        curl_close($curlHandler);
112
        $cookiesContent = file_get_contents($cookieFilePath);
113
        unlink($cookieFilePath);
114
115
        $response = new BeesWaxResponse($curlHandler, $responsePayload, $statusCode, $cookiesContent);
116
117
        return $response;
118
    }
119
}
120