LivePricePostProcessor   A
last analyzed

Complexity

Total Complexity 20

Size/Duplication

Total Lines 162
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 20
lcom 1
cbo 6
dl 0
loc 162
ccs 0
cts 86
cp 0
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A addNotifier() 0 4 1
A notifyAll() 0 6 2
A multiProcess() 0 14 3
A singleProcess() 0 4 1
A setSessionParameters() 0 6 1
B doProcess() 0 32 4
A getDeepLinkUrl() 0 6 2
A getPrice() 0 4 1
A getAgentName() 0 15 4
1
<?php
2
/**
3
 * @author Jean Silva <[email protected]>
4
 * @license MIT
5
 */
6
namespace Jeancsil\FlightSpy\Api\Processor;
7
8
use Jeancsil\FlightSpy\Api\DataTransfer\SessionParameters;
9
use Jeancsil\FlightSpy\Notifier\Deal;
10
use Jeancsil\FlightSpy\Notifier\NotifiableInterface;
11
use Jeancsil\FlightSpy\Service\Currency\PriceFormatter;
12
use Psr\Log\LoggerAwareTrait;
13
14
class LivePricePostProcessor
15
{
16
    use LoggerAwareTrait;
17
18
    const MAX_PARSED_DEALS = 50;
19
20
    /**
21
     * @var PriceFormatter
22
     */
23
    private $priceFormatter;
24
25
    /**
26
     * @var NotifiableInterface[]
27
     */
28
    private $notifiers = [];
29
    /**
30
     * @var SessionParameters
31
     */
32
    private $sessionParameters;
33
    /**
34
     * @var array
35
     */
36
    private $agents = [];
37
38
    public function __construct(PriceFormatter $priceFormatter)
39
    {
40
        $this->priceFormatter = $priceFormatter;
41
    }
42
43
44
    /**
45
     * @param \Jeancsil\FlightSpy\Notifier\NotifiableInterface $notifier
46
     */
47
    public function addNotifier(NotifiableInterface $notifier)
48
    {
49
        $this->notifiers[] = $notifier;
50
    }
51
52
    /**
53
     * @param Deal[] $deals
54
     */
55
    public function notifyAll(array $deals)
56
    {
57
        foreach ($this->notifiers as $notifier) {
58
            $notifier->notify($deals, $this->sessionParameters);
59
        }
60
    }
61
62
    /**
63
     * @param array $responses
64
     */
65
    public function multiProcess(array $responses)
66
    {
67
        $deals = [];
68
        foreach ($responses as $response) {
69
            if (!$response) {
70
                $this->logger->warning(sprintf('Empty response received: %s', var_export($response, true)));
71
                continue;
72
            }
73
74
            $deals = array_merge($deals, $this->doProcess($response));
75
        }
76
77
        $this->notifyAll($deals);
78
    }
79
80
    /**
81
     * @param \stdClass $response
82
     */
83
    public function singleProcess(\stdClass $response)
84
    {
85
        $this->notifyAll($this->doProcess($response));
86
    }
87
88
    /**
89
     * @param SessionParameters $sessionParameters
90
     * @return $this
91
     */
92
    public function setSessionParameters(SessionParameters $sessionParameters)
93
    {
94
        $this->sessionParameters = $sessionParameters;
95
96
        return $this;
97
    }
98
99
    /**
100
     * @param \stdClass $response
101
     * @return Deal[]
102
     */
103
    private function doProcess(\stdClass $response)
104
    {
105
        $itineraries = $response->Itineraries;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 2 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
106
        $this->agents = $response->Agents;
107
108
        $deals = [];
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 15 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
109
        $resultCount = 1;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 9 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
110
        $cheapestItineraries = array_slice($itineraries, 0, static::MAX_PARSED_DEALS);
111
        foreach ($cheapestItineraries as $itinerary) {
112
            $logMessage = sprintf("Verifying itinerary #%s: ", $resultCount++);
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal Verifying itinerary #%s: does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
113
114
            foreach ($itinerary['PricingOptions'] as $pricingOption) {
115
                $price = $this->getPrice($pricingOption);
116
117
                if ($price <= $this->sessionParameters->getMaxPrice()) {
118
                    $formattedPrice = $this->priceFormatter->format($price, $this->sessionParameters->currency);
119
                    $this->logger->debug(
120
                        "$logMessage [{$this->getAgentName($pricingOption)}] Deal found ($formattedPrice)"
0 ignored issues
show
Coding Style Best Practice introduced by
As per coding-style, please use concatenation or sprintf for the variable $logMessage 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 $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...
Coding Style Best Practice introduced by
As per coding-style, please use concatenation or sprintf for the variable $pricingOption 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 $formattedPrice 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...
121
                    );
122
123
                    $deals[] = new Deal(
124
                        $this->sessionParameters,
125
                        $price,
126
                        $this->getAgentName($pricingOption),
127
                        $this->getDeepLinkUrl($pricingOption)
128
                    );
129
                }
130
            }
131
        }
132
133
        return $deals;
134
    }
135
136
    /**
137
     * @param \stdClass $pricingOptions
138
     * @return string
139
     */
140
    private function getDeepLinkUrl($pricingOptions)
141
    {
142
        if (isset($pricingOptions['DeeplinkUrl'])) {
143
            return $pricingOptions['DeeplinkUrl'];
144
        }
145
    }
146
147
    /**
148
     * @param \stdClass $pricingOptions
149
     * @return string
150
     */
151
    private function getPrice($pricingOptions)
152
    {
153
        return $pricingOptions['Price'];
154
    }
155
156
    /**
157
     * @param \stdClass $pricingOptions
158
     * @return string
159
     */
160
    private function getAgentName($pricingOptions)
161
    {
162
        $agentId = 0;
163
        $agents = $pricingOptions['Agents'];
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 2 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
164
        foreach ($agents as $agent) {
165
            $agentId = $agent;
166
            continue;
167
        }
168
169
        foreach ($this->agents as $agent) {
170
            if ($agent['Id'] == $agentId) {
171
                return $agent['Name'];
172
            }
173
        }
174
    }
175
}
176