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\models; |
12
|
|
|
|
13
|
|
|
use Yii; |
14
|
|
|
use yii\base\Model; |
15
|
|
|
|
16
|
|
|
class DomainService extends Model |
17
|
|
|
{ |
18
|
|
|
const SERVICE_OPERATION_PURCHASE = 'purchase'; |
19
|
|
|
const SERVICE_OPERATION_RENEW = 'renew'; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var string Human-readable name |
23
|
|
|
*/ |
24
|
|
|
public $name; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var string Service type |
28
|
|
|
*/ |
29
|
|
|
public $type; |
30
|
|
|
|
31
|
|
|
/** @var DomainResource[] */ |
32
|
|
|
public $resources; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Returns resource for $operation. |
36
|
|
|
* |
37
|
|
|
* @param string $operation |
38
|
|
|
* @return DomainResource |
39
|
|
|
*/ |
40
|
|
|
public function getResource($operation) |
41
|
|
|
{ |
42
|
|
|
return $this->resources[$operation]; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Tries to assign a resource in this service, if the type is correct. |
47
|
|
|
* |
48
|
|
|
* @param DomainResource $resource |
49
|
|
|
* @return bool |
50
|
|
|
*/ |
51
|
|
|
public function tryResourceAssignation(DomainResource $resource) |
52
|
|
|
{ |
53
|
|
|
if ($type = $this->matchType($resource)) { |
54
|
|
|
$this->resources[$type] = $resource; |
55
|
|
|
|
56
|
|
|
return true; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
return false; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Check whether $resource belongs to this service type. |
64
|
|
|
* |
65
|
|
|
* @param DomainResource $resource |
66
|
|
|
* @return false|string operation type or false, if resource does not match this service |
67
|
|
|
*/ |
68
|
|
|
protected function matchType(DomainResource $resource) |
69
|
|
|
{ |
70
|
|
|
foreach ($this->getOperations() as $operation => $name) { |
71
|
|
|
if ($resource->type === $this->type . "_$operation") { |
72
|
|
|
return $operation; |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
return false; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @return array available operations |
81
|
|
|
*/ |
82
|
|
View Code Duplication |
public static function getOperations() |
|
|
|
|
83
|
|
|
{ |
84
|
|
|
return [ |
85
|
|
|
static::SERVICE_OPERATION_PURCHASE => Yii::t('hipanel:finance:tariff', 'Purchase'), |
86
|
|
|
static::SERVICE_OPERATION_RENEW => Yii::t('hipanel:finance:tariff', 'Renewal'), |
87
|
|
|
]; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @return bool whether service contains all necessary resources |
92
|
|
|
*/ |
93
|
|
|
public function isFulfilled() |
94
|
|
|
{ |
95
|
|
|
return count($this->resources) === count($this->getOperations()); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
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.