GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 61d647...5c0fd8 )
by Odiseo
08:26
created

TimePeriodDataFetcher::setExtraConfiguration()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 5
dl 0
loc 12
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Odiseo\SyliusReportPlugin\DataFetcher;
4
5
/**
6
 * Abstract class to provide time periods logic.
7
 *
8
 * @author Łukasz Chruściel <[email protected]>
9
 * @author Diego D'amico <[email protected]>
10
 */
11
abstract class TimePeriodDataFetcher extends BaseDataFetcher
12
{
13
    const PERIOD_DAY = 'day';
14
    const PERIOD_MONTH = 'month';
15
    const PERIOD_YEAR = 'year';
16
17
    /**
18
     * @return array
19
     */
20
    public static function getPeriodChoices()
21
    {
22
        return [
23
            'Daily' => self::PERIOD_DAY,
24
            'Monthly' => self::PERIOD_MONTH,
25
            'Yearly' => self::PERIOD_YEAR,
26
        ];
27
    }
28
29
    /**
30
     * {@inheritdoc}
31
     */
32
    public function fetch(array $configuration): Data
33
    {
34
        $data = new Data();
35
36
        /** @var \DateTime $endDate */
37
        $endDate = $configuration['timePeriod']['end'];
38
39
        //There is added 23 hours 59 minutes 59 seconds to the end date to provide records for whole end date
40
        $configuration['timePeriod']['end'] = $endDate?$endDate->add(new \DateInterval('PT23H59M59S')):null;
0 ignored issues
show
introduced by
$endDate is of type DateTime, thus it always evaluated to true.
Loading history...
41
42
        switch ($configuration['timePeriod']['period']) {
43
            case self::PERIOD_DAY:
44
                $this->setExtraConfiguration($configuration, 'P1D', '%a', 'Y-m-d', ['date']);
45
                break;
46
            case self::PERIOD_MONTH:
47
                $this->setExtraConfiguration($configuration, 'P1M', '%m', 'F Y', ['month', 'year']);
48
                break;
49
            case self::PERIOD_YEAR:
50
                $this->setExtraConfiguration($configuration, 'P1Y', '%y', 'Y', ['year']);
51
                break;
52
            default:
53
                throw new \InvalidArgumentException('Wrong data fetcher period');
54
        }
55
56
        $rawData = $this->getData($configuration);
57
58
        if (empty($rawData)) {
59
            return $data;
60
        }
61
62
        $labelsAux = array_keys($rawData[0]);
63
        $labels = [];
64
        foreach ($labelsAux as $label) {
65
            if (!in_array($label, ['MonthDate', 'YearDate', 'DateDate'])) {
66
                $labels[] = $label;
67
            }
68
        }
69
        $data->setLabels($labels);
70
71
        $fetched = [];
72
73
        if ($configuration['empty_records']) {
74
            $fetched = $this->fillEmptyRecords($fetched, $configuration);
75
        }
76
        foreach ($rawData as $row) {
77
            $rowFetched = [];
78
            foreach ($labels as $i => $label) {
79
                if ($i === 0) {
80
                    $date = new \DateTime($row[$labels[0]]);
81
                    $rowFetched[] = $date->format($configuration['timePeriod']['presentationFormat']);
82
                } else {
83
                    $rowFetched[] = $row[$labels[$i]];
84
                }
85
            }
86
            $fetched[] = $rowFetched;
87
        }
88
89
        $data->setData($fetched);
90
91
        $labels = [];
92
        foreach ($labelsAux as $label) {
93
            if (!in_array($label, ['MonthDate', 'YearDate', 'DateDate'])) {
94
                $labels[] = preg_replace('/(?!^)[A-Z]{2,}(?=[A-Z][a-z])|[A-Z][a-z]/', ' $0', $label);
95
            }
96
        }
97
        $data->setLabels($labels);
98
99
        return $data;
100
    }
101
102
    /**
103
     * @param array  $configuration
104
     * @param string $interval
105
     * @param string $periodFormat
106
     * @param string $presentationFormat
107
     * @param array  $groupBy
108
     */
109
    protected function setExtraConfiguration(
110
        array &$configuration,
111
        $interval,
112
        $periodFormat,
113
        $presentationFormat,
114
        array $groupBy
115
    ) {
116
        $configuration['timePeriod']['interval'] = $interval;
117
        $configuration['timePeriod']['periodFormat'] = $periodFormat;
118
        $configuration['timePeriod']['presentationFormat'] = $presentationFormat;
119
        $configuration['groupBy'] = $groupBy;
120
        $configuration['empty_records'] = false;
121
    }
122
123
    /**
124
     * @param array $fetched
125
     * @param array $configuration
126
     *
127
     * @return array
128
     */
129
    private function fillEmptyRecords(array $fetched, array $configuration)
130
    {
131
        /** @var \DateTime $startDate */
132
        $startDate = $configuration['start'];
133
        /** @var \DateTime $startDate */
134
        $endDate = $configuration['end'];
135
136
        try {
137
            $dateInterval = new \DateInterval($configuration['interval']);
138
        } catch (\Exception $e) {
139
            return $fetched;
140
        }
141
142
        $numberOfPeriods = $startDate->diff($endDate);
143
        $formattedNumberOfPeriods = $numberOfPeriods->format($configuration['periodFormat']);
144
145
        for ($i = 0; $i <= $formattedNumberOfPeriods; ++$i) {
146
            $fetched[$startDate->format($configuration['presentationFormat'])] = 0;
147
            $startDate = $startDate->add($dateInterval);
148
        }
149
150
        return $fetched;
151
    }
152
153
    /**
154
     * @param array $datas
155
     * @param array $configuration
156
     * @return array
157
     */
158
    protected function getMediaResults(array $datas = [], array $configuration = [])
159
    {
160
        if (empty($datas)) {
161
            return [];
162
        }
163
        $labels = array_keys($datas[0]);
164
        $datesMedia = [];
165
        foreach ($datas as $data) {
166
            $date = new \DateTime($data[$labels[0]]);
167
            $dateFormated = $date->format($configuration['timePeriod']['presentationFormat']);
168
            $currentDateMedia = isset($datesMedia[$dateFormated])?$datesMedia[$dateFormated]:array('quantity' => 0, 'media' => 0);
169
            $currentDateMedia['quantity'] = $currentDateMedia['quantity']+1;
170
            $currentDateMedia['media'] = $currentDateMedia['media']+$data[$labels[1]];
171
            $datesMedia[$dateFormated] = $currentDateMedia;
172
        }
173
        $fetched = [];
174
        foreach ($datesMedia as $date => $dateMedia) {
175
            $fetched[] = [
176
                $labels[0] => $date,
177
                $labels[1] => round($dateMedia['media']/$dateMedia['quantity'], 1)
178
            ];
179
        }
180
        return $fetched;
181
    }
182
}
183