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

Period   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 113
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 55.56%

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 1
dl 0
loc 113
ccs 15
cts 27
cp 0.5556
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A generateDateCombinations() 0 21 3
A getDurationInDays() 0 4 1
A getDateFrom() 0 4 1
A getDateTo() 0 4 1
A __toString() 0 18 2
A getOneDayMore() 0 3 1
1
<?php
2
/**
3
 * @author Jean Silva <[email protected]>
4
 * @license MIT
5
 */
6
namespace Jeancsil\FlightSpy\Api\DataTransfer;
7
8
use Doctrine\Common\Collections\ArrayCollection;
9
10
class Period
11
{
12
    /**
13
     * @var int
14
     */
15
    private $durationInDays;
16
17
    /**
18
     * @var \Datetime
19
     */
20
    private $dateFrom;
21
22
    /**
23
     * @var \Datetime
24
     */
25
    private $dateTo;
26
27
    /**
28
     * @var ArrayCollection
29
     */
30
    private $combinations;
31
32
    /**
33
     * @param int $durationInDays
34
     * @param \Datetime $dateFrom
35
     * @param \Datetime $dateTo
36
     */
37
    public function __construct($durationInDays, \Datetime $dateFrom, \Datetime $dateTo)
38
    {
39
        $this->durationInDays = $durationInDays;
40
        $this->dateFrom = $dateFrom;
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...
41
        $this->dateTo = $dateTo;
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...
42 2
        $this->combinations = new ArrayCollection();
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...
43
    }
44 2
45 2
    /**
46
     * @return ArrayCollection
0 ignored issues
show
Documentation introduced by
Should the return type not be ArrayCollection|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
47 2
     */
48 2
    public function generateDateCombinations()
49
    {
50 2
        $possibleDays = $this->dateFrom->diff($this->dateTo)->days;
51 2
52 2
        $initialDate = clone $this->dateFrom;
53 2
        $endDate = clone $this->dateFrom->add(new \DateInterval(sprintf('P%dD', $this->durationInDays)));
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...
54
55
        for ($i = 0; $i < $possibleDays; $i++) {
56 2
            $this->combinations->add([
57 2
                'outboundDate' => $initialDate->format('Y-m-d'),
58
                'inboundDate' => $endDate->format('Y-m-d')
59
            ]);
60 2
61 2
            if ($endDate->diff($this->dateTo)->days == 0) {
62
                return $this->combinations;
63
            }
64
65
            $initialDate->add($this->getOneDayMore());
66
            $endDate->add($this->getOneDayMore());
67
        }
68
    }
69
70
    /**
71
     * @return int
72
     */
73
    public function getDurationInDays()
74
    {
75
        return $this->durationInDays;
76
    }
77
78
    /**
79
     * @return \Datetime
80
     */
81
    public function getDateFrom()
82
    {
83
        return $this->dateFrom;
84
    }
85
86
    /**
87
     * @return \Datetime
88
     */
89
    public function getDateTo()
90
    {
91
        return $this->dateTo;
92 2
    }
93 2
94
    /**
95
     * @return string
96
     */
97
    public function __toString()
98
    {
99
        $string = sprintf(
100
            "Duration: %d days\nBetween %s and %s (inclusive)",
101
            $this->durationInDays,
102
            $this->dateFrom->format('d/m/Y'),
103
            $this->dateTo->format('d/m/Y')
104
        );
105
106
        if (!$this->combinations->isEmpty()) {
107
            $string .= sprintf(
108
                "\nResulted in these combinations:\n %s",
109
                var_export($this->combinations->toArray(), true)
110
            );
111
        }
112
113
        return $string;
114
    }
115
116
    /**
117
     * @return \DateInterval
118
     */
119
    private function getOneDayMore() {
120
        return new \DateInterval('P1D');
121
    }
122
}
123