Failed Conditions
Push — experimental/3.1 ( 965511...751c7a )
by chihiro
21s
created

CartService::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 2
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
ccs 0
cts 0
cp 0
crap 2
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\Entity\Cart;
29
use Eccube\Entity\CartItem;
30
use Eccube\Entity\ItemHolderInterface;
31
use Eccube\Entity\ProductClass;
32
use Symfony\Component\HttpFoundation\Session\Session;
33
34
class CartService
0 ignored issues
show
introduced by
Missing class doc comment
Loading history...
35
{
36
    /**
37
     * @var Session
38
     */
39
    protected $session;
40
41
    /**
42
     * @var EntityManager
43
     */
44
    protected $em;
45
46
    /**
47
     * @var ItemHolderInterface
48
     */
49
    protected $cart;
50
51
    public function __construct(Session $session, EntityManager $em)
0 ignored issues
show
Bug introduced by
You have injected the EntityManager via parameter $em. This is generally not recommended as it might get closed and become unusable. Instead, it is recommended to inject the ManagerRegistry and retrieve the EntityManager via getManager() each time you need it.

The EntityManager might become unusable for example if a transaction is rolled back and it gets closed. Let’s assume that somewhere in your application, or in a third-party library, there is code such as the following:

function someFunction(ManagerRegistry $registry) {
    $em = $registry->getManager();
    $em->getConnection()->beginTransaction();
    try {
        // Do something.
        $em->getConnection()->commit();
    } catch (\Exception $ex) {
        $em->getConnection()->rollback();
        $em->close();

        throw $ex;
    }
}

If that code throws an exception and the EntityManager is closed. Any other code which depends on the same instance of the EntityManager during this request will fail.

On the other hand, if you instead inject the ManagerRegistry, the getManager() method guarantees that you will always get a usable manager instance.

Loading history...
introduced by
Missing function doc comment
Loading history...
52
    {
53
        $this->session = $session;
54
        $this->cart = $session->get('cart', new Cart());
55
        $this->em = $em;
56
57
        $this->loadItems();
58
    }
59
60
    /**
61
     * @return ItemHolderInterface|Cart
62
     */
63
    public function getCart()
64
    {
65
        return $this->cart;
66
    }
67
68
    protected function loadItems()
69
    {
70
        /** @var CartItem $item */
71
        foreach ($this->cart->getItems() as $item) {
72
            $id = $item->getClassId();
73
            $class = $item->getClassName();
74
            $entity = $this->em->getRepository($class)->find($id);
75
            $item->setObject($entity);
76
        }
77 182
    }
78
79 182
    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...
introduced by
Missing function doc comment
Loading history...
80 182
    {
81 182 View Code Duplication
        if (!$ProductClass instanceof ProductClass) {
82
            $ProductClassId = $ProductClass;
83 182
            $ProductClass = $this->em
84
                ->getRepository(ProductClass::class)
85
                ->find($ProductClassId);
86 182
            if (is_null($ProductClass)) {
87
                return false;
88
            }
89 182
        }
90
91 182
        /** @var Cart $cart */
92
        $cart = $this->cart;
93
        $exists = $cart->getCartItemByIdentifier(ProductClass::class, $ProductClass->getId());
94
95
        if ($exists) {
96
            $exists->setQuantity($exists->getQuantity() + $quantity);
97
        } else {
98
            $item = new CartItem();
99
            $item->setQuantity($quantity);
100
            $item->setPrice($ProductClass->getPrice01IncTax());
101
            $item->setClassId($ProductClass->getId());
102
            $item->setClassName(ProductClass::class);
103
            $item->setObject($ProductClass);
104
            $cart->addItem($item);
105
        }
106
107
        return true;
108
    }
109
110
    public function removeProduct($ProductClass)
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
111
    {
112 View Code Duplication
        if (!$ProductClass instanceof ProductClass) {
113
            $ProductClassId = $ProductClass;
114
            $ProductClass = $this->em
115
                ->getRepository(ProductClass::class)
116
                ->find($ProductClassId);
117
            if (is_null($ProductClass)) {
118 48
                return false;
119
            }
120
        }
121 48
122 48
        /** @var Cart $cart */
123 48
        $cart = $this->cart;
124
        $cart->removeCartItemByIdentifier(ProductClass::class, $ProductClass->getId());
125 48
126
        return true;
127 48
    }
128
129
    public function save()
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
130
    {
131
        return $this->session->set('cart', $this->cart);
132 88
    }
133
134 88
    public function unlock()
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
135 88
    {
136
        $this->cart
137
            ->setLock(false)
138 88
            ->setPreOrderId(null);
139
    }
140
141 82
    public function lock()
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
142
    {
143 82
        $this->cart
144
            ->setLock(true)
145
            ->setPreOrderId(null);
146 4
    }
147
148 4
    /**
149 4
     * @return bool
150 4
     */
151
    public function isLocked()
152
    {
153 38
        return $this->cart->getLock();
154
    }
155 38
156 38
    /**
157 38
     * @param  string $pre_order_id
158
     * @return \Eccube\Service\CartService
159
     */
160
    public function setPreOrderId($pre_order_id)
161
    {
162
        $this->cart->setPreOrderId($pre_order_id);
163 45
164
        return $this;
165 45
    }
166
167
    /**
168
     * @return string
169
     */
170
    public function getPreOrderId()
171
    {
172 20
        return $this->cart->getPreOrderId();
173
    }
174 20
175
    /**
176 20
     * @return \Eccube\Service\CartService
177
     */
178
    public function clear()
179
    {
180
        $this->cart
181
            ->setPreOrderId(null)
182 39
            ->setLock(false)
183
            ->clearCartItems();
184 39
185
        return $this;
186
    }
187
}
188