Completed
Push — master ( f563c7...7369e3 )
by Kamil
20:40
created

OrderRepository   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 211
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 4
dl 0
loc 211
rs 10
c 0
b 0
f 0

13 Methods

Rating   Name   Duplication   Size   Complexity  
A createListQueryBuilder() 0 11 1
A createByCustomerIdQueryBuilder() 0 9 1
A findByCustomer() 0 7 1
A findOneForPayment() 0 13 1
A countByCustomerAndCoupon() 0 14 1
A countByCustomer() 0 12 1
A findOneByNumberAndCustomer() 0 11 1
A findCartByChannel() 0 13 1
A findLatestCartByChannelAndCustomer() 0 15 1
A getTotalSalesForChannel() 0 12 1
A countByChannel() 0 12 1
A findLatestInChannel() 0 13 1
A findOrdersUnpaidSince() 0 15 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 Sylius\Bundle\OrderBundle\Doctrine\ORM\OrderRepository as BaseOrderRepository;
15
use Sylius\Component\Core\Model\ChannelInterface;
16
use Sylius\Component\Core\Model\CustomerInterface;
17
use Sylius\Component\Core\Model\OrderInterface;
18
use Sylius\Component\Core\Model\PromotionCouponInterface;
19
use Sylius\Component\Core\OrderCheckoutStates;
20
use Sylius\Component\Core\OrderPaymentStates;
21
use Sylius\Component\Core\Repository\OrderRepositoryInterface;
22
23
class OrderRepository extends BaseOrderRepository implements OrderRepositoryInterface
24
{
25
    /**
26
     * {@inheritdoc}
27
     */
28
    public function createListQueryBuilder()
29
    {
30
        return $this->createQueryBuilder('o')
31
            ->addSelect('channel')
32
            ->addSelect('customer')
33
            ->innerJoin('o.channel', 'channel')
34
            ->leftJoin('o.customer', 'customer')
35
            ->andWhere('o.state != :state')
36
            ->setParameter('state', OrderInterface::STATE_CART)
37
        ;
38
    }
39
40
    /**
41
     * {@inheritdoc}
42
     */
43
    public function createByCustomerIdQueryBuilder($customerId)
44
    {
45
        return $this->createQueryBuilder('o')
46
            ->andWhere('o.customer = :customerId')
47
            ->andWhere('o.state != :state')
48
            ->setParameter('customerId', $customerId)
49
            ->setParameter('state', OrderInterface::STATE_CART)
50
        ;
51
    }
52
53
    /**
54
     * {@inheritdoc}
55
     */
56
    public function findByCustomer(CustomerInterface $customer)
57
    {
58
        return $this->createByCustomerIdQueryBuilder($customer->getId())
59
            ->getQuery()
60
            ->getResult()
61
        ;
62
    }
63
64
    /**
65
     * {@inheritdoc}
66
     */
67
    public function findOneForPayment($id)
68
    {
69
        return $this->createQueryBuilder('o')
70
            ->addSelect('payments')
71
            ->addSelect('paymentMethods')
72
            ->leftJoin('o.payments', 'payments')
73
            ->leftJoin('payments.method', 'paymentMethods')
74
            ->andWhere('o.id = :id')
75
            ->setParameter('id', $id)
76
            ->getQuery()
77
            ->getOneOrNullResult()
78
        ;
79
    }
80
81
    /**
82
     * {@inheritdoc}
83
     */
84
    public function countByCustomerAndCoupon(CustomerInterface $customer, PromotionCouponInterface $coupon)
85
    {
86
        return (int) $this->createQueryBuilder('o')
87
            ->select('COUNT(o.id)')
88
            ->andWhere('o.customer = :customer')
89
            ->andWhere('o.promotionCoupon = :coupon')
90
            ->andWhere('o.state NOT IN (:states)')
91
            ->setParameter('customer', $customer)
92
            ->setParameter('coupon', $coupon)
93
            ->setParameter('states', [OrderInterface::STATE_CART, OrderInterface::STATE_CANCELLED])
94
            ->getQuery()
95
            ->getSingleScalarResult()
96
        ;
97
    }
98
99
    /**
100
     * {@inheritdoc}
101
     */
102
    public function countByCustomer(CustomerInterface $customer)
103
    {
104
        return (int) $this->createQueryBuilder('o')
105
            ->select('COUNT(o.id)')
106
            ->andWhere('o.customer = :customer')
107
            ->andWhere('o.state NOT IN (:states)')
108
            ->setParameter('customer', $customer)
109
            ->setParameter('states', [OrderInterface::STATE_CART, OrderInterface::STATE_CANCELLED])
110
            ->getQuery()
111
            ->getSingleScalarResult()
112
        ;
113
    }
114
115
    /**
116
     * {@inheritdoc}
117
     */
118
    public function findOneByNumberAndCustomer($number, CustomerInterface $customer)
119
    {
120
        return $this->createQueryBuilder('o')
121
            ->andWhere('o.customer = :customer')
122
            ->andWhere('o.number = :number')
123
            ->setParameter('customer', $customer)
124
            ->setParameter('number', $number)
125
            ->getQuery()
126
            ->getOneOrNullResult()
127
        ;
128
    }
129
130
    /**
131
     * {@inheritdoc}
132
     */
133
    public function findCartByChannel($id, ChannelInterface $channel)
134
    {
135
        return $this->createQueryBuilder('o')
136
            ->andWhere('o.id = :id')
137
            ->andWhere('o.state = :state')
138
            ->andWhere('o.channel = :channel')
139
            ->setParameter('id', $id)
140
            ->setParameter('state', OrderInterface::STATE_CART)
141
            ->setParameter('channel', $channel)
142
            ->getQuery()
143
            ->getOneOrNullResult()
144
        ;
145
    }
146
147
    /**
148
     * {@inheritdoc}
149
     */
150
    public function findLatestCartByChannelAndCustomer(ChannelInterface $channel, CustomerInterface $customer)
151
    {
152
        return $this->createQueryBuilder('o')
153
            ->andWhere('o.state = :state')
154
            ->andWhere('o.channel = :channel')
155
            ->andWhere('o.customer = :customer')
156
            ->setParameter('state', OrderInterface::STATE_CART)
157
            ->setParameter('channel', $channel)
158
            ->setParameter('customer', $customer)
159
            ->addOrderBy('o.createdAt', 'DESC')
160
            ->setMaxResults(1)
161
            ->getQuery()
162
            ->getOneOrNullResult()
163
        ;
164
    }
165
166
    /**
167
     * {@inheritdoc}
168
     */
169
    public function getTotalSalesForChannel(ChannelInterface $channel)
170
    {
171
        return (int) $this->createQueryBuilder('o')
172
            ->select('SUM(o.total)')
173
            ->andWhere('o.channel = :channel')
174
            ->andWhere('o.state NOT IN (:states)')
175
            ->setParameter('channel', $channel)
176
            ->setParameter('states', [OrderInterface::STATE_CART, OrderInterface::STATE_CANCELLED])
177
            ->getQuery()
178
            ->getSingleScalarResult()
179
        ;
180
    }
181
182
    /**
183
     * {@inheritdoc}
184
     */
185
    public function countByChannel(ChannelInterface $channel)
186
    {
187
        return (int) $this->createQueryBuilder('o')
188
            ->select('COUNT(o.id)')
189
            ->andWhere('o.channel = :channel')
190
            ->andWhere('o.state NOT IN (:states)')
191
            ->setParameter('channel', $channel)
192
            ->setParameter('states', [OrderInterface::STATE_CART, OrderInterface::STATE_CANCELLED])
193
            ->getQuery()
194
            ->getSingleScalarResult()
195
        ;
196
    }
197
198
    /**
199
     * {@inheritdoc}
200
     */
201
    public function findLatestInChannel($count, ChannelInterface $channel)
202
    {
203
        return $this->createQueryBuilder('o')
204
            ->andWhere('o.channel = :channel')
205
            ->andWhere('o.state != :state')
206
            ->addOrderBy('o.checkoutCompletedAt', 'DESC')
207
            ->setParameter('channel', $channel)
208
            ->setParameter('state', OrderInterface::STATE_CART)
209
            ->setMaxResults($count)
210
            ->getQuery()
211
            ->getResult()
212
        ;
213
    }
214
215
    /**
216
     * {@inheritdoc}
217
     */
218
    public function findOrdersUnpaidSince(\DateTime $terminalDate)
219
    {
220
        return $this->createQueryBuilder('o')
221
            ->where('o.checkoutState = :checkoutState')
222
            ->andWhere('o.paymentState != :paymentState')
223
            ->andWhere('o.state = :orderState')
224
            ->andWhere('o.checkoutCompletedAt < :terminalDate')
225
            ->setParameter('checkoutState', OrderCheckoutStates::STATE_COMPLETED)
226
            ->setParameter('paymentState', OrderPaymentStates::STATE_PAID)
227
            ->setParameter('orderState', OrderInterface::STATE_NEW)
228
            ->setParameter('terminalDate', $terminalDate)
229
            ->getQuery()
230
            ->getResult()
231
        ;
232
    }
233
}
234