Passed
Branch master (168fd2)
by Dariusz
06:34
created

CartPresenter   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 92
Duplicated Lines 13.04 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 7
dl 12
loc 92
ccs 46
cts 46
cp 1
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 2
A totalNet() 0 4 1
A totalGross() 0 4 1
A tax() 0 4 1
A totalAfterDiscounts() 0 4 1
A shippingCost() 0 4 1
A paymentFee() 0 4 1
A toArray() 12 47 4

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php declare(strict_types=1);
2
3
/*
4
 * This file is part of the Plane\Shop package.
5
 *
6
 * (c) Dariusz Korsak <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Plane\Shop;
13
14
use Money\Money;
15
use Money\MoneyFormatter;
16
use Plane\Shop\CartInterface;
17
use Plane\Shop\CartItemInterface;
18
use Plane\Shop\Traits\CartCommon;
19
use Money\Currencies\ISOCurrencies;
20
use Plane\Shop\CartDiscountInterface;
21
use Plane\Shop\CartPresenterInterface;
22
use Money\Formatter\DecimalMoneyFormatter;
23
24
class CartPresenter implements CartPresenterInterface
25
{
26
    use CartCommon;
27
    
28
    private $cart;
29
30
    private $priceFormatter;
31
32 13
    public function __construct(CartInterface $cart, MoneyFormatter $priceFormatter = null)
33
    {
34 13
        $this->cart = $cart;
35 13
        $this->priceFormatter = $priceFormatter ?: new DecimalMoneyFormatter(new ISOCurrencies());
36 13
    }
37
38 2
    public function totalNet(): string
39
    {
40 2
        return $this->priceFormatter->format($this->cart->totalNet());
41
    }
42
43 2
    public function totalGross(): string
44
    {
45 2
        return $this->priceFormatter->format($this->cart->totalGross());
46
    }
47
    
48 2
    public function tax(): string
49
    {
50 2
        return $this->priceFormatter->format($this->cart->tax());
51
    }
52
    
53 2
    public function totalAfterDiscounts(): string
54
    {
55 2
        return $this->priceFormatter->format($this->cart->totalAfterDiscounts());
56
    }
57
    
58 2
    public function shippingCost(): string
59
    {
60 2
        return $this->priceFormatter->format($this->cart->shippingCost());
61
    }
62
    
63 2
    public function paymentFee(): string
64
    {
65 2
        return $this->priceFormatter->format($this->cart->paymentFee());
66
    }
67
68 1
    public function toArray(): array
69
    {
70 1
        $array = [];
71
        $array['items'] = array_map(function (CartItemInterface $item) {
72 1
            $itemArray = $item->toArray($this->getCurrency());
73
74
            array_walk_recursive($itemArray, function (&$value, $key) {
0 ignored issues
show
Unused Code introduced by
The parameter $key is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
75 1
                if ($value instanceof Money) {
76 1
                    $value = $this->priceFormatter->format($value);
77
                }
78 1
            });
79
80 1
            return $itemArray;
81 1
        }, $this->items());
82
        
83 1 View Code Duplication
        if (!is_null($this->getShipping())) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
84 1
            $array['shipping']['id']        = $this->getShipping()->getId();
85 1
            $array['shipping']['name']      = $this->getShipping()->getName();
86 1
            $array['shipping']['desc']      = $this->getShipping()->getDescription();
87 1
            $array['shipping']['cost']      = $this->shippingCost();
88
        }
89
        
90 1 View Code Duplication
        if (!is_null($this->getPayment())) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
91 1
            $array['payment']['id']         = $this->getPayment()->getId();
92 1
            $array['payment']['name']       = $this->getPayment()->getName();
93 1
            $array['payment']['desc']       = $this->getPayment()->getDescription();
94 1
            $array['payment']['fee']        = $this->paymentFee();
95
        }
96
        
97
        $array['discounts'] = array_map(function (CartDiscountInterface $discount) {
98
            return [
99 1
                'text'  => $discount->getDescription(),
100 1
                'price' => $this->priceFormatter->format($discount->getPriceAfterDiscount())
101
            ];
102 1
        }, $this->getDiscounts());
103
        
104 1
        $array['itemsQuantity']         = $this->itemsQuantity();
105 1
        $array['totalNet']              = $this->totalNet();
106 1
        $array['totalGross']            = $this->totalGross();
107 1
        $array['tax']                   = $this->tax();
108 1
        $array['totalWeight']           = $this->weight();
109 1
        $array['shippingCost']          = $this->shippingCost();
110 1
        $array['paymentFee']            = $this->paymentFee();
111 1
        $array['totalAfterDiscounts']   = $this->totalAfterDiscounts();
112
        
113 1
        return $array;
114
    }
115
}
116