Completed
Pull Request — master (#11)
by Carlos C
03:41
created

ReportTotalCommand::__construct()   C

Complexity

Conditions 14
Paths 7

Size

Total Lines 43
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 26
CRAP Score 14

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 14
eloc 25
c 1
b 0
f 0
nc 7
nop 6
dl 0
loc 43
ccs 26
cts 26
cp 1
crap 14
rs 6.2666

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace PhpCfdi\Finkok\Services\Utilities;
6
7
use DateTime;
8
use LogicException;
9
10
class ReportTotalCommand
11
{
12
    /** @var string */
13
    private $rfc;
14
15
    /** @var string */
16
    private $type;
17
18
    /** @var int */
19
    private $startYear;
20
21
    /** @var int */
22
    private $startMonth;
23
24
    /** @var int */
25
    private $endYear;
26
27
    /** @var int */
28
    private $endMonth;
29
30
    /** @var string */
31
    private $startPeriod;
32
33
    /** @var string */
34
    private $endPeriod;
35
36 17
    public function __construct(
37
        string $rfc,
38
        string $type,
39
        int $startYear,
40
        int $startMonth,
41
        int $endYear = 0,
42
        int $endMonth = 0
43
    ) {
44 17
        $endYear = $endYear ?: $startYear;
45 17
        $endMonth = $endMonth ?: $startMonth;
46 17
        $today = new DateTime('today');
47 17
        $currentYear = intval($today->format('Y'));
48
49 17
        $this->rfc = $rfc;
50 17
        $this->type = $type;
51 17
        $this->startYear = $startYear;
52 17
        $this->startMonth = $startMonth;
53 17
        $this->endYear = $endYear;
54 17
        $this->endMonth = $endMonth;
55 17
        $this->startPeriod = sprintf('%04d-%02d', $this->startYear, $this->startMonth);
56 17
        $this->endPeriod = sprintf('%04d-%02d', $this->endYear, $this->endMonth);
57
58 17
        if ($startYear < 2000 || $startYear > $currentYear) {
59 2
            throw new LogicException(sprintf('Start year %d is not between 2000 and %d', $startYear, $currentYear));
60
        }
61 15
        if ($endYear < 2000 || $endYear > $currentYear) {
62 2
            throw new LogicException(sprintf('End year %d is not between 2000 and %d', $endYear, $currentYear));
63
        }
64 13
        if ($startMonth < 1 || $startMonth > 12) {
65 2
            throw new LogicException(sprintf('Start month %d is not between 1 and 12', $startMonth));
66
        }
67 11
        if ($endMonth < 1 || $endMonth > 12) {
68 2
            throw new LogicException(sprintf('End month %d is not between 1 and 12', $endMonth));
69
        }
70
71 9
        if ($this->startPeriod > $this->endPeriod) {
72 1
            throw new LogicException(
73 1
                sprintf('Start period %s cannot be greater than end period %s', $this->startPeriod, $this->endPeriod)
74
            );
75
        }
76
77 8
        if ($this->startPeriod < $this->endPeriod && $this->endPeriod >= $today->format('Y-m')) {
78 2
            throw new LogicException(sprintf('Cannot combine multiple past months with current/future months'));
79
        }
80 6
    }
81
82 3
    public function rfc(): string
83
    {
84 3
        return $this->rfc;
85
    }
86
87 2
    public function type(): string
88
    {
89 2
        return $this->type;
90
    }
91
92 2
    public function startPeriod(): string
93
    {
94 2
        return $this->startPeriod;
95
    }
96
97 6
    public function endPeriod(): string
98
    {
99 6
        return $this->endPeriod;
100
    }
101
102 3
    public function startString(): string
103
    {
104 3
        return sprintf('%04d-%02d-01T00:00:00', $this->startYear, $this->startMonth);
105
    }
106
107 3
    public function endString(): string
108
    {
109 3
        $date = (new DateTime())->setDate($this->endYear, $this->endMonth, 1);
110 3
        $today = new DateTime('today');
111 3
        if ($this->endPeriod() === $today->format('Y-m')) {
112 1
            $date = $today;
113
        } else {
114 2
            $date->modify('+ 1 month');
115
        }
116 3
        return sprintf('%sT00:00:00', $date->format('Y-m-d'));
117
    }
118
}
119