Test Failed
Push — master ( dea459...15b84c )
by David
06:38
created

CartManager   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 262
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 74
dl 0
loc 262
rs 10
c 0
b 0
f 0
wmc 21

13 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A setCurrencyManager() 0 4 1
A getCartFromCustomer() 0 18 4
A addProductToCart() 0 25 2
A addProductToCartForm() 0 22 5
A createNewOrderFromCart() 0 15 1
A persistCart() 0 3 1
A flushCart() 0 3 1
A clearCart() 0 7 1
A emptyCart() 0 3 1
A setLangManager() 0 4 1
A createNewCart() 0 8 1
A isCartTemp() 0 3 1
1
<?php
2
3
namespace GGGGino\SkuskuCartBundle\Service;
4
5
use Doctrine\ORM\EntityManager;
6
use GGGGino\SkuskuCartBundle\Entity\CartForm;
7
use GGGGino\SkuskuCartBundle\Model\SkuskuCart;
8
use GGGGino\SkuskuCartBundle\Model\SkuskuCartProduct;
9
use GGGGino\SkuskuCartBundle\Model\SkuskuCustomerInterface;
10
use GGGGino\SkuskuCartBundle\Model\SkuskuOrder;
11
use GGGGino\SkuskuCartBundle\Model\SkuskuProductInterface;
12
use Symfony\Component\Form\Form;
13
use Symfony\Component\Form\FormInterface;
14
use Symfony\Component\HttpFoundation\Request;
15
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage;
16
use Symfony\Component\Security\Core\User\UserInterface;
17
18
class CartManager
19
{
20
    /**
21
     * @var EntityManager
22
     */
23
    private $em;
24
25
    /**
26
     * @var TokenStorage
27
     */
28
    private $tokenStorage;
29
30
    /** @var  boolean */
31
    private $handled = false;
32
33
    /**
34
     * @var bool
35
     */
36
    private $allowAnonymous;
37
38
    /**
39
     * @var CurrencyManager
40
     */
41
    private $currencyManager;
42
43
    /**
44
     * @var LangManager
45
     */
46
    private $langManager;
47
48
    /**
49
     * CartExtension constructor.
50
     * @param EntityManager $em
51
     * @param TokenStorage $tokenStorage
52
     */
53
    public function __construct(EntityManager $em, TokenStorage $tokenStorage, $allowAnonymous = true)
54
    {
55
        $this->em = $em;
56
        $this->tokenStorage = $tokenStorage;
57
        $this->allowAnonymous = $allowAnonymous;
58
    }
59
60
    /**
61
     * Get the cart from a given Customer.
62
     * If the customer is not passed, is taken from the session
63
     *
64
     * @api
65
     *
66
     * @param SkuskuCustomerInterface|null $customer
67
     * @return SkuskuCart|null
68
     */
69
    public function getCartFromCustomer(SkuskuCustomerInterface $customer = null)
70
    {
71
        if( !$customer ) {
72
            $customer = $this->tokenStorage->getToken()->getUser();
73
        }
74
75
        // If the user is anon. the arg $customer is a string, so i check it
76
        if( !($customer instanceof UserInterface) )
77
            $customer = null;
78
79
        $cart = $this->em
80
            ->getRepository('GGGGino\SkuskuCartBundle\Model\SkuskuCart')
81
            ->getOneNonOrderedCartByCustomer($customer);
0 ignored issues
show
Bug introduced by
It seems like $customer can also be of type Symfony\Component\Security\Core\User\UserInterface; however, parameter $customer of GGGGino\SkuskuCartBundle...OrderedCartByCustomer() does only seem to accept GGGGino\SkuskuCartBundle...uCustomerInterface|null, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

81
            ->getOneNonOrderedCartByCustomer(/** @scrutinizer ignore-type */ $customer);
Loading history...
82
83
        if( !$cart )
84
            $cart = $this->createNewCart($customer);
0 ignored issues
show
Bug introduced by
It seems like $customer can also be of type Symfony\Component\Security\Core\User\UserInterface; however, parameter $customer of GGGGino\SkuskuCartBundle...anager::createNewCart() does only seem to accept GGGGino\SkuskuCartBundle...uCustomerInterface|null, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

84
            $cart = $this->createNewCart(/** @scrutinizer ignore-type */ $customer);
Loading history...
85
86
        return $cart;
87
    }
88
89
    /**
90
     * Aggiungo il carrello alla coda delle entita da processare di doctrine
91
     *
92
     * @api
93
     *
94
     * @param SkuskuCart $cart
95
     */
96
    public function persistCart(SkuskuCart $cart)
97
    {
98
        $this->em->persist($cart);
99
    }
100
101
    /**
102
     * Faccio il flush del carrello
103
     *
104
     * @api
105
     *
106
     * @param SkuskuCart $cart
107
     */
108
    public function flushCart(SkuskuCart $cart)
109
    {
110
        $this->em->flush($cart);
111
    }
112
113
    /**
114
     * Empty the cart tables for development, garbage collector purposes
115
     *
116
     * @api
117
     */
118
    public function clearCart()
119
    {
120
        $this->em->createQuery('DELETE GGGGino\SkuskuCartBundle\Model\SkuskuCartProduct cp')->execute();
121
        $this->em->createQuery('DELETE GGGGino\SkuskuCartBundle\Model\SkuskuCart c')->execute();
122
        $this->em->createQuery('DELETE GGGGino\SkuskuCartBundle\Model\SkuskuPayment c')->execute();
123
        $this->em->createQuery('DELETE GGGGino\SkuskuCartBundle\Model\SkuskuPaymentToken c')->execute();
124
        $this->em->createQuery('DELETE GGGGino\SkuskuCartBundle\Model\SkuskuOrder c')->execute();
125
    }
126
127
    /**
128
     * Used from the mini form
129
     *
130
     * @param Request $request
131
     * @param FormInterface $form
132
     */
133
    public function addProductToCartForm(Request $request, FormInterface $form)
134
    {
135
        if( $this->handled )
136
            return;
137
138
        $form->handleRequest($request);
139
140
        if ($form->isSubmitted() && $form->isValid()) {
141
            $this->handled = true;
142
            /** @var integer $idProduct */
143
            $idProduct = intval($form->get('idProduct')->getData());
144
            /** @var integer $quantity */
145
            $quantity = intval($form->get('quantity')->getData());
146
            /** @var SkuskuCustomerInterface $customer */
147
            $customer = $this->tokenStorage->getToken()->getUser();
148
            /** @var SkuskuProductInterface $productReference */
149
            $productReference = $this->em->getReference(SkuskuProductInterface::class, $idProduct);
150
151
            if( !($customer instanceof UserInterface) )
152
                $customer = null;
0 ignored issues
show
Unused Code introduced by
The assignment to $customer is dead and can be removed.
Loading history...
153
154
            $this->addProductToCart($productReference, $quantity);
155
        }
156
    }
157
158
    /**
159
     * Add the product to the cart checking that:
160
     * - the cart exist
161
     * -- if it not exist, I create it
162
     * - Update the cart date
163
     * - add & update the cart
164
     *
165
     * @api
166
     *
167
     * @param SkuskuProductInterface $product
168
     * @param $quantity
169
     */
170
    public function addProductToCart(SkuskuProductInterface $product, $quantity)
171
    {
172
        /** @var SkuskuCustomerInterface $customer */
173
        $customer = $this->tokenStorage->getToken()->getUser();
174
175
        /** @var SkuskuCart $cart */
176
        $cart = $this->getCartFromCustomer($customer);
177
        $cart->setDateUpd(new \DateTime());
178
179
        $this->em->persist($cart);
180
181
        /** @var SkuskuCartProduct $productCart */
182
        if( $productCart = $cart->getProduct($product)->first() ){
183
            // @todo what?? $productCart->getQuantity() + $quantity
184
            $productCart->setQuantity($productCart->getQuantity() + $quantity);
185
        }else{
186
            $productCart = new SkuskuCartProduct();
187
            $productCart->setProduct($product);
188
            $productCart->setQuantity($quantity);
189
190
            $cart->addProduct($productCart);
191
            $this->em->persist($productCart);
192
        }
193
194
        $this->em->flush();
195
    }
196
197
    /**
198
     * Create new cart
199
     *
200
     * @api
201
     *
202
     * @param SkuskuCustomerInterface|null $customer
203
     * @return SkuskuCart
204
     */
205
    public function createNewCart(SkuskuCustomerInterface $customer = null)
206
    {
207
        $cart = new SkuskuCart();
208
        $cart->setDateAdd(new \DateTime());
209
        $cart->setCustomer($customer);
210
        $cart->setCurrency($this->currencyManager->getCurrentCurrency());
211
212
        return $cart;
213
    }
214
215
    /**
216
     * Build a new Order from a given cart.
217
     * Used for example when the payment gone good
218
     *
219
     * @api
220
     *
221
     * @param SkuskuCart $cart
222
     * @return SkuskuOrder
223
     */
224
    public function createNewOrderFromCart(SkuskuCart $cart)
225
    {
226
        $totPrice = $cart->getTotalPrice();
227
228
        $order = new SkuskuOrder();
229
        $order->setCurrency($cart->getCurrency());
230
        $order->setDateAdd(new \DateTime());
231
        $order->setCustomer($cart->getCustomer());
232
        $order->setCart($cart);
233
        $order->setLang($cart->getLang());
234
        $order->setTotalPaid($totPrice);
235
        $order->setTotalPaidReal($totPrice);
236
        $order->setTotalProducts($totPrice);
237
238
        return $order;
239
    }
240
241
    /**
242
     * @param CartForm $formData
243
     */
244
    public function emptyCart(CartForm $formData)
245
    {
246
        $formData->getCart()->emptyProducts();
247
    }
248
249
    /**
250
     * Check if the cart is temporary created or permanent
251
     *
252
     * @api
253
     *
254
     * @param SkuskuCart $cart
255
     * @return bool
256
     */
257
    public function isCartTemp(SkuskuCart $cart)
258
    {
259
        return !((bool) $cart->getId());
260
    }
261
262
    /**
263
     * @param CurrencyManager $currencyManager
264
     * @return CartManager
265
     */
266
    public function setCurrencyManager($currencyManager)
267
    {
268
        $this->currencyManager = $currencyManager;
269
        return $this;
270
    }
271
272
    /**
273
     * @param LangManager $langManager
274
     * @return CartManager
275
     */
276
    public function setLangManager($langManager)
277
    {
278
        $this->langManager = $langManager;
279
        return $this;
280
    }
281
}