Completed
Push — master ( a82fec...cf3991 )
by Dmitry
09:24
created

CertificateTariffManager::determineParentTariff()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 17
rs 9.4285
cc 3
eloc 10
nc 3
nop 0
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-2017, 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\forms\DomainTariffForm;
16
use hipanel\modules\finance\models\Tariff;
17
use hiqdev\hiart\ConnectionInterface;
18
use hiqdev\hiart\ResponseErrorException;
19
use Yii;
20
use yii\base\InvalidConfigException;
21
use yii\web\NotFoundHttpException;
22
use yii\web\UnprocessableEntityHttpException;
23
24
/**
25
 * Class CertificateTariffManager
26
 *
27
 * @author Dmytro Naumenko <[email protected]>
28
 */
29
class CertificateTariffManager extends AbstractTariffManager
30
{
31
    /**
32
     * @var DomainTariffForm
33
     */
34
    public $form;
35
36
    /**
37
     * {@inheritdoc}
38
     */
39
    protected $type = Tariff::TYPE_CERT;
40
41
    /**
42
     * @var ConnectionInterface
43
     */
44
    private $connection;
45
46
    public function __construct(ConnectionInterface $connection, $config = [])
47
    {
48
        $this->connection = $connection;
49
50
        parent::__construct($config);
51
    }
52
53
    public function init()
54
    {
55
        if (!Yii::getAlias('@certificate', true)) {
56
            throw new InvalidConfigException('Certificate module is missing');
57
        }
58
59
        parent::init();
60
    }
61
62
    protected function determineParentTariff()
63
    {
64
        $id = parent::determineParentTariff();
65
66
        if ($id === null) {
67
            $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...
68
                ->action('get-available-info')
69
                ->andFilterWhere(['type' => $this->type])
70
                ->all());
71
72
            if ($tariff instanceof Tariff) {
73
                $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...
74
            }
75
        }
76
77
        return $id;
78
    }
79
80
    public function insert()
81
    {
82
        $data = $this->form->toArray();
83
84
        try {
85
            $result = Tariff::perform('create', $data);
86
        } catch (ResponseErrorException $e) {
87
            throw new UnprocessableEntityHttpException($e->getMessage(), 0, $e);
88
        }
89
90
        $this->form->id = $result['id'];
91
92
        return true;
93
    }
94
95 View Code Duplication
    public function update()
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...
96
    {
97
        $data = $this->form->toArray();
98
99
        try {
100
            $result = Tariff::perform('update', $data);
0 ignored issues
show
Unused Code introduced by
$result is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
101
        } catch (ResponseErrorException $e) {
102
            throw new UnprocessableEntityHttpException($e->getMessage(), 0, $e);
103
        }
104
105
        return true;
106
    }
107
108
    protected function getFormOptions()
109
    {
110
        return array_merge([
111
            'class' => CertificateTariffForm::class,
112
            'certificateTypes' => Ref::getList('type,certificate', 'hipanel:certificate', [
113
                'select' => 'id_label',
114
                'mapOptions' => ['from' => 'id'],
115
            ]),
116
        ], parent::getFormOptions());
117
    }
118
}
119