File size: 5,735 Bytes
d2897cd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
<?php

namespace Mautic\StatsBundle\Aggregate\Helper;

use Mautic\StatsBundle\Aggregate\Collection\DAO\StatDAO;

class CalculatorHelper
{
    /**
     * @throws \Exception
     */
    public static function getYearLabel($year, $labelFormat): string
    {
        return (new \DateTime(self::getYearDateString($year)))->format($labelFormat);
    }

    public static function getYearDateString($year): string
    {
        return "$year-01-01 00:00:00";
    }

    /**
     * @param string $lastYear
     * @param string $thisYear
     * @param string $labelFormat
     *
     * @throws \Exception
     */
    public static function fillInMissingYears(StatDAO $statDAO, $lastYear, $thisYear, $labelFormat): void
    {
        if (!$lastYear) {
            return;
        }

        $lastYear = new \DateTime(self::getYearDateString($lastYear));
        $thisYear = new \DateTime(self::getYearDateString($thisYear));

        if (!isset($statDAO->getStats()[$lastYear->format($labelFormat)])) {
            $statDAO->addStat($lastYear->format($labelFormat), 0);
        }

        while ($lastYear < $thisYear) {
            $lastYear->modify('+1 year');
            $statDAO->addStat($lastYear->format($labelFormat), 0);
        }
    }

    /**
     * @throws \Exception
     */
    public static function getMonthLabel($month, $labelFormat): string
    {
        return (new \DateTime(self::getMonthDateString($month)))->format($labelFormat);
    }

    public static function getMonthDateString($month): string
    {
        return "$month-01 00:00:00";
    }

    /**
     * @param string $lastMonth
     * @param string $thisMonth
     * @param string $labelFormat
     *
     * @throws \Exception
     */
    public static function fillInMissingMonths(StatDAO $statDAO, $lastMonth, $thisMonth, $labelFormat): void
    {
        if (!$lastMonth) {
            return;
        }

        $lastMonth = new \DateTime(self::getMonthDateString($lastMonth));
        $thisMonth = new \DateTime(self::getMonthDateString($thisMonth));

        if (!isset($statDAO->getStats()[$lastMonth->format($labelFormat)])) {
            $statDAO->addStat($lastMonth->format($labelFormat), 0);
        }

        while ($lastMonth < $thisMonth) {
            $lastMonth->modify('+1 month');
            $statDAO->addStat($lastMonth->format($labelFormat), 0);
        }
    }

    /**
     * @throws \Exception
     */
    public static function getDayLabel($day, $labelFormat): string
    {
        return (new \DateTime(self::getDayDateString($day)))->format($labelFormat);
    }

    public static function getDayDateString($day): string
    {
        return "$day 00:00:00";
    }

    /**
     * @param string $date
     *
     * @throws \Exception
     */
    public static function getWeekFromDayString($date): string
    {
        return (new \DateTime($date))->format('Y-W');
    }

    /**
     * @param string $date
     * @param string $labelFormat
     *
     * @throws \Exception
     */
    public static function getWeekLabel($date, $labelFormat = 'Y-W'): string
    {
        return (new \DateTime(self::getWeekDateString($date)))->format($labelFormat);
    }

    public static function getWeekDateString($date): string
    {
        if (!preg_match('/^([0-9]{4})-([0-9]{2})$/', $date, $matches)) {
            throw new \InvalidArgumentException('Invalid argument, Y-W format is required.');
        }
        $year = $matches[1];
        $week = $matches[2];

        return date('Y-m-d 00:00:00', strtotime($year.'W'.$week.'1'));
    }

    /**
     * @param string $yesterday
     * @param string $today
     * @param string $labelFormat
     *
     * @throws \Exception
     */
    public static function fillInMissingWeeks(StatDAO $statDAO, $yesterday, $today, $labelFormat): void
    {
        if (!$yesterday) {
            return;
        }

        $yesterday = new \DateTime(self::getWeekDateString($yesterday));
        $today     = new \DateTime(self::getWeekDateString($today));

        while ($yesterday < $today) {
            $statDAO->addStat($yesterday->format($labelFormat), 0);
            $yesterday->modify('+1 week');
        }
    }

    /**
     * @param string $yesterday
     * @param string $today
     * @param string $labelFormat
     *
     * @throws \Exception
     */
    public static function fillInMissingDays(StatDAO $statDAO, $yesterday, $today, $labelFormat): void
    {
        if (!$yesterday) {
            return;
        }

        $yesterday = (new \DateTime(self::getDayDateString($yesterday)))->modify('-1 day');
        $today     = new \DateTime(self::getDayDateString($today));

        while ($yesterday < $today) {
            $yesterday->modify('+1 day');
            $statDAO->addStat($yesterday->format($labelFormat), 0);
        }
    }

    /**
     * @throws \Exception
     */
    public static function getHourLabel($hour, $labelFormat): string
    {
        return (new \DateTime(self::getHourDateString($hour)))->format($labelFormat);
    }

    public static function getHourDateString($hour): string
    {
        return "$hour:00:00";
    }

    /**
     * @param string $lastHour
     * @param string $thisHour
     * @param string $labelFormat
     *
     * @throws \Exception
     */
    public static function fillInMissingHours(StatDAO $statDAO, $lastHour, $thisHour, $labelFormat): void
    {
        if (!$lastHour) {
            return;
        }

        $lastHour = new \DateTime(self::getHourDateString($lastHour));
        $thisHour = new \DateTime(self::getHourDateString($thisHour));

        while ($lastHour < $thisHour) {
            $lastHour->modify('+1 hour');
            $statDAO->addStat($lastHour->format($labelFormat), 0);
        }
    }
}