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

EverySecondItemFreeDiscount   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 24
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 1
dl 0
loc 24
ccs 10
cts 10
cp 1
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A applyDiscount() 0 16 3
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\Discount;
13
14
use Plane\Shop\CartDiscountAbstract;
15
16
class EverySecondItemFreeDiscount extends CartDiscountAbstract
17
{
18
    protected $items;
19
    
20
    /**
21
     * Apply discount so that every even item price is set to 0
22
     */
23 1
    protected function applyDiscount(): void
24
    {
25 1
        $subctracts = [];
26 1
        $i = 1;
27 1
        foreach ($this->items as $item) {
28
            // every even item is free
29 1
            if ($i % 2 == 0) {
30 1
                $subctracts[] = $item->getPriceTotalWithTax($this->currency);
31
            }
32 1
            $i++;
33
        }
34
35 1
        $afterDiscount = call_user_func_array([$this->price, 'subtract'], $subctracts);
36
37 1
        $this->priceAfterDiscount = $afterDiscount;
38 1
    }
39
}
40