Completed
Push — master ( a62229...4b3a11 )
by Jean
02:34
created

Transport   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 102
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 6
dl 0
loc 102
ccs 0
cts 43
cp 0
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A findQuotes() 0 8 2
B createSession() 0 27 3
A poll() 0 18 2
A setPollUrl() 0 5 1
1
<?php
2
/**
3
 * @author Jean Silva <[email protected]>
4
 * @license MIT
5
 */
6
namespace Jeancsil\FlightSpy\Api\Http;
7
8
use GuzzleHttp\ClientInterface;
9
use GuzzleHttp\Exception\BadResponseException;
10
use Jeancsil\FlightSpy\Api\DataTransfer\SessionParameters;
11
use Psr\Log\LoggerAwareTrait;
12
13
class Transport
14
{
15
    use LoggerAwareTrait;
16
17
    const LIVE_PRICING = '/apiservices/pricing/v1.0';
18
19
    /**
20
     * @var \GuzzleHttp\Client
21
     */
22
    private $client;
23
24
    /**
25
     * @var string
26
     */
27
    private $pollUrl;
28
29
    /**
30
     * @param ClientInterface $client
31
     */
32
    public function __construct(ClientInterface $client)
33
    {
34
        $this->client = $client;
0 ignored issues
show
Documentation Bug introduced by
$client is of type object<GuzzleHttp\ClientInterface>, but the property $client was declared to be of type object<GuzzleHttp\Client>. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
35
    }
36
37
    /**
38
     * @param SessionParameters $parameters
39
     * @param bool $newSession
40
     * @return array
41
     */
42
    public function findQuotes(SessionParameters $parameters, $newSession)
43
    {
44
        if ($newSession) {
45
            $this->createSession($parameters);
46
        }
47
48
        return $this->poll();
49
    }
50
51
    /**
52
     * @param SessionParameters $parameters
53
     * @throws \Exception
54
     */
55
    private function createSession(SessionParameters $parameters)
56
    {
57
        try {
58
            $parametersArray = $parameters->toArray();
59
60
            $request = $this
61
                ->client
62
                ->post(static::LIVE_PRICING, ['form_params' => $parametersArray]);
63
64
            if (!isset($request->getHeaders()['Location'][0])) {
65
                throw new \Exception('Location not found for the poll');
66
            }
67
68
            $this->setPollUrl($request->getHeaders()['Location'][0], $parametersArray['apiKey']);
69
            sleep(1);
70
        } catch (BadResponseException $e) {
71
            $this->logger->error(
72
                sprintf(
73
                    "[Transport::createSession]\nStatusCode: %d\nBody: %s",
74
                    $e->getResponse()->getStatusCode(),
75
                    $e->getResponse()->getBody()
76
                )
77
            );
78
79
            throw $e;
80
        }
81
    }
82
83
    /**
84
     * @return array
85
     */
86
    private function poll()
87
    {
88
        try {
89
            $request = $this
90
                ->client
91
                ->get($this->pollUrl);
92
93
            return \GuzzleHttp\json_decode($request->getBody()->getContents());
94
        } catch (BadResponseException $e) {
95
            $this->logger->error(
96
                sprintf(
97
                    "[Transport::pool]\nStatusCode: %d\nBody: %s",
98
                    $e->getResponse()->getStatusCode(),
99
                    $e->getResponse()->getBody()
100
                )
101
            );
102
        }
103
    }
104
105
    /**
106
     * @param string $url
107
     * @param string $apiKey
108
     */
109
    private function setPollUrl($url, $apiKey)
110
    {
111
        $this->pollUrl = "$url?apiKey=$apiKey&sorttype=price&sortorder=desc";
0 ignored issues
show
Coding Style Best Practice introduced by
As per coding-style, please use concatenation or sprintf for the variable $url instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
Coding Style Best Practice introduced by
As per coding-style, please use concatenation or sprintf for the variable $apiKey instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
112
        $this->logger->debug("Pool url is: $this->pollUrl");
0 ignored issues
show
Coding Style Best Practice introduced by
As per coding-style, please use concatenation or sprintf for the variable $this instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
113
    }
114
}
115