Completed
Push — master ( 37f66f...c69285 )
by Andrii
12:09
created

AbstractPurchase   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 8
lcom 2
cbo 4
dl 0
loc 72
ccs 0
cts 37
cp 0
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getResult() 0 4 1
A operation() 0 4 1
A init() 0 7 1
A execute() 0 9 2
A renderNotes() 0 4 1
A rules() 0 7 1
A tableName() 0 4 1
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\cart;
12
13
use yii\base\InvalidConfigException;
14
15
/**
16
 * Class Purchase.
17
 */
18
abstract class AbstractPurchase extends \hipanel\base\Model
19
{
20
    use \hipanel\base\ModelTrait;
21
22
    /**
23
     * @var AbstractCartPosition
24
     */
25
    public $position;
26
27
    /**
28
     * @var array result of purchase execution
29
     */
30
    protected $_result;
31
32
    public function getResult()
33
    {
34
        return $this->_result;
35
    }
36
37
    /** {@inheritdoc} */
38
    public static function tableName()
39
    {
40
        throw new InvalidConfigException('Method "tableName" must be declared');
41
    }
42
43
    /**
44
     * @var string operation to be performed, e.g.: Renew, Transfer, Registration
45
     */
46
    public static function operation()
47
    {
48
        throw new InvalidConfigException('Method "operation" must be declared');
49
    }
50
51
    /** {@inheritdoc} */
52
    public function init()
53
    {
54
        parent::init();
55
56
        $this->calculation_id = $this->position->getId();
0 ignored issues
show
Documentation introduced by
The property calculation_id does not exist on object<hipanel\modules\f...\cart\AbstractPurchase>. 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...
57
        $this->amount = $this->position->getQuantity();
0 ignored issues
show
Documentation introduced by
The property amount does not exist on object<hipanel\modules\f...\cart\AbstractPurchase>. 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...
58
    }
59
60
    /**
61
     * Executes the purchase.
62
     * Calls proper API commands to purchase the product.
63
     * @throws ErrorPurchaseException in case of failed purchase
64
     * @return true if the item was purchased successfully
65
     */
66
    public function execute()
67
    {
68
        if ($this->validate()) {
69
            $this->_result = static::perform(static::operation(), $this->getAttributes());
70
            return true;
71
        }
72
73
        return false;
74
    }
75
76
    public function renderNotes()
77
    {
78
        return '';
79
    }
80
81
    /** {@inheritdoc} */
82
    public function rules()
83
    {
84
        return [
85
            [['calculation_id', 'object', 'client', 'type', 'currency', 'item'], 'safe'],
86
            [['amount'], 'number'],
87
        ];
88
    }
89
}
90