Client::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 4
Bugs 0 Features 2
Metric Value
c 4
b 0
f 2
dl 0
loc 7
ccs 6
cts 6
cp 1
rs 9.4285
cc 1
eloc 5
nc 1
nop 4
crap 1
1
<?php
2
3
/*
4
 * This file is part of the Kickbox Bundle.
5
 *
6
 * (c) Abdoul Ndiaye <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Andi\KickBoxBundle\Http;
13
14
use Andi\KickBoxBundle\Exception\EmptyContentException;
15
use Andi\KickBoxBundle\Exception\KickBoxApiException;
16
use Andi\KickBoxBundle\Factory\ResponseFactory;
17
use GuzzleHttp\Client as HttpClient;
18
use GuzzleHttp\Exception\RequestException;
19
use Psr\Http\Message\ResponseInterface;
20
21
/**
22
 * The Kickbox http client that verify email addresses.
23
 *
24
 * @author Abdoul Ndiaye <[email protected]>
25
 */
26
class Client
27
{
28
    /**
29
     * @var HttpClient
30
     */
31
    protected $client;
32
33
    /**
34
     * @var ResponseFactory
35
     */
36
    protected $responseFactory;
37
38
    /**
39
     * @var string
40
     */
41
    protected $endPoint;
42
43
    /**
44
     * @var string
45
     */
46
    protected $key;
47
48
    /**
49
     * Construct.
50
     *
51
     * @param HttpClient      $client          A Guzzle client instance.
52
     * @param ResponseFactory $responseFactory A Response Factory instance.
53
     * @param string          $endPoint        A KickBox API endpoint.
54
     * @param string          $key             An Api key generated in kickbox.io.
55
     */
56 3
    public function __construct(HttpClient $client, ResponseFactory $responseFactory, $endPoint, $key)
57
    {
58 3
        $this->client          = $client;
59 3
        $this->responseFactory = $responseFactory;
60 3
        $this->endPoint        = $endPoint;
61 3
        $this->key             = $key;
62 3
    }
63
64
    /**
65
     * Call the Api to validate one specific email address.
66
     *
67
     * @param string $email   The email address to be verified
68
     * @param int    $timeout Maximum time, in milliseconds, for the API to complete a verification request.
69
     *
70
     * @return Response     A Kickbox response instance.
71
     */
72 3
    public function verify($email, $timeout = 6000)
73
    {
74
        try {
75 3
            $httpResponse = $this->client->get($this->endPoint, $this->getQueryParameters($email, $timeout));
76 3
        } catch (RequestException $exception) {
77
            /* @var ResponseInterface $exceptionResponse */
78 1
            $exceptionResponse = $exception->getResponse();
79 1
            $errorContent      = json_decode($exceptionResponse->getBody(), true);
80
81 1
            throw new KickBoxApiException(
82 1
                $errorContent['message'],
83 1
                $exception->getRequest(),
84 1
                $exception->getResponse(),
85
                $exception
86 1
            );
87
        }
88
89 2
        $parameters = json_decode($httpResponse->getBody(), true);
90
91 2
        if (empty($parameters)) {
92 1
            throw new EmptyContentException();
93
        }
94
95 1
        return $this->responseFactory->createResponse($httpResponse->getHeaders(), $parameters);
96
    }
97
98
    /**
99
     * Return the query parameters for a kickbox api call.
100
     *
101
     * @param string $email   The email address.
102
     * @param int    $timeout Time in milliseconds.
103
     *
104
     * @return array The query parameters.
105
     */
106 3
    protected function getQueryParameters($email, $timeout = 6000)
107
    {
108
        return [
109
            'query' => [
110 3
                'email'   => $email,
111 3
                'apikey'  => $this->key,
112 3
                'timeout' => $timeout,
113
            ]
114 3
        ];
115
    }
116
}
117