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

CertificateResource::getMoneyForPeriod()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 0
cts 5
cp 0
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 6
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
Duplication introduced by
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)) {
0 ignored issues
show
Documentation Bug introduced by
The method hasPriceForPeriod does not exist on object<hipanel\modules\f...ls\CertificateResource>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
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
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
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
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
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
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
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
     * @return array
127
     */
128 View Code Duplication
    public function getTypes()
0 ignored issues
show
Duplication introduced by
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...
129
    {
130
        return [
131
            static::TYPE_CERT_PURCHASE => Yii::t('hipanel:finance:tariff', 'Purchase'),
132
            static::TYPE_CERT_RENEWAL => Yii::t('hipanel:finance:tariff', 'Renewal'),
133
        ];
134
    }
135
}
136