CertificateTariffManager::init()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 0
cts 7
cp 0
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
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\logic;
12
13
use hipanel\models\Ref;
14
use hipanel\modules\finance\forms\CertificateTariffForm;
15
use hipanel\modules\finance\models\Tariff;
16
use hiqdev\hiart\ConnectionInterface;
17
use Yii;
18
use yii\base\InvalidConfigException;
19
20
/**
21
 * Class CertificateTariffManager.
22
 *
23
 * @author Dmytro Naumenko <[email protected]>
24
 */
25
class CertificateTariffManager extends AbstractTariffManager
26
{
27
    /**
28
     * @var CertificateTariffForm
29
     */
30
    public $form;
31
32
    /**
33
     * {@inheritdoc}
34
     */
35
    protected $type = Tariff::TYPE_CERT;
36
37
    /**
38
     * @var ConnectionInterface
39
     */
40
    private $connection;
41
42
    public function __construct(ConnectionInterface $connection, $config = [])
43
    {
44
        $this->connection = $connection;
45
46
        parent::__construct($config);
47
    }
48
49
    public function init()
50
    {
51
        if (!Yii::getAlias('@certificate', true)) {
52
            throw new InvalidConfigException('Certificate module is missing');
53
        }
54
55
        parent::init();
56
    }
57
58
    protected function determineParentTariff()
59
    {
60
        $id = parent::determineParentTariff();
61
62
        if ($id === null) {
63
            $tariff = reset(Tariff::find()
0 ignored issues
show
Bug introduced by
\hipanel\modules\finance...=> $this->type))->all() cannot be passed to reset() as the parameter $array expects a reference.
Loading history...
64
                ->action('get-available-info')
65
                ->andFilterWhere(['type' => $this->type])
66
                ->all());
67
68
            if ($tariff instanceof Tariff) {
69
                $id = $tariff->id;
0 ignored issues
show
Documentation introduced by
The property id does not exist on object<hipanel\modules\finance\models\Tariff>. 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...
70
            }
71
        }
72
73
        return $id;
74
    }
75
76
    protected function getFormOptions()
77
    {
78
        return array_merge([
79
            'class' => CertificateTariffForm::class,
80
            'certificateTypes' => Ref::getList('type,certificate', 'hipanel:certificate', [
81
                'select' => 'id_label',
82
                'mapOptions' => ['from' => 'id'],
83
            ]),
84
        ], parent::getFormOptions());
85
    }
86
}
87