Completed
Push — master ( 4b3a6a...ccebfa )
by Dmitry
04:39
created

src/models/CertificateResource.php (2 issues)

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 Yii;
15
use yii\base\InvalidConfigException;
16
use yii\validators\NumberValidator;
17
18
/**
19
 * Class CertificateResource.
20
 *
21
 * @author Dmytro Naumenko <[email protected]>
22
 * @deprecated Is implemented in plan module
23
 */
24
class CertificateResource extends Resource
25
{
26
    use ModelTrait;
27
28
    public $certificateType;
29
30
    public static function tableName()
31
    {
32
        return 'resource';
33
    }
34
35
    const TYPE_CERT_PURCHASE = 'certificate_purchase';
36
    const TYPE_CERT_RENEWAL = 'certificate_renewal';
37
38 View Code Duplication
    public function rules()
39
    {
40
        $rules = parent::rules();
41
        $rules['create-required'] = [
42
            ['object_id'],
43
            'required',
44
            'on' => ['create', 'update'],
45
            'when' => function ($model) {
46
                /** @var self $model */
47
                return $model->isTypeCorrect();
48
            },
49
        ];
50
        $rules[] = [['certificateType'], 'safe'];
51
        $rules[] = [['data'], 'validatePrices', 'on' => ['create', 'update']];
52
53
        return $rules;
54
    }
55
56
    /**
57
     * @return array
58
     */
59
    public static function getPeriods()
60
    {
61
        return [
62
            1 => Yii::t('hipanel:finance:tariff', '{n, plural, one{# year} other{# years}}', ['n' => 1]),
63
            2 => Yii::t('hipanel:finance:tariff', '{n, plural, one{# year} other{# years}}', ['n' => 2]),
64
        ];
65
    }
66
67
    /**
68
     * @return array
69
     */
70
    public function getAvailablePeriods()
71
    {
72
        $periods = [];
73
        foreach ([1, 2] as $period) {
74
            if ($this->hasPriceForPeriod($period)) {
75
                $periods[$period] = Yii::t('hipanel:finance:tariff', '{n, plural, one{# year} other{# years}}', ['n' => $period]);
76
            }
77
        }
78
79
        return $periods;
80
    }
81
82
    public function getPriceForPeriod($period)
83
    {
84
        if (!$this->hasPriceForPeriod($period)) {
85
            return null;
86
            /// XXX throw new InvalidConfigException('Period ' . $period . ' is not available');
87
        }
88
89
        return (float) $this->data['sums'][$period];
0 ignored issues
show
The property data does not exist on object<hipanel\modules\f...ls\CertificateResource>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
90
    }
91
92
    public function hasPriceForPeriod($period)
93
    {
94
        return !empty($this->data['sums'][$period]);
0 ignored issues
show
The property data does not exist on object<hipanel\modules\f...ls\CertificateResource>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
95
    }
96
97
    public function validatePrices()
98
    {
99
        $periods = $this->getPeriods();
100
        $validator = new NumberValidator();
101
102
        foreach (array_keys($periods) as $period) {
103
            $validation = $validator->validate($this->data['sums'][$period]);
104
            if ($validation === false) {
105
                unset($this->data['sums'][$period]);
106
            }
107
        }
108
109
        $this->data = ['sums' => $this->data['sums']];
110
111
        return true;
112
    }
113
114
    /**
115
     * @return array
116
     */
117 View Code Duplication
    public function getTypes()
118
    {
119
        return [
120
            static::TYPE_CERT_PURCHASE => Yii::t('hipanel:finance:tariff', 'Purchase'),
121
            static::TYPE_CERT_RENEWAL => Yii::t('hipanel:finance:tariff', 'Renewal'),
122
        ];
123
    }
124
}
125