Completed
Push — master ( 678089...988510 )
by Andrii
02:43
created

CertificatePlan::get()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 0
dl 0
loc 7
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\tests\support\plan;
12
13
use hiqdev\php\billing\action\Action;
14
use hiqdev\php\billing\customer\Customer;
15
use hiqdev\php\billing\plan\Plan;
16
use hiqdev\php\billing\price\EnumPrice;
17
use hiqdev\php\billing\target\Target;
18
use hiqdev\php\billing\type\Type;
19
use hiqdev\php\units\Unit;
20
use Money\Currency;
21
22
class CertificatePlan extends Plan
23
{
24
    protected static $instance;
25
26
    /** @var Customer */
27
    public $customer;
28
    /** @var Type  */
29
    public $purchase;
30
    /** @var Type  */
31
    public $renewal;
32
    /** @var Target  */
33
    public $rapidssl;
34
    /** @var Target  */
35
    public $verisign;
36
    /** @var array  */
37
    public $types;
38
    /** @var array  */
39
    public $rawPrices;
40
    /** @var Target[] */
41
    public $targets;
42
43
    public static function get()
44
    {
45
        if (static::$instance === null) {
46
            new static();
47
        }
48
49
        return static::$instance;
50
    }
51
52
    public function __construct()
53
    {
54
        if (static::$instance === null) {
55
            static::$instance = $this;
56
        }
57
        $this->seller   = new Customer(1, 'seller');
58
        $this->customer = new Customer(2, 'client', $this->seller);
59
        $this->purchase = new Type(1, 'certificate_purchase');
60
        $this->renewal  = new Type(2, 'certificate_renewal');
61
        $this->rapidssl = new Target('rapidssl_standard', 'certificate_type');
62
        $this->verisign = new Target('verisign_standard', 'certificate_type');
63
        $this->types = [
64
            'purchase'  => $this->purchase,
65
            'renewal'   => $this->renewal,
66
        ];
67
        $this->targets = [
68
            'rapidssl'  => $this->rapidssl,
69
            'verisign'  => $this->verisign,
70
        ];
71
        $this->rawPrices = [
72
            $this->getRawPriceKey($this->purchase, $this->rapidssl) => [
73
                1 => 1129,
74
                2 => 1219,
75
                3 => 1309,
76
            ],
77
            $this->getRawPriceKey($this->renewal, $this->rapidssl) => [
78
                1 => 1125,
79
                2 => 1215,
80
                3 => 1305,
81
            ],
82
            $this->getRawPriceKey($this->purchase, $this->verisign) => [
83
                1 => 2129,
84
                2 => 2219,
85
                3 => 2309,
86
            ],
87
            $this->getRawPriceKey($this->renewal, $this->verisign) => [
88
                1 => 2125,
89
                2 => 2215,
90
                3 => 2305,
91
            ],
92
        ];
93
        $prices = [];
94
        foreach ($this->types as $type) {
95
            foreach ($this->targets as $target) {
96
                $prices[] = new EnumPrice(null, $type, $target, null, Unit::year(), new Currency('USD'), $this->getRawPrices($type, $target));
97
            }
98
        }
99
        parent::__construct(null, 'Test Certificate Plan', $this->seller, $prices);
100
    }
101
102
103
    /**
104
     * @param Action $action
105
     * @return mixed
106
     */
107
    public function getRawPrice($action)
108
    {
109
        $years = $action->getQuantity()->convert(Unit::year())->getQuantity();
110
111
        return $this->getRawPrices($action->getType(), $action->getTarget())[$years];
112
    }
113
114
    public function getRawPrices($type, $target)
115
    {
116
        return $this->rawPrices[$this->getRawPriceKey($type, $target)];
117
    }
118
119
    /**
120
     * @param Type $type
121
     * @param Target $target
122
     * @return string
123
     */
124
    public function getRawPriceKey($type, $target)
125
    {
126
        return $type->getName() . '-' . $target->getUniqueId();
127
    }
128
}
129