Completed
Push — master ( 390235...3437c9 )
by Paweł
25s
created

OrderRepository   A

Complexity

Total Complexity 32

Size/Duplication

Total Lines 385
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 32
c 1
b 0
f 0
lcom 1
cbo 4
dl 0
loc 385
rs 9.6

17 Methods

Rating   Name   Duplication   Size   Complexity  
A createListQueryBuilder() 0 10 1
B findForDetailsPage() 0 43 1
D createFilterPaginator() 0 64 10
A countByCustomerAndCoupon() 0 20 1
B createCheckoutsPaginator() 0 35 6
A createByCustomerQueryBuilder() 0 13 1
A createPaginatorByCustomer() 0 7 1
A findByCustomer() 0 10 1
A findBetweenDates() 0 9 1
A countBetweenDates() 0 10 1
A revenueBetweenDates() 0 10 1
A countByCustomer() 0 8 1
A findOneForPayment() 0 13 1
A findExpired() 0 16 1
A findCompleted() 0 13 1
A findOneByNumberAndCustomer() 0 12 1
A createQueryBuilderBetweenDates() 0 18 2
1
<?php
2
3
/*
4
 * This file is part of the Sylius package.
5
 *
6
 * (c) Paweł Jędrzejewski
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Sylius\Bundle\CoreBundle\Doctrine\ORM;
13
14
use Doctrine\ORM\QueryBuilder;
15
use Sylius\Bundle\CoreBundle\Doctrine\ORM\CartRepository;
16
use Sylius\Component\Core\Model\CouponInterface;
17
use Sylius\Component\Core\Model\CustomerInterface;
18
use Sylius\Component\Core\Model\OrderInterface;
19
use Sylius\Component\Core\Repository\OrderRepositoryInterface;
20
21
class OrderRepository extends CartRepository implements OrderRepositoryInterface
22
{
23
    /**
24
     * {@inheritdoc}
25
     */
26
    public function createListQueryBuilder()
27
    {
28
        $queryBuilder = $this->createQueryBuilder('o');
29
30
        return $queryBuilder
31
            ->addSelect('customer')
32
            ->leftJoin('o.customer', 'customer')
33
            ->andWhere($queryBuilder->expr()->isNotNull('o.completedAt'))
34
        ;
35
    }
36
37
    /**
38
     * {@inheritdoc}
39
     */
40
    public function createByCustomerQueryBuilder(CustomerInterface $customer)
41
    {
42
        $queryBuilder = $this->createQueryBuilder('o');
43
44
        $queryBuilder
45
            ->andWhere($queryBuilder->expr()->isNotNull('o.completedAt'))
46
            ->innerJoin('o.customer', 'customer')
47
            ->andWhere('customer = :customer')
48
            ->setParameter('customer', $customer)
49
        ;
50
51
        return $queryBuilder;
52
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57
    public function createPaginatorByCustomer(CustomerInterface $customer, array $sorting = [])
58
    {
59
        $queryBuilder = $this->createByCustomerQueryBuilder($customer);
60
        $this->applySorting($queryBuilder, $sorting);
61
62
        return $this->getPaginator($queryBuilder);
63
    }
64
65
    /**
66
     * {@inheritdoc}
67
     */
68
    public function findByCustomer(CustomerInterface $customer, array $sorting = [])
69
    {
70
        $queryBuilder = $this->createByCustomerQueryBuilder($customer);
71
        $this->applySorting($queryBuilder, $sorting);
72
73
        return $queryBuilder
74
            ->getQuery()
75
            ->getResult()
76
        ;
77
    }
78
79
    /**
80
     * {@inheritdoc}
81
     */
82
    public function findForDetailsPage($id)
83
    {
84
        $queryBuilder = $this->createQueryBuilder('o');
85
        $queryBuilder
86
            ->leftJoin('o.adjustments', 'adjustment')
87
            ->leftJoin('o.customer', 'customer')
88
            ->leftJoin('o.items', 'item')
89
            ->leftJoin('item.units', 'itemUnit')
90
            ->leftJoin('o.shipments', 'shipment')
91
            ->leftJoin('shipment.method', 'shippingMethod')
92
            ->leftJoin('o.payments', 'payments')
93
            ->leftJoin('payments.method', 'paymentMethods')
94
            ->leftJoin('item.variant', 'variant')
95
            ->leftJoin('variant.images', 'image')
96
            ->leftJoin('variant.object', 'product')
97
            ->leftJoin('variant.options', 'optionValue')
98
            ->leftJoin('optionValue.option', 'option')
99
            ->leftJoin('o.billingAddress', 'billingAddress')
100
            ->leftJoin('o.shippingAddress', 'shippingAddress')
101
            ->addSelect('item')
102
            ->addSelect('adjustment')
103
            ->addSelect('customer')
104
            ->addSelect('itemUnit')
105
            ->addSelect('shipment')
106
            ->addSelect('shippingMethod')
107
            ->addSelect('payments')
108
            ->addSelect('paymentMethods')
109
            ->addSelect('variant')
110
            ->addSelect('image')
111
            ->addSelect('product')
112
            ->addSelect('option')
113
            ->addSelect('optionValue')
114
            ->addSelect('billingAddress')
115
            ->addSelect('shippingAddress')
116
            ->andWhere($queryBuilder->expr()->eq('o.id', ':id'))
117
            ->setParameter('id', $id)
118
        ;
119
120
        return $queryBuilder
121
            ->getQuery()
122
            ->getOneOrNullResult()
123
        ;
124
    }
125
126
    /**
127
     * {@inheritdoc}
128
     */
129
    public function findOneForPayment($id)
130
    {
131
        return $this->createQueryBuilder('o')
132
            ->leftJoin('o.payments', 'payments')
133
            ->leftJoin('payments.method', 'paymentMethods')
134
            ->addSelect('payments')
135
            ->addSelect('paymentMethods')
136
            ->andWhere('o.id = :id')
137
            ->setParameter('id', $id)
138
            ->getQuery()
139
            ->getOneOrNullResult()
140
        ;
141
    }
142
143
    /**
144
     * {@inheritdoc}
145
     */
146
    public function createFilterPaginator(array $criteria = null, array $sorting = null)
147
    {
148
        $queryBuilder = $this->createQueryBuilder('o');
149
150
        $queryBuilder
151
            ->andWhere($queryBuilder->expr()->isNotNull('o.completedAt'))
152
            ->leftJoin('o.customer', 'customer')
153
            ->addSelect('customer')
154
        ;
155
156
        if (!empty($criteria['number'])) {
157
            $queryBuilder
158
                ->andWhere('o.number = :number')
159
                ->setParameter('number', $criteria['number'])
160
            ;
161
        }
162
        if (!empty($criteria['totalFrom'])) {
163
            $queryBuilder
164
                ->andWhere($queryBuilder->expr()->gte('o.total', ':totalFrom'))
165
                ->setParameter('totalFrom', $criteria['totalFrom'] * 100)
166
            ;
167
        }
168
        if (!empty($criteria['totalTo'])) {
169
            $queryBuilder
170
                ->andWhere($queryBuilder->expr()->lte('o.total', ':totalTo'))
171
                ->setParameter('totalTo', $criteria['totalTo'] * 100)
172
            ;
173
        }
174
        if (!empty($criteria['createdAtFrom'])) {
175
            $queryBuilder
176
                ->andWhere($queryBuilder->expr()->gte('o.createdAt', ':createdAtFrom'))
177
                ->setParameter('createdAtFrom', $criteria['createdAtFrom'])
178
            ;
179
        }
180
        if (!empty($criteria['createdAtTo'])) {
181
            $queryBuilder
182
                ->andWhere($queryBuilder->expr()->lte('o.createdAt', ':createdAtTo'))
183
                ->setParameter('createdAtTo', $criteria['createdAtTo'])
184
            ;
185
        }
186
        if (!empty($criteria['paymentState'])) {
187
            $queryBuilder
188
                ->andWhere($queryBuilder->expr()->eq('o.paymentState', ':paymentState'))
189
                ->setParameter('paymentState', $criteria['paymentState'])
190
            ;
191
        }
192
        if (!empty($criteria['channel'])) {
193
            $queryBuilder
194
                ->andWhere($queryBuilder->expr()->eq('o.channel', ':channel'))
195
                ->setParameter('channel', $criteria['channel'])
196
            ;
197
        }
198
199
        if (empty($sorting)) {
200
            if (!is_array($sorting)) {
201
                $sorting = [];
202
            }
203
            $sorting['updatedAt'] = 'desc';
204
        }
205
206
        $this->applySorting($queryBuilder, $sorting);
207
208
        return $this->getPaginator($queryBuilder);
209
    }
210
211
    /**
212
     * {@inheritdoc}
213
     */
214
    public function countByCustomerAndCoupon(CustomerInterface $customer, CouponInterface $coupon)
215
    {
216
        $queryBuilder = $this->createQueryBuilder('o')
217
            ->select('count(o.id)')
218
            ->leftJoin('o.items', 'item')
219
            ->innerJoin('o.promotionCoupon', 'coupon')
220
            ->andWhere('o.customer = :customer')
221
            ->andWhere('o.completedAt IS NOT NULL')
222
            ->andWhere('coupon = :coupon')
223
            ->setParameter('customer', $customer)
224
            ->setParameter('coupon', $coupon)
225
        ;
226
227
        $count = (int) $queryBuilder
228
            ->getQuery()
229
            ->getSingleScalarResult()
230
        ;
231
232
        return $count;
233
    }
234
235
    /**
236
     * {@inheritdoc}
237
     */
238
    public function createCheckoutsPaginator(array $criteria = null, array $sorting = null)
239
    {
240
        $queryBuilder = $this->createQueryBuilder('o');
241
        $queryBuilder->andWhere($queryBuilder->expr()->isNull('o.completedAt'));
242
243
        if (!empty($criteria['createdAtFrom'])) {
244
            $queryBuilder
245
                ->andWhere($queryBuilder->expr()->gte('o.createdAt', ':createdAtFrom'))
246
                ->setParameter('createdAtFrom', $criteria['createdAtFrom'])
247
            ;
248
        }
249
        if (!empty($criteria['createdAtTo'])) {
250
            $queryBuilder
251
                ->andWhere($queryBuilder->expr()->lte('o.createdAt', ':createdAtTo'))
252
                ->setParameter('createdAtTo', $criteria['createdAtTo'])
253
            ;
254
        }
255
        if (!empty($criteria['channel'])) {
256
            $queryBuilder
257
                ->andWhere($queryBuilder->expr()->eq('o.channel', ':channel'))
258
                ->setParameter('channel', $criteria['channel'])
259
            ;
260
        }
261
262
        if (empty($sorting)) {
263
            if (!is_array($sorting)) {
264
                $sorting = [];
265
            }
266
            $sorting['updatedAt'] = 'desc';
267
        }
268
269
        $this->applySorting($queryBuilder, $sorting);
270
271
        return $this->getPaginator($queryBuilder);
272
    }
273
274
    /**
275
     * {@inheritdoc}
276
     */
277
    public function countByCustomer(CustomerInterface $customer)
278
    {
279
       return (int) $this->createByCustomerQueryBuilder($customer)
280
            ->select('count(o.id)')
281
            ->getQuery()
282
            ->getSingleScalarResult()
283
        ;
284
    }
285
286
    /**
287
     * {@inheritdoc}
288
     */
289
    public function findBetweenDates(\DateTime $from, \DateTime $to, $state = null)
290
    {
291
        $queryBuilder = $this->createQueryBuilderBetweenDates($from, $to, $state);
292
293
        return $queryBuilder
294
            ->getQuery()
295
            ->getResult()
296
        ;
297
    }
298
299
    /**
300
     * {@inheritdoc}
301
     */
302
    public function countBetweenDates(\DateTime $from, \DateTime $to, $state = null)
303
    {
304
        $queryBuilder = $this->createQueryBuilderBetweenDates($from, $to, $state);
305
306
        return (int) $queryBuilder
307
            ->select('count(o.id)')
308
            ->getQuery()
309
            ->getSingleScalarResult()
310
        ;
311
    }
312
313
    /**
314
     * {@inheritdoc}
315
     */
316
    public function revenueBetweenDates(\DateTime $from, \DateTime $to, $state = null)
317
    {
318
        $queryBuilder = $this->createQueryBuilderBetweenDates($from, $to, $state);
319
320
        return (int)$queryBuilder
321
            ->select('sum(o.total)')
322
            ->getQuery()
323
            ->getSingleScalarResult()
324
        ;
325
    }
326
327
    /**
328
     * {@inheritdoc}
329
     */
330
    public function findExpired(\DateTime $expiresAt, $state = OrderInterface::STATE_NEW)
331
    {
332
        $queryBuilder = $this->createQueryBuilder('o')
333
            ->leftJoin('o.items', 'item')
334
            ->addSelect('item')
335
        ;
336
337
        $queryBuilder
338
            ->andWhere($queryBuilder->expr()->lt('o.expiresAt', ':expiresAt'))
339
            ->andWhere('o.state = :state')
340
            ->setParameter('expiresAt', $expiresAt)
341
            ->setParameter('state', $state)
342
        ;
343
344
        return $queryBuilder->getQuery()->getResult();
345
    }
346
347
    /**
348
     * {@inheritdoc}
349
     */
350
    public function findCompleted(array $sorting = [], $limit = 5)
351
    {
352
        $queryBuilder = $this->createQueryBuilder('o');
353
        $queryBuilder->andWhere($queryBuilder->expr()->isNotNull('o.completedAt'));
354
355
        $this->applySorting($queryBuilder, $sorting);
356
357
        return $queryBuilder
358
            ->setMaxResults($limit)
359
            ->getQuery()
360
            ->getResult()
361
        ;
362
    }
363
364
    /**
365
     * {@inheritdoc}
366
     */
367
    public function findOneByNumberAndCustomer($number, CustomerInterface $customer)
368
    {
369
        return $this->createQueryBuilder('o')
370
            ->leftJoin('o.customer', 'customer')
371
            ->andWhere('customer = :customer')
372
            ->andWhere('o.number = :number')
373
            ->setParameter('customer', $customer)
374
            ->setParameter('number', $number)
375
            ->getQuery()
376
            ->getOneOrNullResult()
377
        ;
378
    }
379
380
    /**
381
     * @param \DateTime $from
382
     * @param \DateTime $to
383
     * @param string $state
384
     *
385
     * @return QueryBuilder
386
     */
387
    private function createQueryBuilderBetweenDates(\DateTime $from, \DateTime $to, $state)
388
    {
389
        $queryBuilder = $this->createQueryBuilder('o');
390
391
        if (null !== $state) {
392
            $queryBuilder->andWhere('o.state = :state')->setParameter('state', $state);
393
        }
394
395
        $queryBuilder
396
            ->andWhere($queryBuilder->expr()->isNotNull('o.completedAt'))
397
            ->andWhere($queryBuilder->expr()->gte('o.createdAt', ':from'))
398
            ->andWhere($queryBuilder->expr()->lte('o.createdAt', ':to'))
399
            ->setParameter('from', $from)
400
            ->setParameter('to', $to)
401
        ;
402
403
        return $queryBuilder;
404
    }
405
}
406