Completed
Push — checkout-optimisation ( 40d0de )
by Kamil
21:30
created

OrderRepository::findCartForSelectPayment()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 24
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 24
rs 8.9713
c 0
b 0
f 0
cc 2
eloc 15
nc 2
nop 1
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\EntityManager;
15
use Doctrine\ORM\Mapping;
16
use Sylius\Bundle\OrderBundle\Doctrine\ORM\OrderRepository as BaseOrderRepository;
17
use Sylius\Component\Core\Model\ChannelInterface;
18
use Sylius\Component\Core\Model\CustomerInterface;
19
use Sylius\Component\Core\Model\OrderInterface;
20
use Sylius\Component\Core\Model\PromotionCouponInterface;
21
use Sylius\Component\Core\OrderCheckoutStates;
22
use Sylius\Component\Core\OrderPaymentStates;
23
use Sylius\Component\Core\Repository\OrderRepositoryInterface;
24
25
class OrderRepository extends BaseOrderRepository implements OrderRepositoryInterface
26
{
27
    /**
28
     * @var AssociationHydrator
29
     */
30
    protected $associationHydrator;
31
32
    /**
33
     * {@inheritdoc}
34
     */
35
    public function __construct(EntityManager $em, Mapping\ClassMetadata $class)
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $em. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
36
    {
37
        parent::__construct($em, $class);
38
39
        $this->associationHydrator = new AssociationHydrator($this->_em, $class);
40
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45
    public function createListQueryBuilder()
46
    {
47
        return $this->createQueryBuilder('o')
48
            ->addSelect('channel')
49
            ->addSelect('customer')
50
            ->innerJoin('o.channel', 'channel')
51
            ->leftJoin('o.customer', 'customer')
52
            ->andWhere('o.state != :state')
53
            ->setParameter('state', OrderInterface::STATE_CART)
54
        ;
55
    }
56
57
    /**
58
     * {@inheritdoc}
59
     */
60
    public function createByCustomerIdQueryBuilder($customerId)
61
    {
62
        return $this->createQueryBuilder('o')
63
            ->andWhere('o.customer = :customerId')
64
            ->andWhere('o.state != :state')
65
            ->setParameter('customerId', $customerId)
66
            ->setParameter('state', OrderInterface::STATE_CART)
67
        ;
68
    }
69
70
    /**
71
     * {@inheritdoc}
72
     */
73
    public function findByCustomer(CustomerInterface $customer)
74
    {
75
        return $this->createByCustomerIdQueryBuilder($customer->getId())
76
            ->getQuery()
77
            ->getResult()
78
        ;
79
    }
80
81
    /**
82
     * {@inheritdoc}
83
     */
84
    public function findForCustomerStatistics(CustomerInterface $customer)
85
    {
86
        return $this->createByCustomerIdQueryBuilder($customer->getId())
87
            ->andWhere('o.state NOT IN (:states)')
88
            ->setParameter('states', [OrderInterface::STATE_CART, OrderInterface::STATE_CANCELLED])
89
            ->getQuery()
90
            ->getResult()
91
        ;
92
    }
93
94
    /**
95
     * {@inheritdoc}
96
     */
97
    public function findOneForPayment($id)
98
    {
99
        return $this->createQueryBuilder('o')
100
            ->addSelect('payments')
101
            ->addSelect('paymentMethods')
102
            ->leftJoin('o.payments', 'payments')
103
            ->leftJoin('payments.method', 'paymentMethods')
104
            ->andWhere('o.id = :id')
105
            ->setParameter('id', $id)
106
            ->getQuery()
107
            ->getOneOrNullResult()
108
        ;
109
    }
110
111
    /**
112
     * {@inheritdoc}
113
     */
114
    public function countByCustomerAndCoupon(CustomerInterface $customer, PromotionCouponInterface $coupon)
115
    {
116
        return (int) $this->createQueryBuilder('o')
117
            ->select('COUNT(o.id)')
118
            ->andWhere('o.customer = :customer')
119
            ->andWhere('o.promotionCoupon = :coupon')
120
            ->andWhere('o.state NOT IN (:states)')
121
            ->setParameter('customer', $customer)
122
            ->setParameter('coupon', $coupon)
123
            ->setParameter('states', [OrderInterface::STATE_CART, OrderInterface::STATE_CANCELLED])
124
            ->getQuery()
125
            ->getSingleScalarResult()
126
        ;
127
    }
128
129
    /**
130
     * {@inheritdoc}
131
     */
132
    public function countByCustomer(CustomerInterface $customer)
133
    {
134
        return (int) $this->createQueryBuilder('o')
135
            ->select('COUNT(o.id)')
136
            ->andWhere('o.customer = :customer')
137
            ->andWhere('o.state NOT IN (:states)')
138
            ->setParameter('customer', $customer)
139
            ->setParameter('states', [OrderInterface::STATE_CART, OrderInterface::STATE_CANCELLED])
140
            ->getQuery()
141
            ->getSingleScalarResult()
142
        ;
143
    }
144
145
    /**
146
     * {@inheritdoc}
147
     */
148
    public function findOneByNumberAndCustomer($number, CustomerInterface $customer)
149
    {
150
        return $this->createQueryBuilder('o')
151
            ->andWhere('o.customer = :customer')
152
            ->andWhere('o.number = :number')
153
            ->setParameter('customer', $customer)
154
            ->setParameter('number', $number)
155
            ->getQuery()
156
            ->getOneOrNullResult()
157
        ;
158
    }
159
160
    /**
161
     * {@inheritdoc}
162
     */
163
    public function findCartByChannel($id, ChannelInterface $channel)
164
    {
165
        return $this->createQueryBuilder('o')
166
            ->andWhere('o.id = :id')
167
            ->andWhere('o.state = :state')
168
            ->andWhere('o.channel = :channel')
169
            ->setParameter('id', $id)
170
            ->setParameter('state', OrderInterface::STATE_CART)
171
            ->setParameter('channel', $channel)
172
            ->getQuery()
173
            ->getOneOrNullResult()
174
        ;
175
    }
176
177
    /**
178
     * {@inheritdoc}
179
     */
180
    public function findLatestCartByChannelAndCustomer(ChannelInterface $channel, CustomerInterface $customer)
181
    {
182
        return $this->createQueryBuilder('o')
183
            ->andWhere('o.state = :state')
184
            ->andWhere('o.channel = :channel')
185
            ->andWhere('o.customer = :customer')
186
            ->setParameter('state', OrderInterface::STATE_CART)
187
            ->setParameter('channel', $channel)
188
            ->setParameter('customer', $customer)
189
            ->addOrderBy('o.createdAt', 'DESC')
190
            ->setMaxResults(1)
191
            ->getQuery()
192
            ->getOneOrNullResult()
193
        ;
194
    }
195
196
    /**
197
     * {@inheritdoc}
198
     */
199
    public function getTotalSalesForChannel(ChannelInterface $channel)
200
    {
201
        return (int) $this->createQueryBuilder('o')
202
            ->select('SUM(o.total)')
203
            ->andWhere('o.channel = :channel')
204
            ->andWhere('o.state NOT IN (:states)')
205
            ->setParameter('channel', $channel)
206
            ->setParameter('states', [OrderInterface::STATE_CART, OrderInterface::STATE_CANCELLED])
207
            ->getQuery()
208
            ->getSingleScalarResult()
209
        ;
210
    }
211
212
    /**
213
     * {@inheritdoc}
214
     */
215
    public function countByChannel(ChannelInterface $channel)
216
    {
217
        return (int) $this->createQueryBuilder('o')
218
            ->select('COUNT(o.id)')
219
            ->andWhere('o.channel = :channel')
220
            ->andWhere('o.state NOT IN (:states)')
221
            ->setParameter('channel', $channel)
222
            ->setParameter('states', [OrderInterface::STATE_CART, OrderInterface::STATE_CANCELLED])
223
            ->getQuery()
224
            ->getSingleScalarResult()
225
        ;
226
    }
227
228
    /**
229
     * {@inheritdoc}
230
     */
231
    public function findLatestInChannel($count, ChannelInterface $channel)
232
    {
233
        return $this->createQueryBuilder('o')
234
            ->andWhere('o.channel = :channel')
235
            ->andWhere('o.state != :state')
236
            ->addOrderBy('o.checkoutCompletedAt', 'DESC')
237
            ->setParameter('channel', $channel)
238
            ->setParameter('state', OrderInterface::STATE_CART)
239
            ->setMaxResults($count)
240
            ->getQuery()
241
            ->getResult()
242
        ;
243
    }
244
245
    /**
246
     * {@inheritdoc}
247
     */
248
    public function findOrdersUnpaidSince(\DateTime $terminalDate)
249
    {
250
        return $this->createQueryBuilder('o')
251
            ->where('o.checkoutState = :checkoutState')
252
            ->andWhere('o.paymentState != :paymentState')
253
            ->andWhere('o.state = :orderState')
254
            ->andWhere('o.checkoutCompletedAt < :terminalDate')
255
            ->setParameter('checkoutState', OrderCheckoutStates::STATE_COMPLETED)
256
            ->setParameter('paymentState', OrderPaymentStates::STATE_PAID)
257
            ->setParameter('orderState', OrderInterface::STATE_NEW)
258
            ->setParameter('terminalDate', $terminalDate)
259
            ->getQuery()
260
            ->getResult()
261
        ;
262
    }
263
264
    /**
265
     * {@inheritdoc}
266
     */
267
    public function findCartForSummary($id)
268
    {
269
        /** @var OrderInterface $order */
270
        $order = $this->createQueryBuilder('o')
271
            ->andWhere('o.id = :id')
272
            ->andWhere('o.state = :state')
273
            ->setParameter('id', $id)
274
            ->setParameter('state', OrderInterface::STATE_CART)
275
            ->getQuery()
276
            ->getOneOrNullResult()
277
        ;
278
279
        if (null === $order) {
280
            return null;
281
        }
282
283
        $this->associationHydrator->hydrateAssociations($order, [
284
            'adjustments',
285
            'items',
286
            'items.adjustments',
287
            'items.units',
288
            'items.units.adjustments',
289
            'items.variant.optionValues',
290
            'items.variant.optionValues.translations',
291
            'items.variant.product',
292
            'items.variant.product.translations',
293
            'items.variant.product.images',
294
            'items.variant.product.options',
295
            'items.variant.product.options.translations',
296
        ]);
297
298
        return $order;
299
    }
300
301
    /**
302
     * {@inheritdoc}
303
     */
304
    public function findCartForAddressing($id)
305
    {
306
        /** @var OrderInterface $order */
307
        $order = $this->createQueryBuilder('o')
308
            ->andWhere('o.id = :id')
309
            ->andWhere('o.state = :state')
310
            ->setParameter('id', $id)
311
            ->setParameter('state', OrderInterface::STATE_CART)
312
            ->getQuery()
313
            ->getOneOrNullResult()
314
        ;
315
316
        if (null === $order) {
317
            return null;
318
        }
319
320
        $this->associationHydrator->hydrateAssociations($order, [
321
            'items',
322
            'items.variant.product',
323
            'items.variant.product.translations',
324
        ]);
325
326
        return $order;
327
    }
328
329
    /**
330
     * {@inheritdoc}
331
     */
332
    public function findCartForSelectShipping($id)
333
    {
334
        /** @var OrderInterface $order */
335
        $order = $this->createQueryBuilder('o')
336
            ->andWhere('o.id = :id')
337
            ->andWhere('o.state = :state')
338
            ->setParameter('id', $id)
339
            ->setParameter('state', OrderInterface::STATE_CART)
340
            ->getQuery()
341
            ->getOneOrNullResult()
342
        ;
343
344
        if (null === $order) {
345
            return null;
346
        }
347
348
        $this->associationHydrator->hydrateAssociations($order, [
349
            'items',
350
            'items.variant.product',
351
            'items.variant.product.translations',
352
        ]);
353
354
        return $order;
355
    }
356
357
    /**
358
     * {@inheritdoc}
359
     */
360
    public function findCartForSelectPayment($id)
361
    {
362
        /** @var OrderInterface $order */
363
        $order = $this->createQueryBuilder('o')
364
            ->andWhere('o.id = :id')
365
            ->andWhere('o.state = :state')
366
            ->setParameter('id', $id)
367
            ->setParameter('state', OrderInterface::STATE_CART)
368
            ->getQuery()
369
            ->getOneOrNullResult()
370
        ;
371
372
        if (null === $order) {
373
            return null;
374
        }
375
376
        $this->associationHydrator->hydrateAssociations($order, [
377
            'items',
378
            'items.variant.product',
379
            'items.variant.product.translations',
380
        ]);
381
382
        return $order;
383
    }
384
}
385