Completed
Push — checkout-optimisation ( adf3e5...4a6bfb )
by Kamil
51:33 queued 29:45
created

OrderRepository::findCartForSelectingShipping()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 20
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 13
nc 1
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
        $this->associationHydrator->hydrateAssociations($order, [
280
            'adjustments',
281
            'items',
282
            'items.adjustments',
283
            'items.units',
284
            'items.units.adjustments',
285
            'items.variant.optionValues',
286
            'items.variant.optionValues.translations',
287
            'items.variant.product',
288
            'items.variant.product.translations',
289
            'items.variant.product.images',
290
            'items.variant.product.options',
291
            'items.variant.product.options.translations',
292
        ]);
293
294
        return $order;
295
    }
296
297
    /**
298
     * {@inheritdoc}
299
     */
300
    public function findCartForAddressing($id)
301
    {
302
        /** @var OrderInterface $order */
303
        $order = $this->createQueryBuilder('o')
304
            ->andWhere('o.id = :id')
305
            ->andWhere('o.state = :state')
306
            ->setParameter('id', $id)
307
            ->setParameter('state', OrderInterface::STATE_CART)
308
            ->getQuery()
309
            ->getOneOrNullResult()
310
        ;
311
312
        $this->associationHydrator->hydrateAssociations($order, [
313
            'items',
314
            'items.variant.product',
315
            'items.variant.product.translations',
316
        ]);
317
318
        return $order;
319
    }
320
321
    /**
322
     * {@inheritdoc}
323
     */
324
    public function findCartForSelectingShipping($id)
325
    {
326
        /** @var OrderInterface $order */
327
        $order = $this->createQueryBuilder('o')
328
            ->andWhere('o.id = :id')
329
            ->andWhere('o.state = :state')
330
            ->setParameter('id', $id)
331
            ->setParameter('state', OrderInterface::STATE_CART)
332
            ->getQuery()
333
            ->getOneOrNullResult()
334
        ;
335
336
        $this->associationHydrator->hydrateAssociations($order, [
337
            'items',
338
            'items.variant.product',
339
            'items.variant.product.translations',
340
        ]);
341
342
        return $order;
343
    }
344
345
    /**
346
     * {@inheritdoc}
347
     */
348
    public function findCartForSelectingPayment($id)
349
    {
350
        /** @var OrderInterface $order */
351
        $order = $this->createQueryBuilder('o')
352
            ->andWhere('o.id = :id')
353
            ->andWhere('o.state = :state')
354
            ->setParameter('id', $id)
355
            ->setParameter('state', OrderInterface::STATE_CART)
356
            ->getQuery()
357
            ->getOneOrNullResult()
358
        ;
359
360
        $this->associationHydrator->hydrateAssociations($order, [
361
            'items',
362
            'items.variant.product',
363
            'items.variant.product.translations',
364
        ]);
365
366
        return $order;
367
    }
368
}
369