ReportRepository   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 226
Duplicated Lines 0 %

Importance

Changes 6
Bugs 0 Features 0
Metric Value
wmc 15
eloc 73
c 6
b 0
f 0
dl 0
loc 226
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getSalesForChannelForDates() 0 27 2
A getProductVariantSalesForChannelForDates() 0 25 2
A getProductOptionValueSalesForChannelForDates() 0 37 4
A getProductOptionSalesForChannelForDates() 0 36 4
A getProductSalesForChannelForDates() 0 25 2
A getAverageSalesForChannelForDates() 0 6 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace MonsieurBiz\SyliusSalesReportsPlugin\Repository;
6
7
use MonsieurBiz\SyliusSalesReportsPlugin\Exception\InvalidDateException;
8
use MonsieurBiz\SyliusSalesReportsPlugin\Exception\MissingLocaleException;
9
use Sylius\Component\Core\Model\ChannelInterface;
10
11
class ReportRepository extends AbstractReportRepository
12
{
13
    /**
14
     * Get total sales for channel between 2 date times, or average sales from a given field
15
     *
16
     * @param ChannelInterface $channel
17
     * @param \DateTimeInterface $from
18
     * @param \DateTimeInterface|null $to
19
     * @param string|null $groupField
20
     * @return array
21
     * @throws InvalidDateException
22
     */
23
    public function getSalesForChannelForDates(
24
        ChannelInterface $channel,
25
        \DateTimeInterface $from,
26
        ?\DateTimeInterface $to = null,
27
        ?string $groupField = null
28
    ): array {
29
        $to = $to ?? $from; // If to is null, take the same day as from to make report on one day
30
        try {
31
            $from = new \DateTime($from->format("Y-m-d") . " 00:00:00");
32
            $to = new \DateTime($to->format("Y-m-d") . " 23:59:59");
33
        } catch (\Exception $e) {
34
            throw new InvalidDateException('Invalid date given to report.');
35
        }
36
37
        $this->initResult();
38
39
        // Order Item Units values
40
        $this->addResults($this->getOrderItemUnitValues($channel, $from, $to), $groupField);
41
        // Order Items values
42
        $this->addResults($this->getOrderItemValues($channel, $from, $to), $groupField);
43
        // Order values
44
        $this->addResults($this->getOrderValues($channel, $from, $to), $groupField);
45
46
        // Divide results by number of elements if needed
47
        $this->averageResult();
48
49
        return $this->result;
50
    }
51
52
53
    /**
54
     * Get average sales for channel between 2 date times
55
     *
56
     * @param ChannelInterface $channel
57
     * @param \DateTimeInterface $from
58
     * @param \DateTimeInterface|null $to
59
     * @return array
60
     * @throws InvalidDateException
61
     */
62
    public function getAverageSalesForChannelForDates(
63
        ChannelInterface $channel,
64
        \DateTimeInterface $from,
65
        ?\DateTimeInterface $to = null
66
    ): array {
67
        return $this->getSalesForChannelForDates($channel, $from, $to, 'order_id');
68
    }
69
70
    /**
71
     * Get sales per product variant for channel between 2 date times
72
     *
73
     * @param ChannelInterface $channel
74
     * @param \DateTimeInterface $from
75
     * @param \DateTimeInterface|null $to
76
     * @return array
77
     * @throws InvalidDateException
78
     */
79
    public function getProductVariantSalesForChannelForDates(
80
        ChannelInterface $channel,
81
        \DateTimeInterface $from,
82
        ?\DateTimeInterface $to = null
83
    ): array {
84
        $to = $to ?? $from; // If to is null, take the same day as from to make report on one day
85
        try {
86
            $from = new \DateTime($from->format("Y-m-d") . " 00:00:00");
87
            $to = new \DateTime($to->format("Y-m-d") . " 23:59:59");
88
        } catch (\Exception $e) {
89
            throw new InvalidDateException('Invalid date given to report.');
90
        }
91
92
        $this->results = [];
93
94
        // Order Item Units values
95
        $this->addResultsByElement(
96
            $this->getOrderItemUnitValues($channel, $from, $to), 'variant_id', 'variant_name'
97
        );
98
        // Order Items values
99
        $this->addResultsByElement(
100
            $this->getOrderItemValues($channel, $from, $to), 'variant_id', 'variant_name'
101
        );
102
103
        return $this->results;
104
    }
105
106
    /**
107
     * Get sales per product option for channel between 2 date times
108
     *
109
     * @param ChannelInterface $channel
110
     * @param \DateTimeInterface $from
111
     * @param \DateTimeInterface|null $to
112
     * @return array
113
     * @throws InvalidDateException
114
     * @throws MissingLocaleException
115
     */
116
    public function getProductOptionSalesForChannelForDates(
117
        ChannelInterface $channel,
118
        \DateTimeInterface $from,
119
        ?\DateTimeInterface $to = null
120
    ): array {
121
        $to = $to ?? $from; // If to is null, take the same day as from to make report on one day
122
        try {
123
            $from = new \DateTime($from->format("Y-m-d") . " 00:00:00");
124
            $to = new \DateTime($to->format("Y-m-d") . " 23:59:59");
125
        } catch (\Exception $e) {
126
            throw new InvalidDateException('Invalid date given to report.');
127
        }
128
129
        $this->results = [];
130
131
        // Order Item Units values
132
        $this->addResultsByElement(
133
            $this->getOrderItemUnitValues($channel, $from, $to), 'variant_id', 'variant_name'
134
        );
135
        // Order Items values
136
        $this->addResultsByElement(
137
            $this->getOrderItemValues($channel, $from, $to), 'variant_id', 'variant_name'
138
        );
139
140
        // Populate array with options values data
141
        if (!($locale = $channel->getDefaultLocale()) || (!$localeCode = $locale->getCode())) {
142
            throw new MissingLocaleException('Missing default locale for channel');
143
        }
144
        $resultsWithOptions = $this->populateOptions($localeCode);
145
146
        // Reinit results to generate a new one
147
        $this->results = [];
148
149
        $this->addResultsByElement($resultsWithOptions, 'option_code', 'option_label');
150
151
        return $this->results;
152
    }
153
154
    /**
155
     * Get sales per product option for channel between 2 date times
156
     *
157
     * @param ChannelInterface $channel
158
     * @param \DateTimeInterface $from
159
     * @param \DateTimeInterface|null $to
160
     * @return array
161
     * @throws InvalidDateException
162
     * @throws MissingLocaleException
163
     */
164
    public function getProductOptionValueSalesForChannelForDates(
165
        ChannelInterface $channel,
166
        \DateTimeInterface $from,
167
        ?\DateTimeInterface $to = null
168
    ): array {
169
        $to = $to ?? $from; // If to is null, take the same day as from to make report on one day
170
        try {
171
            $from = new \DateTime($from->format("Y-m-d") . " 00:00:00");
172
            $to = new \DateTime($to->format("Y-m-d") . " 23:59:59");
173
        } catch (\Exception $e) {
174
            throw new InvalidDateException('Invalid date given to report.');
175
        }
176
177
        $this->results = [];
178
179
        // Order Item Units values
180
        $this->addResultsByElement(
181
            $this->getOrderItemUnitValues($channel, $from, $to), 'variant_id', 'variant_name'
182
        );
183
        // Order Items values
184
        $this->addResultsByElement(
185
            $this->getOrderItemValues($channel, $from, $to), 'variant_id', 'variant_name'
186
        );
187
188
        // Populate array with options values data
189
        if (!($locale = $channel->getDefaultLocale()) || (!$localeCode = $locale->getCode())) {
190
            throw new MissingLocaleException('Missing default locale for channel');
191
        }
192
        $resultsWithOptions = $this->populateOptions($localeCode);
193
194
        // Reinit results to generate a new one
195
        $this->results = [];
196
197
        $this->addResultsByElement($resultsWithOptions, 'option_value_code', 'option_value_label',
198
            ['option_code', 'option_label']);
199
200
        return $this->results;
201
    }
202
203
    /**
204
     * Get sales per product for channel between 2 date times
205
     *
206
     * @param ChannelInterface $channel
207
     * @param \DateTimeInterface $from
208
     * @param \DateTimeInterface|null $to
209
     * @return array
210
     * @throws InvalidDateException
211
     */
212
    public function getProductSalesForChannelForDates(
213
        ChannelInterface $channel,
214
        \DateTimeInterface $from,
215
        ?\DateTimeInterface $to = null
216
    ): array {
217
        $to = $to ?? $from; // If to is null, take the same day as from to make report on one day
218
        try {
219
            $from = new \DateTime($from->format("Y-m-d") . " 00:00:00");
220
            $to = new \DateTime($to->format("Y-m-d") . " 23:59:59");
221
        } catch (\Exception $e) {
222
            throw new InvalidDateException('Invalid date given to report.');
223
        }
224
225
        $this->results = [];
226
227
        // Order Item Units values
228
        $this->addResultsByElement(
229
            $this->getOrderItemUnitValues($channel, $from, $to), 'product_id', 'product_name'
230
        );
231
        // Order Items values
232
        $this->addResultsByElement(
233
            $this->getOrderItemValues($channel, $from, $to), 'product_id', 'product_name'
234
        );
235
236
        return $this->results;
237
    }
238
}
239