AbstractApi   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 11
c 3
b 0
f 0
lcom 1
cbo 1
dl 0
loc 74
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 7 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
66
        return $year > $currentYear || ($year === $currentYear && $month > $currentMonth);
67
    }
68
69
    /**
70
     * Set the Datetime parse format. Default 'Y-m-d H:i:s'.
71
     *
72
     * @param $format
73
     *
74
     * @return $this
75
     */
76
    public function setDateTimeFormat($format)
77
    {
78
        $this->datetimeFormat = $format;
79
80
        return $this;
81
    }
82
}
83