Passed
Push — master ( e39bb0...54e3cc )
by Dmitry
14:48
created

PriceFactory::createProgressivePrice()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
rs 10
ccs 0
cts 0
cp 0
cc 1
nc 1
nop 1
crap 2
1
<?php
2
/**
3
 * PHP Billing Library
4
 *
5
 * @link      https://github.com/hiqdev/php-billing
6
 * @package   php-billing
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2017-2020, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hiqdev\php\billing\price;
12
13
/**
14
 * Default price factory.
15
 *
16
 * @author Andrii Vasyliev <[email protected]>
17
 */
18
class PriceFactory implements PriceFactoryInterface
19
{
20
    protected $creators = [
21
        EnumPrice::class    => 'createEnumPrice',
22
        RatePrice::class    => 'createRatePrice',
23
        SinglePrice::class  => 'createSinglePrice',
24
        ProgressivePrice::class => 'createProgressivePrice',
25
    ];
26
27
    protected $types = [
28
        'enum'      => EnumPrice::class,
29
        'single'    => SinglePrice::class,
30
        'progressive' => ProgressivePrice::class,
31
    ];
32
33
    /**
34
     * @var string default price class, when given will be used for not found types
35 16
     */
36
    protected $defaultClass = null;
37 16
38 16
    public function __construct(array $types = [], $defaultClass = null)
39 16
    {
40
        $this->types = $types;
41
        $this->defaultClass = $defaultClass;
42
    }
43
44 4
45
    /**
46 4
     * Creates price object.
47 4
     */
48 4
    public function create(PriceCreationDto $dto): PriceInterface
49
    {
50 4
        $class = $this->findClassForTypes([
51
            get_class($dto),
52 4
            $dto->type->getName(),
53
        ]);
54
        $method = $this->findMethodForClass($class);
55 4
56
        return $this->{$method}($dto);
57 4
    }
58 4
59 4
    public function findClassForTypes(array $types)
60
    {
61
        foreach ($types as $type) {
62 2
            if (isset($this->types[$type])) {
63 2
                return $this->types[$type];
64
            }
65
        }
66
        if ($this->defaultClass) {
67
            return $this->defaultClass;
68 4
        }
69
        throw new FailedCreatePriceException(sprintf('unknown types: "%s"', implode(',', $types)));
70 4
    }
71 4
72
    public function findMethodForClass($class)
73
    {
74
        if (isset($this->creators[$class])) {
75
            return $this->creators[$class];
76 1
        }
77
        throw new FailedCreatePriceException("unknown class: $class");
78 1
    }
79
80
    public function createEnumPrice(PriceCreationDto $dto)
81 3
    {
82
        return new EnumPrice($dto->id, $dto->type, $dto->target, $dto->plan, $dto->unit, $dto->currency, $dto->sums);
83 3
    }
84
85
    public function createRatePrice(PriceCreationDto $dto)
86
    {
87
        return new RatePrice($dto->id, $dto->type, $dto->target, $dto->plan, $dto->rate);
88
    }
89
90
    public function createSinglePrice(PriceCreationDto $dto)
91
    {
92
        return new SinglePrice($dto->id, $dto->type, $dto->target, $dto->plan, $dto->prepaid, $dto->price);
93
    }
94
95
    public function createProgressivePrice(PriceCreationDto $dto): ProgressivePrice
96
    {
97
        return new ProgressivePrice($dto->id, $dto->type, $dto->target, $dto->prepaid, $dto->thresholds, $dto->plan);
0 ignored issues
show
Bug introduced by
$dto->plan of type hiqdev\php\billing\plan\PlanInterface is incompatible with the type array expected by parameter $thresholds of hiqdev\php\billing\price...ivePrice::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

97
        return new ProgressivePrice($dto->id, $dto->type, $dto->target, $dto->prepaid, $dto->thresholds, /** @scrutinizer ignore-type */ $dto->plan);
Loading history...
Bug introduced by
$dto->thresholds of type array is incompatible with the type Money\Money expected by parameter $price of hiqdev\php\billing\price...ivePrice::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

97
        return new ProgressivePrice($dto->id, $dto->type, $dto->target, $dto->prepaid, /** @scrutinizer ignore-type */ $dto->thresholds, $dto->plan);
Loading history...
98
    }
99
}
100