Completed
Push — master ( c2bc12...ef5bc2 )
by Dmitry
04:55
created

src/models/CertificateResource.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/**
3
 * Finance module for HiPanel
4
 *
5
 * @link      https://github.com/hiqdev/hipanel-module-finance
6
 * @package   hipanel-module-finance
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2015-2019, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hipanel\modules\finance\models;
12
13
use hipanel\base\ModelTrait;
14
use Money\Formatter\IntlMoneyFormatter;
15
use Money\Money;
16
use Money\MoneyParser;
17
use Yii;
18
use yii\base\InvalidConfigException;
19
use yii\validators\NumberValidator;
20
21
/**
22
 * Class CertificateResource.
23
 *
24
 * @author Dmytro Naumenko <[email protected]>
25
 * @deprecated Is implemented in plan module
26
 */
27
class CertificateResource extends Resource
28
{
29
    use ModelTrait;
30
31
    public $certificateType;
32
33
    public static function tableName()
34
    {
35
        return 'resource';
36
    }
37
38
    const TYPE_CERT_PURCHASE = 'certificate_purchase';
39
    const TYPE_CERT_RENEWAL = 'certificate_renewal';
40
41 View Code Duplication
    public function rules()
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
42
    {
43
        $rules = parent::rules();
44
        $rules['create-required'] = [
45
            ['object_id'],
46
            'required',
47
            'on' => ['create', 'update'],
48
            'when' => function ($model) {
49
                /** @var self $model */
50
                return $model->isTypeCorrect();
51
            },
52
        ];
53
        $rules[] = [['certificateType'], 'safe'];
54
        $rules[] = [['data'], 'validatePrices', 'on' => ['create', 'update']];
55
56
        return $rules;
57
    }
58
59
    /**
60
     * @return array
61
     */
62
    public static function getPeriods()
63
    {
64
        return [
65
            1 => Yii::t('hipanel:finance:tariff', '{n, plural, one{# year} other{# years}}', ['n' => 1]),
66
            2 => Yii::t('hipanel:finance:tariff', '{n, plural, one{# year} other{# years}}', ['n' => 2]),
67
        ];
68
    }
69
70
    /**
71
     * @return array
72
     */
73
    public function getAvailablePeriods()
74
    {
75
        $periods = [];
76
        foreach ([1, 2] as $period) {
77
            if ($this->hasPriceForPeriod($period)) {
78
                $periods[$period] = Yii::t('hipanel:finance:tariff', '{n, plural, one{# year} other{# years}}', ['n' => $period]);
79
            }
80
        }
81
82
        return $periods;
83
    }
84
85
    public function getPriceForPeriod($period)
86
    {
87
        $sums = $this->data['sums'][$period];
88
        if (empty($sums)) {
89
            return null;
90
            /// XXX throw new InvalidConfigException('Period ' . $period . ' is not available');
91
        }
92
93
        return (float) $sums;
94
    }
95
96
    /**
97
     * @param int $period
98
     * @return string|null
99
     */
100
    public function getMoneyForPeriod(int $period)
101
    {
102
        $this->price = $this->getPriceForPeriod($period);
103
        if (empty($this->currency))
104
            return null;
105
        return $this->getMoney();
106
    }
107
108
    public function validatePrices()
109
    {
110
        $periods = $this->getPeriods();
111
        $validator = new NumberValidator();
112
113
        foreach (array_keys($periods) as $period) {
114
            $validation = $validator->validate($this->data['sums'][$period]);
115
            if ($validation === false) {
116
                unset($this->data['sums'][$period]);
117
            }
118
        }
119
120
        $this->data = ['sums' => $this->data['sums']];
121
122
        return true;
123
    }
124
125
    /**
126
     * @param int $period
127
     * @return bool
128
     */
129
    public function hasPriceForPeriod(int $period)
130
    {
131
        return !empty($this->data['sums'][$period]);
132
    }
133
134
    /**
135
     * @return array
136
     */
137 View Code Duplication
    public function getTypes()
138
    {
139
        return [
140
            static::TYPE_CERT_PURCHASE => Yii::t('hipanel:finance:tariff', 'Purchase'),
141
            static::TYPE_CERT_RENEWAL => Yii::t('hipanel:finance:tariff', 'Renewal'),
142
        ];
143
    }
144
}
145