Completed
Push — master ( 88fe7e...615209 )
by William Johnson S.
02:00
created

AbstractApi::isValidPeriod()   B

Complexity

Conditions 5
Paths 7

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 7
ccs 0
cts 4
cp 0
rs 8.8571
cc 5
eloc 4
nc 7
nop 2
crap 30
1
<?php
2
namespace Katapoka\Ahgora;
3
4
use Katapoka\Ahgora\Contracts\IAhgoraApi;
5
6
/**
7
 * Class AbstractApi.
8
 */
9
abstract class AbstractApi implements IAhgoraApi
10
{
11
    use Loggable;
12
13
    protected $datetimeFormat = 'Y-m-d H:i:s';
14
15
    /**
16
     * Validate if the given month is valid.
17
     *
18
     * @param int $month
19
     *
20
     * @return bool
21
     */
22
    protected function isValidMonth($month)
23
    {
24
        return is_int($month) && ($month >= 1 && $month <= 12);
25
    }
26
27
    /**
28
     * Validate if the given year is "valid".
29
     *
30
     * @param int $year
31
     *
32
     * @return bool
33
     */
34
    protected function isValidYear($year)
35
    {
36
        return $year <= date('Y');
37
    }
38
39
    /**
40
     * Validate if the given period is valid.
41
     *
42
     * @param int $month
43
     * @param int $year
44
     *
45
     * @return bool
46
     */
47
    protected function isValidPeriod($month, $year)
48
    {
49
        $currentYear = (int)date('Y');
50
        $currentMonth = (int)date('m');
51
52
        return $this->isValidMonth($month) && $this->isValidYear($year) && ($year < $currentYear || $year === $currentYear && $month <= $currentMonth);
53
    }
54
55
    /**
56
     * Set the Datetime parse format. Default 'Y-m-d H:i:s'.
57
     *
58
     * @param $format
59
     *
60
     * @return $this
61
     */
62
    public function setDateTimeFormat($format)
63
    {
64
        $this->datetimeFormat = $format;
65
66
        return $this;
67
    }
68
}
69