Completed
Push — master ( d7bed1...8b3382 )
by Andrii
02:16
created

PriceFactory::createEnumPrice()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
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
/**
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
        SinglePrice::class  => 'createSinglePrice',
23
    ];
24
25
    protected $types = [
26
        'enum'      => EnumPrice::class,
27
        'single'    => SinglePrice::class,
28
    ];
29
30
    /**
31
     * @var string default price class, when given will be used for not found types
32
     */
33
    protected $defaultClass = null;
34
35 2
    public function __construct(array $types = [], $defaultClass = null)
36
    {
37 2
        $this->types = $types;
38 2
        $this->defaultClass = $defaultClass;
39 2
    }
40
41
    /**
42
     * Creates price object.
43
     *
44
     * @param PriceCreationDto $dto
45
     * @return PriceInterface
46
     */
47 2
    public function create(PriceCreationDto $dto): PriceInterface
48
    {
49 2
        $class = $this->findClassForTypes([
50 2
            get_class($dto),
51 2
            $dto->type->getName(),
52
        ]);
53 2
        $method = $this->findMethodForClass($class);
54
55 2
        return $this->{$method}($dto);
56
    }
57
58 2
    public function findClassForTypes(array $types)
59
    {
60 2
        foreach ($types as $type) {
61 2
            if (isset($this->types[$type])) {
62 2
                return $this->types[$type];
63
            }
64
        }
65
        if ($this->defaultClass) {
66
            return $this->defaultClass;
67
        }
68
        throw new FailedCreatePriceException(sprintf(
69
            'unknown types: "%s"',
70
            implode(',', $types)
71
        ));
72
    }
73
74 2
    public function findMethodForClass($class)
75
    {
76 2
        if (isset($this->creators[$class])) {
77 2
            return $this->creators[$class];
78
        }
79
        throw new FailedCreatePriceException("unknown class: $class");
80
    }
81
82 1
    public function createEnumPrice(PriceCreationDto $dto)
83
    {
84 1
        return new EnumPrice($dto->id, $dto->type, $dto->target, $dto->plan, $dto->unit, $dto->currency, $dto->sums);
85
    }
86
87 1
    public function createSinglePrice(PriceCreationDto $dto)
88
    {
89 1
        return new SinglePrice($dto->id, $dto->type, $dto->target, $dto->plan, $dto->prepaid, $dto->price);
90
    }
91
}
92