ReportTotalCommand::__construct()   C
last analyzed

Complexity

Conditions 14
Paths 7

Size

Total Lines 44
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 28
CRAP Score 14

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 14
eloc 26
c 2
b 0
f 0
nc 7
nop 6
dl 0
loc 44
ccs 28
cts 28
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 DateTimeImmutable;
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 19
    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 19
        $endYear = $endYear ?: $startYear;
45 19
        $endMonth = $endMonth ?: $startMonth;
46 19
        $today = $this->today();
47 19
        $currentYear = intval($today->format('Y'));
48 19
        $currentPeriod = sprintf('%04d-%02d', $currentYear, intval($today->format('m')));
49
50 19
        $this->rfc = $rfc;
51 19
        $this->type = $type;
52 19
        $this->startYear = $startYear;
53 19
        $this->startMonth = $startMonth;
54 19
        $this->endYear = $endYear;
55 19
        $this->endMonth = $endMonth;
56 19
        $this->startPeriod = sprintf('%04d-%02d', $this->startYear, $this->startMonth);
57 19
        $this->endPeriod = sprintf('%04d-%02d', $this->endYear, $this->endMonth);
58
59 19
        if ($startYear < 2000 || $startYear > $currentYear) {
60 2
            throw new LogicException(sprintf('Start year %d is not between 2000 and %d', $startYear, $currentYear));
61
        }
62 17
        if ($endYear < 2000 || $endYear > $currentYear) {
63 2
            throw new LogicException(sprintf('End year %d is not between 2000 and %d', $endYear, $currentYear));
64
        }
65 15
        if ($startMonth < 1 || $startMonth > 12) {
66 2
            throw new LogicException(sprintf('Start month %d is not between 1 and 12', $startMonth));
67
        }
68 13
        if ($endMonth < 1 || $endMonth > 12) {
69 2
            throw new LogicException(sprintf('End month %d is not between 1 and 12', $endMonth));
70
        }
71
72 11
        if ($this->startPeriod > $this->endPeriod) {
73 1
            throw new LogicException(
74 1
                sprintf('Start period %s cannot be greater than end period %s', $this->startPeriod, $this->endPeriod)
75 1
            );
76
        }
77
78 10
        if ($this->startPeriod < $this->endPeriod && $this->endPeriod >= $currentPeriod) {
79 2
            throw new LogicException('Cannot combine multiple past months with current/future months');
80
        }
81
    }
82
83 5
    public function rfc(): string
84
    {
85 5
        return $this->rfc;
86
    }
87
88 4
    public function type(): string
89
    {
90 4
        return $this->type;
91
    }
92
93 2
    public function startPeriod(): string
94
    {
95 2
        return $this->startPeriod;
96
    }
97
98 8
    public function endPeriod(): string
99
    {
100 8
        return $this->endPeriod;
101
    }
102
103 5
    public function startString(): string
104
    {
105 5
        return sprintf('%04d-%02d-01T00:00:00', $this->startYear, $this->startMonth);
106
    }
107
108 5
    public function endString(): string
109
    {
110 5
        $date = new DateTimeImmutable(sprintf('%04d-%02d-01', $this->endYear, $this->endMonth));
111 5
        $today = $this->today();
112 5
        if ($this->endPeriod() === $today->format('Y-m')) {
113 2
            $date = $today;
114
        } else {
115 3
            $date = $date->modify('+ 1 month');
116
        }
117 5
        return sprintf('%sT00:00:00', $date->format('Y-m-d'));
118
    }
119
120 18
    protected function today(): DateTimeImmutable
121
    {
122 18
        return new DateTimeImmutable('today');
123
    }
124
}
125