Failed Conditions
Push — experimental/3.1 ( ea436b...26b8cd )
by chihiro
244:59 queued 239:01
created

CartService::setPreOrderId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 6
ccs 0
cts 0
cp 0
crap 2
rs 9.4285
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 = [];
129
130
        foreach ($this->getCarts() as $Cart) {
131
            $allCartItems = $this->mergeCartitems($Cart->getCartItems(), $allCartItems);
132
        }
133 2
134 2
        return $this->mergeCartitems($cartItems, $allCartItems);
135
    }
136 2
137
    /**
138
     * @param $cartItems
139 31
     * @param $allCartItems
140
     * @return array
141 31
     */
142
    protected function mergeCartitems($cartItems, $allCartItems)
143
    {
144 3
        foreach ($cartItems as $item) {
145
            $itemExists = false;
146 3
            foreach ($allCartItems as $itemInArray) {
147 3
                // 同じ明細があればマージする
148 3
                if ($this->cartItemComparator->compare($item, $itemInArray)) {
149
                    $itemInArray->setQuantity($itemInArray->getQuantity() + $item->getQuantity());
150
                    $itemExists = true;
151 15
                    break;
152
                }
153 15
            }
154 15
            if (!$itemExists) {
155 15
                $allCartItems[] = $item;
156
            }
157
        }
158
        return $allCartItems;
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
159
    }
160
161 22
    protected function restoreCarts($cartItems)
162
    {
163 22
        /** @var Cart $Carts */
164
        $Carts = [];
165
166
        foreach ($cartItems as $item) {
167
            $cartId = $this->cartItemAllocator->allocate($item);
168
            if (isset($Carts[$cartId])) {
169
                $Carts[$cartId]->addCartItem($item);
170 12
            } else {
171
                $Cart = new Cart();
172 12
                $Cart->addCartItem($item);
173
                $Carts[$cartId] = $Cart;
174 12
            }
175
        }
176
177
        $this->session->set('carts', $Carts);
178
        // 配列のkeyを0からにする
179
        $this->carts = array_values($Carts);
180 12
    }
181
182 12
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$ProductClass" missing
Loading history...
introduced by
Doc comment for parameter "$quantity" missing
Loading history...
183
     * カートに商品を追加します.
184
     * @param $ProductClass ProductClass 商品規格
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
185
     * @param $quantity int 数量
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
186
     * @return bool 商品を追加できた場合はtrue
187
     */
188 22
    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...
189
    {
190 22 View Code Duplication
        if (!$ProductClass instanceof ProductClass) {
191 22
            $ProductClassId = $ProductClass;
192 22
            $ProductClass = $this->em
193 22
                ->getRepository(ProductClass::class)
194
                ->find($ProductClassId);
195 22
            if (is_null($ProductClass)) {
196
                return false;
197
            }
198
        }
199
200
        $ClassCategory1 = $ProductClass->getClassCategory1();
201
        if ($ClassCategory1 && !$ClassCategory1->isVisible()) {
202
            return false;
203
        }
204
        $ClassCategory2 = $ProductClass->getClassCategory2();
205
        if ($ClassCategory2 && !$ClassCategory2->isVisible()) {
206
            return false;
207
        }
208
209
        $newItem = new CartItem();
210
        $newItem->setQuantity($quantity);
211
        $newItem->setPrice($ProductClass->getPrice01IncTax());
212
        $newItem->setProductClass($ProductClass);
213
214
        $allCartItems = $this->mergeAllCartItems([$newItem]);
215
        $this->restoreCarts($allCartItems);
216
217
218
        return true;
219
    }
220
221
    public function removeProduct($ProductClass)
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
222
    {
223 View Code Duplication
        if (!$ProductClass instanceof ProductClass) {
224
            $ProductClassId = $ProductClass;
225
            $ProductClass = $this->em
226
                ->getRepository(ProductClass::class)
227
                ->find($ProductClassId);
228
            if (is_null($ProductClass)) {
229
                return false;
230
            }
231
        }
232
233
        $removeItem = new CartItem();
234
        $removeItem->setPrice($ProductClass->getPrice01IncTax());
235
        $removeItem->setProductClass($ProductClass);
236
237
        $allCartItems = $this->mergeAllCartItems();
238
        $foundIndex = -1;
239
        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...
240
            if ($this->cartItemComparator->compare($itemInCart, $removeItem)) {
241
                $foundIndex = $index;
242
                break;
243
            }
244
        }
245
        array_splice($allCartItems, $foundIndex, 1);
246
        $this->restoreCarts($allCartItems);
247
248
        return true;
249
    }
250
251
    public function save()
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
252
    {
253
        return $this->session->set('carts', $this->carts);
254
    }
255
256
    public function unlock()
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
257
    {
258
        $this->getCart()
259
            ->setLock(false)
260
            ->setPreOrderId(null);
261
    }
262
263
    public function lock()
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
264
    {
265
        $this->getCart()
266
            ->setLock(true)
267
            ->setPreOrderId(null);
268
    }
269
270
    /**
271
     * @return bool
272
     */
273
    public function isLocked()
274
    {
275
        return $this->getCart()->getLock();
276
    }
277
278
    /**
279
     * @param  string $pre_order_id
280
     * @return \Eccube\Service\CartService
281
     */
282
    public function setPreOrderId($pre_order_id)
283
    {
284
        $this->getCart()->setPreOrderId($pre_order_id);
285
286
        return $this;
287
    }
288
289
    /**
290
     * @return string
291
     */
292
    public function getPreOrderId()
293
    {
294
        return $this->getCart()->getPreOrderId();
295
    }
296
297
    /**
298
     * @return \Eccube\Service\CartService
299
     */
300
    public function clear()
301
    {
302
        $Carts = $this->getCarts();
303
        $removed = array_splice($Carts, 0, 1);
304
        if (!empty($removed)) {
305
            $removedCart = $removed[0];
306
            $removedCart->setPreOrderId(null)
307
                ->setLock(false)
308
                ->setTotalPrice(0)
309
                ->clearCartItems();
310
        }
311
        $this->carts = $Carts;
312
313
        return $this;
314
    }
315
316
    /**
317
     * @param CartItemComparator $cartItemComparator
318
     */
319
    public function setCartItemComparator($cartItemComparator)
320
    {
321
        $this->cartItemComparator = $cartItemComparator;
322
    }
323
324
    /**
325
     * 指定したインデックスにあるカートを優先にする
326
     * @param int $index カートのインデックス
327
     */
328
    public function setPrimary($index = 0)
329
    {
330
        $Carts = $this->getCarts();
331
        $primary = $Carts[$index];
332
        $prev = $Carts[0];
333
        array_splice($Carts, 0, 1, [$primary]);
334
        array_splice($Carts, $index, 1, [$prev]);
335
        $this->carts = $Carts;
336
        $this->save();
337
    }
338
}
339