Completed
Push — master ( 83fde7...b7ebd3 )
by William Johnson S.
02:06
created

AbstractApi   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 11
c 2
b 0
f 0
lcom 1
cbo 1
dl 0
loc 73
ccs 0
cts 13
cp 0
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A isValidMonth() 0 4 3
A isValidYear() 0 4 1
A isValidPeriod() 0 4 3
A isFutureDate() 0 6 3
A setDateTimeFormat() 0 6 1
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
    /** @var string */
14
    protected $datetimeFormat = 'Y-m-d H:i:s';
15
16
    /**
17
     * Validate if the given month is valid.
18
     *
19
     * @param int $month
20
     *
21
     * @return bool
22
     */
23
    protected function isValidMonth($month)
24
    {
25
        return is_int($month) && ($month >= 1 && $month <= 12);
26
    }
27
28
    /**
29
     * Validate if the given year is "valid".
30
     *
31
     * @param int $year
32
     *
33
     * @return bool
34
     */
35
    protected function isValidYear($year)
36
    {
37
        return $year <= date('Y');
38
    }
39
40
    /**
41
     * Validate if the given period is valid.
42
     *
43
     * @param int $month
44
     * @param int $year
45
     *
46
     * @return bool
47
     */
48
    protected function isValidPeriod($month, $year)
49
    {
50
        return $this->isValidMonth($month) && $this->isValidYear($year) && !$this->isFutureDate($month, $year);
51
    }
52
53
    /**
54
     * Check if the given time period is in the future.
55
     *
56
     * @param int $month
57
     * @param int $year
58
     *
59
     * @return bool
60
     */
61
    private function isFutureDate($month, $year)
62
    {
63
        $currentYear = (int) date('Y');
64
        $currentMonth = (int) date('m');
65
        return $year < $currentYear || ($year === $currentYear && $month <= $currentMonth);
66
    }
67
68
    /**
69
     * Set the Datetime parse format. Default 'Y-m-d H:i:s'.
70
     *
71
     * @param $format
72
     *
73
     * @return $this
74
     */
75
    public function setDateTimeFormat($format)
76
    {
77
        $this->datetimeFormat = $format;
78
79
        return $this;
80
    }
81
}
82