|
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") { |
|
|
|
|
|
|
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
|
|
|
|
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@propertyannotation to your class or interface to document the existence of this variable.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.