Completed
Push — master ( 725d9d...16077c )
by Dmitry
05:29
created

PriceFactory::create()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3.0987

Importance

Changes 0
Metric Value
dl 0
loc 17
ccs 7
cts 9
cp 0.7778
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 11
nc 4
nop 1
crap 3.0987
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
        try {
52 2
            $class = $this->findClassForType(get_class($dto));
53 2
        } catch (FailedCreatePriceException $failedForClass) {
54
            try {
55 2
                $type = $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...
56 2
                $class = $this->findClassForType($type);
57
            } catch (FailedCreatePriceException $failedForType) {
58
                throw $failedForType;
59
            }
60
        }
61
62 2
        $method = $this->findMethodForClass($class);
63
64 2
        return $this->{$method}($dto);
65
    }
66
67 2
    public function findClassForType($type)
68
    {
69 2
        if (isset($this->types[$type])) {
70 2
            return $this->types[$type];
71
        }
72 2
        if ($this->defaultClass) {
73
            return $this->defaultClass;
74
        }
75 2
        throw new FailedCreatePriceException("unknown type: $type");
76
    }
77
78 2
    public function findMethodForClass($class)
79
    {
80 2
        if (isset($this->creators[$class])) {
81 2
            return $this->creators[$class];
82
        }
83
        throw new FailedCreatePriceException("unknown class: $class");
84
    }
85
86 1
    public function createEnumPrice(PriceCreationDto $dto)
87
    {
88 1
        return new EnumPrice($dto->id, $dto->type, $dto->target, $dto->plan, $dto->unit, $dto->currency, $dto->sums);
89
    }
90
91 1
    public function createSinglePrice(PriceCreationDto $dto)
92
    {
93 1
        return new SinglePrice($dto->id, $dto->type, $dto->target, $dto->plan, $dto->prepaid, $dto->price);
94
    }
95
}
96