CurlHttpSender   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 79
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 3
dl 0
loc 79
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A send() 0 19 2
A close() 0 5 1
A initializeCurlResource() 0 10 2
A prepareCurlResourceByRequest() 0 18 3
1
<?php
2
3
declare(strict_types = 1);
4
5
/*
6
 * This file is part of the AppleApnPush package
7
 *
8
 * (c) Vitaliy Zhuk <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code
12
 */
13
14
namespace Apple\ApnPush\Protocol\Http\Sender;
15
16
use Apple\ApnPush\Protocol\Http\Request;
17
use Apple\ApnPush\Protocol\Http\Response;
18
use Apple\ApnPush\Protocol\Http\Sender\Exception\HttpSenderException;
19
20
/**
21
 * Send HTTP request via cURL
22
 */
23
class CurlHttpSender implements HttpSenderInterface
24
{
25
    /**
26
     * @var resource
27
     */
28
    private $resource;
29
30
    /**
31
     * {@inheritdoc}
32
     *
33
     * @throws HttpSenderException
34
     */
35
    public function send(Request $request): Response
36
    {
37
        $this->initializeCurlResource();
38
        $this->prepareCurlResourceByRequest($request);
39
40
        $content = \curl_exec($this->resource);
41
42
        if (false === $content) {
43
            throw new HttpSenderException(sprintf(
44
                'cURL Error [%d]: %s',
45
                (int) curl_errno($this->resource),
46
                (string) curl_error($this->resource)
47
            ));
48
        }
49
50
        $statusCode = (int) \curl_getinfo($this->resource, CURLINFO_HTTP_CODE);
51
52
        return new Response($statusCode, (string) $content);
53
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58
    public function close(): void
59
    {
60
        \curl_close($this->resource);
61
        $this->resource = null;
62
    }
63
64
    /**
65
     * Initialize cURL resource
66
     */
67
    private function initializeCurlResource(): void
68
    {
69
        if (!$this->resource) {
70
            $this->resource = \curl_init();
71
72
            \curl_setopt($this->resource, CURLOPT_RETURNTRANSFER, 1);
73
            \curl_setopt($this->resource, CURLOPT_POST, 1);
74
            \curl_setopt($this->resource, CURLOPT_HTTP_VERSION, 3);
75
        }
76
    }
77
78
    /**
79
     * Prepare cURL resource by request
80
     *
81
     * @param Request $request
82
     */
83
    private function prepareCurlResourceByRequest(Request $request): void
84
    {
85
        \curl_setopt($this->resource, CURLOPT_URL, $request->getUrl());
86
        \curl_setopt($this->resource, CURLOPT_POSTFIELDS, $request->getContent());
87
88
        if ($request->getCertificate()) {
89
            \curl_setopt($this->resource, CURLOPT_SSLCERT, $request->getCertificate());
90
            \curl_setopt($this->resource, CURLOPT_SSLCERTPASSWD, $request->getCertificatePassPhrase());
91
        }
92
93
        $inlineHeaders = [];
94
95
        foreach ($request->getHeaders() as $name => $value) {
96
            $inlineHeaders[] = \sprintf('%s: %s', $name, $value);
97
        }
98
99
        \curl_setopt($this->resource, CURLOPT_HTTPHEADER, $inlineHeaders);
100
    }
101
}
102