Completed
Push — master ( e6a7c4...d6f8c7 )
by Jean
06:59
created

Period::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 0
cts 5
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 3
crap 2
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
     * @param int $durationInDays
29
     * @param \Datetime $dateFrom
30
     * @param \Datetime $dateTo
31
     */
32
    public function __construct($durationInDays, \Datetime $dateFrom, \Datetime $dateTo)
33
    {
34
        $this->durationInDays = $durationInDays;
35
        $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...
36
        $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...
37
    }
38
39
    /**
40
     * @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...
41
     */
42 2
    public function generateDateCombinations()
43
    {
44 2
        $combinations = new ArrayCollection();
45 2
        $possibleDays = $this->dateFrom->diff($this->dateTo)->days;
46
47 2
        $initialDate = clone $this->dateFrom;
48 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...
49
50 2
        for ($i = 0; $i < $possibleDays; $i++) {
51 2
            $combinations->add([
52 2
                'from' => clone $initialDate,
53 2
                'to' => clone $endDate
54
            ]);
55
56 2
            if ($endDate->diff($this->dateTo)->days == 0) {
57 2
                return $combinations;
58
            }
59
60 2
            $initialDate->add($this->getOneDayMore());
61 2
            $endDate->add($this->getOneDayMore());
62
        }
63
    }
64
65
    /**
66
     * @return int
67
     */
68
    public function getDurationInDays()
69
    {
70
        return $this->durationInDays;
71
    }
72
73
    /**
74
     * @return \Datetime
75
     */
76
    public function getDateFrom()
77
    {
78
        return $this->dateFrom;
79
    }
80
81
    /**
82
     * @return \Datetime
83
     */
84
    public function getDateTo()
85
    {
86
        return $this->dateTo;
87
    }
88
89
    /**
90
     * @return \DateInterval
91
     */
92 2
    private function getOneDayMore() {
93 2
        return new \DateInterval('P1D');
94
    }
95
}
96