Failed Conditions
Pull Request — experimental/3.1 (#2630)
by Kiyotaka
60:01
created

CartService::removeProduct()   B

Complexity

Conditions 5
Paths 7

Size

Total Lines 29
Code Lines 20

Duplication

Lines 9
Ratio 31.03 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
cc 5
eloc 20
nc 7
nop 1
dl 9
loc 29
ccs 0
cts 0
cp 0
crap 30
rs 8.439
c 0
b 0
f 0
1
<?php
2
/*
3
 * This file is part of EC-CUBE
4
 *
5
 * Copyright(c) 2000-2015 LOCKON CO.,LTD. All Rights Reserved.
6
 *
7
 * http://www.lockon.co.jp/
8
 *
9
 * This program is free software; you can redistribute it and/or
10
 * modify it under the terms of the GNU General Public License
11
 * as published by the Free Software Foundation; either version 2
12
 * of the License, or (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU General Public License
20
 * along with this program; if not, write to the Free Software
21
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22
 */
23
24
25
namespace Eccube\Service;
26
27
use Doctrine\ORM\EntityManager;
28
use Eccube\Annotation\Inject;
29
use Eccube\Annotation\Service;
30
use Eccube\Entity\Cart;
31
use Eccube\Entity\CartItem;
32
use Eccube\Entity\ItemHolderInterface;
33
use Eccube\Entity\ProductClass;
34
use Eccube\Repository\ProductClassRepository;
35
use Eccube\Service\Cart\CartItemAllocator;
36
use Eccube\Service\Cart\CartItemComparator;
37
use Symfony\Component\HttpFoundation\Session\Session;
38
39
/**
40
 * @Service
41
 */
42
class CartService
43
{
44
    /**
45
     * @var Session
46
     * @Inject("session")
47
     */
48
    protected $session;
49
50
    /**
51
     * @var EntityManager
52
     * @Inject("orm.em")
53
     */
54
    protected $em;
55
56
    /**
57
     * @var ItemHolderInterface
58
     * @deprecated
59
     */
60
    protected $cart;
61 86
62
    /**
63 86
     * @var ProductClassRepository
64 86
     * @Inject(ProductClassRepository::class)
65 86
     */
66
    protected $productClassRepository;
67
68 86
    /**
69
     * @var CartItemComparator
70
     * @Inject(CartItemComparator::class)
71 86
     */
72
    protected $cartItemComparator;
73
74 86
    /**
75
     * @var CartItemAllocator
76
     * @Inject(CartItemAllocator::class)
77
     */
78
    protected $cartItemAllocator;
79
80
    /**
81
     * @var Cart[]
82 34
     */
83
    protected $carts;
84 34
85 19
    public function getCarts()
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
86 19
    {
87 19
        if (is_null($this->carts)) {
88 19
            $this->carts = $this->session->get('carts', []);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->session->get('carts', array()) of type * is incompatible with the declared type array<integer,object<Eccube\Entity\Cart>> of property $carts.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
89 19
            $this->loadItems();
90
        }
91
        return $this->carts;
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
92
    }
93 34
94 34
    /**
95
     * @return ItemHolderInterface|Cart
96
     */
97 34
    public function getCart()
98 34
    {
99
        $Carts = $this->getCarts();
100
        if (!$Carts) {
101
            if (!$this->cart) {
0 ignored issues
show
Deprecated Code introduced by
The property Eccube\Service\CartService::$cart has been deprecated.

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
102 34
                $this->cart = new Cart();
0 ignored issues
show
Documentation Bug introduced by
It seems like new \Eccube\Entity\Cart() of type object<Eccube\Entity\Cart> is incompatible with the declared type object<Eccube\Entity\ItemHolderInterface> of property $cart.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
Deprecated Code introduced by
The property Eccube\Service\CartService::$cart has been deprecated.

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
103 34
            }
104
            return $this->cart;
0 ignored issues
show
Deprecated Code introduced by
The property Eccube\Service\CartService::$cart has been deprecated.

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
introduced by
Missing blank line before return statement
Loading history...
105 34
        }
106 3
        return current($this->getCarts());
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
107
    }
108 34
109 34
    protected function loadItems()
110 34
    {
111 34
        foreach ($this->getCarts() as $Cart) {
112 34
            /** @var CartItem $item */
113 34
            foreach ($Cart->getItems() as $item) {
114 34
                /** @var ProductClass $ProductClass */
115
                $ProductClass = $this->productClassRepository->find($item->getProductClassId());
116
                $item->setProductClass($ProductClass);
117 34
            }
118
        }
119
    }
120 2
121
    /**
122 2
     * @param CartItem[] $cartItems
123 1
     * @return CartItem[]
124 1
     */
125 1
    protected function mergeAllCartItems($cartItems = [])
126 1
    {
127 1
        /** @var CartItem[] $allCartItems */
128
        $allCartItems = $cartItems;
129
130
        foreach ($this->getCarts() as $Cart) {
131
            /** @var CartItem $item */
132
            foreach ($Cart->getItems() as $item) {
133 2
                $itemExists = false;
134 2
                foreach ($allCartItems as $itemInArray) {
135
                    // 同じ明細があればマージする
136 2
                    if ($this->cartItemComparator->compare($item, $itemInArray)) {
137
                        $itemInArray->setQuantity($itemInArray->getQuantity() + $item->getQuantity());
138
                        $itemExists = true;
139 31
                        break;
140
                    }
141 31
                }
142
                if (!$itemExists) {
143
                    $allCartItems[] = $item;
144 3
                }
145
            }
146 3
        }
147 3
148 3
        return $allCartItems;
149
    }
150
151 15
    protected function restoreCarts($cartItems)
152
    {
153 15
        /** @var Cart $Carts */
154 15
        $Carts = [];
155 15
156
        foreach ($cartItems as $item) {
157
            $cartId = $this->cartItemAllocator->allocate($item);
158
            if (isset($Carts[$cartId])) {
159
                $Carts[$cartId]->addCartItem($item);
160
            } else {
161 22
                $Cart = new Cart();
162
                $Cart->addCartItem($item);
163 22
                $Carts[$cartId] = $Cart;
164
            }
165
        }
166
167
        $this->session->set('carts', $Carts);
168
        $this->carts = $Carts;
0 ignored issues
show
Documentation Bug introduced by
It seems like $Carts of type object<Eccube\Entity\Cart> is incompatible with the declared type array<integer,object<Eccube\Entity\Cart>> of property $carts.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
169
    }
170 12
171
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$ProductClass" missing
Loading history...
introduced by
Doc comment for parameter "$quantity" missing
Loading history...
172 12
     * カートに商品を追加します.
173
     * @param $ProductClass ProductClass 商品規格
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
174 12
     * @param $quantity int 数量
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
175
     * @return bool 商品を追加できた場合はtrue
176
     */
177
    public function addProduct($ProductClass, $quantity = 1)
0 ignored issues
show
introduced by
Declare public methods first, then protected ones and finally private ones
Loading history...
178
    {
179 View Code Duplication
        if (!$ProductClass instanceof ProductClass) {
180 12
            $ProductClassId = $ProductClass;
181
            $ProductClass = $this->em
182 12
                ->getRepository(ProductClass::class)
183
                ->find($ProductClassId);
184
            if (is_null($ProductClass)) {
185
                return false;
186
            }
187
        }
188 22
189
        $ClassCategory1 = $ProductClass->getClassCategory1();
190 22
        if ($ClassCategory1 && !$ClassCategory1->isVisible()) {
191 22
            return false;
192 22
        }
193 22
        $ClassCategory2 = $ProductClass->getClassCategory2();
194
        if ($ClassCategory2 && !$ClassCategory2->isVisible()) {
195 22
            return false;
196
        }
197
198
        $newItem = new CartItem();
199
        $newItem->setQuantity($quantity);
200
        $newItem->setPrice($ProductClass->getPrice01IncTax());
201
        $newItem->setProductClass($ProductClass);
202
203
        $allCartItems = $this->mergeAllCartItems([$newItem]);
204
        $this->restoreCarts($allCartItems);
205
206
207
        return true;
208
    }
209
210
    public function removeProduct($ProductClass)
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
211
    {
212 View Code Duplication
        if (!$ProductClass instanceof ProductClass) {
213
            $ProductClassId = $ProductClass;
214
            $ProductClass = $this->em
215
                ->getRepository(ProductClass::class)
216
                ->find($ProductClassId);
217
            if (is_null($ProductClass)) {
218
                return false;
219
            }
220
        }
221
222
        $removeItem = new CartItem();
223
        $removeItem->setPrice($ProductClass->getPrice01IncTax());
224
        $removeItem->setProductClass($ProductClass);
225
226
        $allCartItems = $this->mergeAllCartItems();
227
        $foundIndex = -1;
228
        foreach ($allCartItems as $index=>$itemInCart) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space before "=>"; 0 found
Loading history...
Coding Style introduced by
Expected 1 space after "=>"; 0 found
Loading history...
229
            if ($this->cartItemComparator->compare($itemInCart, $removeItem)) {
230
                $foundIndex = $index;
231
                break;
232
            }
233
        }
234
        array_splice($allCartItems, $foundIndex, 1);
235
        $this->restoreCarts($allCartItems);
236
237
        return true;
238
    }
239
240
    public function save()
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
241
    {
242
        return $this->session->set('carts', $this->carts);
243
    }
244
245
    public function unlock()
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
246
    {
247
        $this->getCart()
248
            ->setLock(false)
249
            ->setPreOrderId(null);
250
    }
251
252
    public function lock()
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
253
    {
254
        $this->getCart()
255
            ->setLock(true)
256
            ->setPreOrderId(null);
257
    }
258
259
    /**
260
     * @return bool
261
     */
262
    public function isLocked()
263
    {
264
        return $this->getCart()->getLock();
265
    }
266
267
    /**
268
     * @param  string $pre_order_id
269
     * @return \Eccube\Service\CartService
270
     */
271
    public function setPreOrderId($pre_order_id)
272
    {
273
        $this->getCart()->setPreOrderId($pre_order_id);
274
275
        return $this;
276
    }
277
278
    /**
279
     * @return string
280
     */
281
    public function getPreOrderId()
282
    {
283
        return $this->getCart()->getPreOrderId();
284
    }
285
286
    /**
287
     * @return \Eccube\Service\CartService
288
     */
289
    public function clear()
290
    {
291
        $this->getCart()
292
            ->setPreOrderId(null)
293
            ->setLock(false)
294
            ->setTotalPrice(0)
295
            ->clearCartItems();
296
297
        return $this;
298
    }
299
300
    /**
301
     * @param CartItemComparator $cartItemComparator
302
     */
303
    public function setCartItemComparator($cartItemComparator)
304
    {
305
        $this->cartItemComparator = $cartItemComparator;
306
    }
307
}
308