SessionParametersFactory   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 167
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 16
c 0
b 0
f 0
lcom 1
cbo 5
dl 0
loc 167
ccs 0
cts 101
cp 0
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A createFromConfigFile() 0 14 2
A createFromInput() 0 19 2
B createFromArray() 0 25 3
A createSessionParameters() 0 20 1
A getPeriod() 0 8 1
A shouldCreateAllPossiblePeriods() 0 16 4
A getValue() 0 8 2
1
<?php
2
/**
3
 * @author Jean Silva <[email protected]>
4
 * @license MIT
5
 */
6
namespace Jeancsil\FlightSpy\Api\DataTransfer;
7
8
use Jeancsil\FlightSpy\Command\Entity\Parameter;
9
use Psr\Log\LoggerAwareTrait;
10
use Symfony\Component\Console\Input\InputInterface;
11
12
class SessionParametersFactory
13
{
14
    use LoggerAwareTrait;
15
16
    /**
17
     * @var string
18
     */
19
    private $apiKey;
20
21
    /**
22
     * @var array
23
     */
24
    private $configCache;
25
26
    /**
27
     * @param string $apiKey
28
     */
29
    public function __construct($apiKey)
30
    {
31
        $this->apiKey = $apiKey;
32
    }
33
34
    /**
35
     * @param string $configFile
36
     * @return SessionParameters[]
37
     */
38
    public function createFromConfigFile($configFile)
39
    {
40
        $configurations = json_decode(file_get_contents($configFile), true);
41
42
        $parameters = [];
43
        $maxPrices = [];
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...
44
        foreach ($configurations as $configuration) {
45
            $parameters = $this->createFromArray($configuration);
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...
46
            $maxPrices[] = $this->getValue(Parameter::MAX_PRICE);
47
            $this->logger->debug($parameters);
0 ignored issues
show
Documentation introduced by
$parameters is of type array<integer,object<Jea...fer\SessionParameters>>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
48
        }
49
50
        return $parameters;
51
    }
52
53
    /**
54
     * @param InputInterface $input
55
     * @return SessionParameters
56
     */
57
    public function createFromInput(InputInterface $input)
58
    {
59
        $parameters = new SessionParameters();
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 19 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...
60
        $parameters->apiKey = $input->getOption(Parameter::API_KEY) ?: $this->apiKey;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 11 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...
61
        $parameters->originPlace = $input->getOption(Parameter::FROM);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 6 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...
62
        $parameters->destinationPlace = $input->getOption(Parameter::TO);
63
        $parameters->outboundDate = $input->getOption(Parameter::DEPARTURE_DATE);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 5 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...
64
        $parameters->inboundDate = $input->getOption(Parameter::RETURN_DATE);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 6 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...
65
        $parameters->locationSchema = $input->getOption(Parameter::LOCATION_SCHEMA);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 3 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...
66
        $parameters->country = $input->getOption(Parameter::COUNTRY);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 10 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...
67
        $parameters->currency = $input->getOption(Parameter::CURRENCY);
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...
68
        $parameters->locale = $input->getOption(Parameter::LOCALE);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 11 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...
69
        $parameters->adults = $input->getOption(Parameter::ADULTS);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 11 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...
70
        $parameters->cabinClass = $input->getOption(Parameter::CABIN_CLASS);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 7 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...
71
        $parameters->children = $input->getOption(Parameter::CHILDREN);
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...
72
        $parameters->infants = $input->getOption(Parameter::INFANTS);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 10 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...
73
74
        return $parameters;
75
    }
76
77
    /**
78
     * @param array $configuration
79
     * @return SessionParameters[]
80
     */
81
    private function createFromArray(array $configuration)
82
    {
83
        $this->configCache = $configuration;
84
        $parameters = [];
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 8 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...
85
86
        if ($this->shouldCreateAllPossiblePeriods()) {
87
            $dateCombinations = $this->getPeriod()->generateDateCombinations();
88
89
            foreach ($dateCombinations as $combination) {
0 ignored issues
show
Bug introduced by
The expression $dateCombinations of type object<Doctrine\Common\C...s\ArrayCollection>|null is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
90
                $parameters[] = $this->createSessionParameters(
91
                    $combination['outboundDate'],
92
                    $combination['inboundDate']
93
                );
94
            }
95
96
            return $parameters;
97
        }
98
99
        $parameters[] = $this->createSessionParameters(
100
            $this->getValue(Parameter::DEPARTURE_DATE),
101
            $this->getValue(Parameter::RETURN_DATE)
102
        );
103
104
        return $parameters;
105
    }
106
107
    /**
108
     * @param $outboundDate
109
     * @param $inboundDate
110
     * @return SessionParameters
111
     */
112
    private function createSessionParameters($outboundDate, $inboundDate)
113
    {
114
        $parameters = new SessionParameters();
115
        $parameters->setMaxPrice($this->getValue(Parameter::MAX_PRICE));
116
        $parameters->apiKey = $this->getValue(Parameter::API_KEY, $this->apiKey);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 11 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...
117
        $parameters->originPlace = $this->getValue(Parameter::FROM);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 6 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...
118
        $parameters->destinationPlace = $this->getValue(Parameter::TO);
119
        $parameters->outboundDate = $outboundDate;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 5 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...
120
        $parameters->inboundDate = $inboundDate;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 6 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...
121
        $parameters->locationSchema = $this->getValue(Parameter::LOCATION_SCHEMA, Parameter::DEFAULT_LOCATION_SCHEMA);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 3 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...
122
        $parameters->country = $this->getValue(Parameter::COUNTRY);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 10 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...
123
        $parameters->currency = $this->getValue(Parameter::CURRENCY);
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...
124
        $parameters->locale = $this->getValue(Parameter::LOCALE);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 11 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...
125
        $parameters->adults = $this->getValue(Parameter::ADULTS, Parameter::DEFAULT_ADULTS);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 11 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...
126
        $parameters->cabinClass = $this->getValue(Parameter::CABIN_CLASS, Parameter::DEFAULT_CABIN_CLASS);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 7 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...
127
        $parameters->children = $this->getValue(Parameter::CHILDREN, Parameter::DEFAULT_CHILDREN);
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...
128
        $parameters->infants = $this->getValue(Parameter::INFANTS, Parameter::DEFAULT_INFANTS);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 10 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...
129
130
        return $parameters;
131
    }
132
133
    /**
134
     * @return Period
135
     */
136
    private function getPeriod()
137
    {
138
        return new Period(
139
            $this->configCache[Parameter::SEARCH_PERIOD_TRAVEL_DAYS],
140
            new \DateTime($this->configCache[Parameter::SEARCH_PERIOD_FROM]),
141
            new \DateTime($this->configCache[Parameter::SEARCH_PERIOD_TO])
142
        );
143
    }
144
145
    /**
146
     * @return bool
147
     */
148
    private function shouldCreateAllPossiblePeriods()
149
    {
150
        if (!isset($this->configCache[Parameter::SEARCH_PERIOD_FROM])) {
151
            return false;
152
        }
153
154
        if (!isset($this->configCache[Parameter::SEARCH_PERIOD_TO])) {
155
            return false;
156
        }
157
158
        if (!isset($this->configCache[Parameter::SEARCH_PERIOD_TRAVEL_DAYS])) {
0 ignored issues
show
Unused Code introduced by
This if statement, and the following return statement can be replaced with return isset($this->conf...H_PERIOD_TRAVEL_DAYS]);.
Loading history...
159
            return false;
160
        }
161
162
        return true;
163
    }
164
165
    /**
166
     * @param string $parameter
167
     * @param mixed $defaultValue
168
     * @return mixed
169
     */
170
    private function getValue($parameter, $defaultValue = null)
171
    {
172
        if (isset($this->configCache[$parameter])) {
173
            return $this->configCache[$parameter];
174
        }
175
176
        return $defaultValue;
177
    }
178
}
179