Passed
Push — master ( 2cd332...0af4da )
by Ross
02:35
created

Curl::getFieldsString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
ccs 2
cts 2
cp 1
cc 1
eloc 1
nc 1
nop 1
crap 1
1
<?php
2
declare(strict_types=1);
3
/**
4
 *
5
 * Copyright (C) 2018  Ross Mitchell
6
 *
7
 * This file is part of RossMitchell/UpdateCloudFlare.
8
 *
9
 * RossMitchell/UpdateCloudFlare is free software: you can redistribute
10
 * it and/or modify it under the terms of the GNU General Public License
11
 * as published by the Free Software Foundation, either version 3 of the
12
 * License, or (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17
 * GNU General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU General Public License
20
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21
 */
22
23
namespace RossMitchell\UpdateCloudFlare\Model;
24
25
26
use RossMitchell\UpdateCloudFlare\Interfaces\CurlInterface;
27
use RossMitchell\UpdateCloudFlare\Interfaces\CurlResourceInterface;
28
use RossMitchell\UpdateCloudFlare\Interfaces\RequestInterface;
29
use Symfony\Component\Console\Exception\LogicException;
30
31
/**
32
 * Class Curl
33
 * @package RossMitchell\UpdateCloudFlare\Model
34
 */
35
class Curl implements CurlInterface
36
{
37
    /**
38
     * @var CurlResourceInterface
39
     */
40
    private $curlResource;
41
42
    /**
43
     * Curl constructor.
44
     *
45
     * @param CurlResourceInterface $curlResource
46
     */
47 6
    public function __construct(CurlResourceInterface $curlResource)
48
    {
49 6
        $this->curlResource = $curlResource;
50 6
    }
51
52
    /**
53
     * @param RequestInterface $request
54
     *
55
     * @return string
56
     * @throws LogicException
57
     * @throws \RuntimeException
58
     */
59 6
    public function makeRequest(RequestInterface $request): string
60
    {
61 6
        $curl = \curl_init();
62 6
        $this->setRequiredCurlOptions($request, $curl);
63 6
        $this->setOptionalCurlOptions($request, $curl);
64
65 6
        return $this->sendRequest($curl);
66
    }
67
68
    /**
69
     * @param RequestInterface $request
70
     * @param resource         $curl
71
     */
72 6
    private function setRequiredCurlOptions(RequestInterface $request, $curl): void
73
    {
74 6
        $resource = $this->curlResource;
75 6
        $resource->setOption($curl, \CURLOPT_URL, $request->getUrl());
76 6
        $resource->setOption($curl, \CURLOPT_USERAGENT, 'curl');
77 6
        $resource->setOption($curl, \CURLOPT_CUSTOMREQUEST, $request->getRequestType());
78 6
        $resource->setOption($curl, \CURLOPT_RETURNTRANSFER, true);
79 6
    }
80
81
    /**
82
     * @param RequestInterface $request
83
     * @param resource         $curl
84
     *
85
     * @throws LogicException
86
     */
87 6
    private function setOptionalCurlOptions(RequestInterface $request, $curl): void
88
    {
89 6
        $fields     = $request->getFields();
90 6
        $headers    = $request->getHeaders();
91 6
        $resource   = $this->curlResource;
92 6
        $hasFields  = \count($fields) > 0;
93 6
        $hasHeaders = \count($headers) > 0;
94 6
        if ($hasFields === true) {
95 1
            $resource->setOption($curl, \CURLOPT_POSTFIELDS, $this->getFieldsString($fields));
96
        }
97 6
        if ($hasHeaders === true) {
98 1
            $resource->setOption($curl, \CURLOPT_HTTPHEADER, $headers);
99
        }
100 6
    }
101
102
    /**
103
     * @param resource $curl
104
     *
105
     * @return mixed
106
     * @throws \RuntimeException
107
     */
108 6
    private function sendRequest($curl)
109
    {
110 6
        $resource = $this->curlResource;
111 6
        $result = $resource->curlExec($curl);
112 6
        $error = $resource->curlError($curl);
113 6
        if ($error !== false) {
114 1
            throw new \RuntimeException($error);
115
        }
116 5
        $resource->curlClose($curl);
117
118 5
        return $result;
119
    }
120
121
    /**
122
     * Converts an array of fields into a string that can be sent through
123
     *
124
     * @param array $fields
125
     *
126
     * @return string
127
     * @throws LogicException
128
     */
129 1
    private function getFieldsString(array $fields): string
130
    {
131 1
        return \json_encode($fields);
132
    }
133
}
134