Passed
Pull Request — master (#99)
by
unknown
02:46
created

CertificatePlan::__construct()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 48
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Importance

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