Completed
Push — master ( 16077c...379eac )
by Andrii
07:06
created

PriceFactory::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 2
crap 1
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-2018, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hiqdev\php\billing\price;
12
13
use hiqdev\billing\hiapi\models\Price;
14
15
/**
16
 * Default price factory.
17
 *
18
 * @author Andrii Vasyliev <[email protected]>
19
 */
20
class PriceFactory implements PriceFactoryInterface
21
{
22
    protected $creators = [
23
        EnumPrice::class    => 'createEnumPrice',
24
        SinglePrice::class  => 'createSinglePrice',
25
    ];
26
27
    protected $types = [
28
        'enum'      => EnumPrice::class,
29
        'single'    => SinglePrice::class,
30
    ];
31
32
    /**
33
     * @var string default price class, when given will be used for not found types
34
     */
35
    protected $defaultClass = null;
36
37 2
    public function __construct(array $types = [], $defaultClass = null)
38
    {
39 2
        $this->types = $types;
40 2
        $this->defaultClass = $defaultClass;
41 2
    }
42
43
    /**
44
     * Creates price object.
45
     *
46
     * @param PriceCreationDto $dto
47
     * @return Price
48
     */
49 2
    public function create(PriceCreationDto $dto)
50
    {
51 2
        $class = $this->findClassForTypes([
52 2
            get_class($dto),
53 2
            $dto->type->getName(),
0 ignored issues
show
Bug introduced by
Consider using $dto->type->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
54
        ]);
55 2
        $method = $this->findMethodForClass($class);
56
57 2
        return $this->{$method}($dto);
58
    }
59
60 2
    public function findClassForTypes(array $types)
61
    {
62 2
        foreach ($types as $type) {
63 2
            if (isset($this->types[$type])) {
64 2
                return $this->types[$type];
65
            }
66
        }
67
        if ($this->defaultClass) {
68
            return $this->defaultClass;
69
        }
70
        throw new FailedCreatePriceException("unknown type: $type");
71
    }
72
73 2
    public function findMethodForClass($class)
74
    {
75 2
        if (isset($this->creators[$class])) {
76 2
            return $this->creators[$class];
77
        }
78
        throw new FailedCreatePriceException("unknown class: $class");
79
    }
80
81 1
    public function createEnumPrice(PriceCreationDto $dto)
82
    {
83 1
        return new EnumPrice($dto->id, $dto->type, $dto->target, $dto->plan, $dto->unit, $dto->currency, $dto->sums);
84
    }
85
86 1
    public function createSinglePrice(PriceCreationDto $dto)
87
    {
88 1
        return new SinglePrice($dto->id, $dto->type, $dto->target, $dto->plan, $dto->prepaid, $dto->price);
89
    }
90
}
91