1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Eccube\Twig\Extension; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
use Eccube\Entity\Cart; |
7
|
|
|
use Eccube\Service\CartService; |
8
|
|
|
use Twig\Extension\AbstractExtension; |
9
|
|
|
|
10
|
|
|
class CartServiceExtension extends AbstractExtension |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var CartService |
14
|
|
|
*/ |
15
|
|
|
protected $cartService; |
16
|
|
|
|
17
|
|
|
public function __construct(CartService $cartService) |
18
|
|
|
{ |
19
|
|
|
$this->cartService = $cartService; |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
public function getFunctions() |
23
|
|
|
{ |
24
|
|
|
return [ |
25
|
|
|
new \Twig_Function('get_cart', [$this, 'get_cart'], ['is_safe' => ['all']]), |
26
|
|
|
new \Twig_Function('get_all_carts', [$this, 'get_all_carts'], ['is_safe' => ['all']]), |
27
|
|
|
new \Twig_Function('get_carts_total_price', [$this, 'get_carts_total_price'], ['is_safe' => ['all']]), |
28
|
|
|
new \Twig_Function('get_carts_total_quantity', [$this, 'get_carts_total_quantity'], ['is_safe' => ['all']]), |
29
|
|
|
]; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public function get_cart() |
33
|
|
|
{ |
34
|
|
|
return $this->cartService->getCart(); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function get_all_carts() |
38
|
|
|
{ |
39
|
|
|
return $this->cartService->getCarts(); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
View Code Duplication |
public function get_carts_total_price() |
|
|
|
|
43
|
|
|
{ |
44
|
|
|
$Carts = $this->cartService->getCarts(); |
45
|
|
|
$totalPrice = array_reduce($Carts, function ($total, Cart $Cart) { |
46
|
|
|
$total += $Cart->getTotalPrice(); |
47
|
|
|
|
48
|
|
|
return $total; |
49
|
|
|
}, 0); |
50
|
|
|
|
51
|
|
|
return $totalPrice; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
View Code Duplication |
public function get_carts_total_quantity() |
|
|
|
|
55
|
|
|
{ |
56
|
|
|
$Carts = $this->cartService->getCarts(); |
57
|
|
|
$totalQuantity = array_reduce($Carts, function ($total, Cart $Cart) { |
58
|
|
|
$total += $Cart->getTotalQuantity(); |
59
|
|
|
|
60
|
|
|
return $total; |
61
|
|
|
}, 0); |
62
|
|
|
|
63
|
|
|
return $totalQuantity; |
64
|
|
|
} |
65
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.