AbstractMonthlyReport   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 90
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 1
dl 0
loc 90
c 0
b 0
f 0
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
B initializeMonthsArray() 0 23 5
A addMonth() 0 4 1
A calculateNumberOfMonths() 0 4 1
A getMonthsTitles() 0 16 2
A getMonthsValues() 0 4 1
A getMonths() 0 4 1
A setMonths() 0 4 1
A jsonSerialize() 0 4 1
1
<?php
2
3
/**
4
 * Storgman - Student Organizations Management
5
 * Copyright (C) 2014, Dejan Angelov <[email protected]>
6
 *
7
 * This file is part of Storgman.
8
 *
9
 * Storgman is free software: you can redistribute it and/or modify
10
 * it under the terms of the GNU General Public License as published by
11
 * the Free Software Foundation, either version 3 of the License, or
12
 * (at your option) any later version.
13
 *
14
 * Storgman is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU General Public License
20
 * along with Storgman.  If not, see <http://www.gnu.org/licenses/>.
21
 *
22
 * @package Storgman
23
 * @copyright Copyright (C) 2014, Dejan Angelov <[email protected]>
24
 * @license https://github.com/angelov/storgman/blob/master/LICENSE
25
 * @author Dejan Angelov <[email protected]>
26
 */
27
28
namespace Angelov\Storgman\Core\Reports;
29
30
use Angelov\Storgman\Core\DateTime;
31
use JsonSerializable;
32
33
abstract class AbstractMonthlyReport implements JsonSerializable
34
{
35
    protected $months;
36
    protected $beginDate;
37
    protected $endDate;
38
39
    public function __construct(DateTime $beginDate, DateTime $endDate)
40
    {
41
        $this->beginDate = $beginDate;
42
        $this->endDate = $endDate;
43
44
        $this->initializeMonthsArray();
45
    }
46
47
    /**
48
     * @param mixed $defaultValue
49
     * @todo Smelly code, smelly code... what are you doing here?
50
     */
51
    protected function initializeMonthsArray($defaultValue = 0)
52
    {
53
        $year = $this->beginDate->format("Y");
54
        $count = 0;
55
        $numNeededMonths = $this->calculateNumberOfMonths();
56
57
        for ($m = $this->beginDate->format('m'); $m <= 12; $m++) {
58
            $count++;
59
60
            $dt = new DateTime("1-" . $m . "-" . $year);
61
            $month = $dt->format('m');
62
            $this->months[$year . "-" . $month] = $defaultValue;
63
64
            if ($m == 12 && $count < 12) {
65
                $m = 0;
66
                $year++;
67
            }
68
69
            if ($count == $numNeededMonths) {
70
                break;
71
            }
72
        }
73
    }
74
75
    public function addMonth($month, $value)
76
    {
77
        $this->months[$month] = $value;
78
    }
79
80
    private function calculateNumberOfMonths()
81
    {
82
        return DateTime::monthsBetween($this->beginDate, $this->endDate) + 1;
83
    }
84
85
    /** @todo There's probably a better way to do this. */
86
    public function getMonthsTitles()
87
    {
88
        $months = array_keys($this->months);
89
90
        foreach ($months as &$month) {
91
            $parts = explode("-", $month);
92
            $m = $parts[1];
93
            $y = $parts[0];
94
95
            $m = new DateTime("01-" . $m . "-" . $y);
96
97
            $month = $m->format("M Y");
98
        }
99
100
        return $months;
101
    }
102
103
    public function getMonthsValues()
104
    {
105
        return array_values($this->months);
106
    }
107
108
    public function getMonths()
109
    {
110
        return $this->months;
111
    }
112
113
    public function setMonths(array $months)
114
    {
115
        $this->months = $months;
116
    }
117
118
    public function jsonSerialize()
119
    {
120
        // @todo
121
    }
122
}
123