Completed
Push — master ( 7354ed...57f1a6 )
by Andrii
05:45
created

CertificateResource::hasPriceForPeriod()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 3
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 2
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
    public function rules()
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];
0 ignored issues
show
Documentation introduced by strorch
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...
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);
0 ignored issues
show
Documentation introduced by strorch
The property price does not exist on object<hipanel\modules\f...ls\CertificateResource>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write 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.");
        }
    }

}

Since the property has write access only, you can use the @property-write 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...
103
        if (empty($this->currency))
0 ignored issues
show
Documentation introduced by strorch
The property currency 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...
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]);
0 ignored issues
show
Documentation introduced by SilverFire - Dmitry Naumenko
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...
115
            if ($validation === false) {
116
                unset($this->data['sums'][$period]);
117
            }
118
        }
119
120
        $this->data = ['sums' => $this->data['sums']];
0 ignored issues
show
Documentation introduced by Andrii Vasyliev
The property data does not exist on object<hipanel\modules\f...ls\CertificateResource>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write 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.");
        }
    }

}

Since the property has write access only, you can use the @property-write 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...
Documentation introduced by Andrii Vasyliev
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...
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->sums[$period]);
0 ignored issues
show
Documentation introduced by Andrii Vasyliev
The property sums 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...
132
    }
133
134
    /**
135
     * @return array
136
     */
137 View Code Duplication
    public function getTypes()
0 ignored issues
show
Duplication introduced by SilverFire - Dmitry Naumenko
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...
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