Completed
Push — symfony3-wololo ( 5670c9...50a832 )
by Kamil
20:37
created

OrderRepository   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 190
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 5
dl 0
loc 190
rs 10
c 0
b 0
f 0

11 Methods

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