Completed
Push — 4.0 ( 268f2c...88f012 )
by Hideki
05:48 queued 10s
created

src/Eccube/Twig/Extension/CartServiceExtension.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
/*
4
 * This file is part of EC-CUBE
5
 *
6
 * Copyright(c) EC-CUBE CO.,LTD. All Rights Reserved.
7
 *
8
 * http://www.ec-cube.co.jp/
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Eccube\Twig\Extension;
15
16
use Eccube\Entity\Cart;
17
use Eccube\Service\CartService;
18
use Twig\Extension\AbstractExtension;
19
20
class CartServiceExtension extends AbstractExtension
21
{
22
    /**
23
     * @var CartService
24
     */
25
    protected $cartService;
26
27
    public function __construct(CartService $cartService)
28
    {
29
        $this->cartService = $cartService;
30
    }
31
32
    public function getFunctions()
33
    {
34
        return [
35
            new \Twig_Function('get_cart', [$this, 'get_cart'], ['is_safe' => ['all']]),
36
            new \Twig_Function('get_all_carts', [$this, 'get_all_carts'], ['is_safe' => ['all']]),
37
            new \Twig_Function('get_carts_total_price', [$this, 'get_carts_total_price'], ['is_safe' => ['all']]),
38
            new \Twig_Function('get_carts_total_quantity', [$this, 'get_carts_total_quantity'], ['is_safe' => ['all']]),
39
        ];
40
    }
41
42
    public function get_cart()
43
    {
44
        return $this->cartService->getCart();
45
    }
46
47
    public function get_all_carts()
48
    {
49
        return $this->cartService->getCarts();
50
    }
51
52 View Code Duplication
    public function get_carts_total_price()
0 ignored issues
show
This method seems to be duplicated in your project.

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.

Loading history...
53
    {
54
        $Carts = $this->cartService->getCarts();
55
        $totalPrice = array_reduce($Carts, function ($total, Cart $Cart) {
56
            $total += $Cart->getTotalPrice();
57
58
            return $total;
59
        }, 0);
60
61
        return $totalPrice;
62
    }
63
64 View Code Duplication
    public function get_carts_total_quantity()
0 ignored issues
show
This method seems to be duplicated in your project.

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.

Loading history...
65
    {
66
        $Carts = $this->cartService->getCarts();
67
        $totalQuantity = array_reduce($Carts, function ($total, Cart $Cart) {
68
            $total += $Cart->getTotalQuantity();
69
70
            return $total;
71
        }, 0);
72
73
        return $totalQuantity;
74
    }
75
}
76