Completed
Push — master ( 030ae6...89eb04 )
by Dmitry
04:29
created

DomainService::matchType()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 10
ccs 0
cts 9
cp 0
rs 9.4285
cc 3
eloc 5
nc 3
nop 1
crap 12
1
<?php
2
3
namespace hipanel\modules\finance\models;
4
5
use Yii;
6
use yii\base\Model;
7
8
class DomainService extends Model
9
{
10
    const SERVICE_OPERATION_PURCHASE = 'purchase';
11
    const SERVICE_OPERATION_RENEW = 'renew';
12
13
    /**
14
     * @var string Human-readable name
15
     */
16
    public $name;
17
18
    /**
19
     * @var string Service type
20
     */
21
    public $type;
22
23
    /** @var DomainResource[] */
24
    public $resources;
25
26
    /**
27
     * Returns resource for $operation
28
     *
29
     * @param string $operation
30
     * @return DomainResource
31
     */
32
    public function getResource($operation)
33
    {
34
        return $this->resources[$operation];
35
    }
36
37
    /**
38
     * Tries to assign a resource in this service, if the type is correct
39
     *
40
     * @param DomainResource $resource
41
     * @return bool
42
     */
43
    public function tryResourceAssignation(DomainResource $resource)
44
    {
45
        if ($type = $this->matchType($resource)) {
46
            $this->resources[$type] = $resource;
47
            return true;
48
        }
49
50
        return false;
51
    }
52
53
    /**
54
     * Check whether $resource belongs to this service type
55
     *
56
     * @param DomainResource $resource
57
     * @return false|string operation type or false, if resource does not match this service
58
     */
59
    protected function matchType(DomainResource $resource)
60
    {
61
        foreach ($this->getOperations() as $operation => $name) {
62
            if ($resource->type === $this->type . "_$operation") {
0 ignored issues
show
Documentation introduced by
The property type does not exist on object<hipanel\modules\f...\models\DomainResource>. 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...
63
                return $operation;
64
            }
65
        }
66
67
        return false;
68
    }
69
70
    /**
71
     * @return array available operations
72
     */
73
    public static function getOperations()
74
    {
75
        return [
76
            static::SERVICE_OPERATION_PURCHASE => Yii::t('hipanel/finance/tariff', 'Purchase'),
77
            static::SERVICE_OPERATION_RENEW => Yii::t('hipanel/finance/tariff', 'Renewal')
78
        ];
79
    }
80
81
    /**
82
     * @return bool whether service contains all necessary resources
83
     */
84
    public function isFulfilled()
85
    {
86
        return count($this->resources) === count($this->getOperations());
87
    }
88
89
}
90