1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @copyright Copyright (c) Flipbox Digital Limited |
5
|
|
|
* @license https://flipboxfactory.com/software/patron/license |
6
|
|
|
* @link https://www.flipboxfactory.com/software/patron/ |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace flipbox\patron\services; |
10
|
|
|
|
11
|
|
|
use flipbox\ember\services\traits\records\Accessor; |
12
|
|
|
use flipbox\patron\db\ProviderQuery; |
13
|
|
|
use flipbox\patron\records\ProviderLock; |
14
|
|
|
use yii\base\Component; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @author Flipbox Factory <[email protected]> |
18
|
|
|
* @since 1.0.0 |
19
|
|
|
* |
20
|
|
|
* @method ProviderQuery getQuery($config = []) |
21
|
|
|
* @method ProviderLock create(array $attributes = []) |
22
|
|
|
* @method ProviderLock find($identifier) |
23
|
|
|
* @method ProviderLock get($identifier) |
24
|
|
|
* @method ProviderLock findByCondition($condition = []) |
25
|
|
|
* @method ProviderLock getByCondition($condition = []) |
26
|
|
|
* @method ProviderLock findByCriteria($criteria = []) |
27
|
|
|
* @method ProviderLock getByCriteria($criteria = []) |
28
|
|
|
* @method ProviderLock[] findAllByCondition($condition = []) |
29
|
|
|
* @method ProviderLock[] getAllByCondition($condition = []) |
30
|
|
|
* @method ProviderLock[] findAllByCriteria($criteria = []) |
31
|
|
|
* @method ProviderLock[] getAllByCriteria($criteria = []) |
32
|
|
|
*/ |
33
|
|
|
class ProviderLocks extends Component |
34
|
|
|
{ |
35
|
|
|
use Accessor; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @inheritdoc |
39
|
|
|
*/ |
40
|
|
|
public static function recordClass(): string |
41
|
|
|
{ |
42
|
|
|
return ProviderLock::class; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
|
46
|
|
|
/******************************************* |
47
|
|
|
* ASSOCIATE / DISSOCIATE |
48
|
|
|
*******************************************/ |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param ProviderLock $record |
52
|
|
|
* @return bool |
53
|
|
|
* @throws \Exception |
54
|
|
|
*/ |
55
|
|
|
public function associate( |
56
|
|
|
ProviderLock $record |
57
|
|
|
): bool { |
58
|
|
|
return $record->save(); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param ProviderLock $record |
64
|
|
|
* @return bool |
65
|
|
|
* @throws \Throwable |
66
|
|
|
* @throws \yii\db\StaleObjectException |
67
|
|
|
*/ |
68
|
|
|
public function dissociate( |
69
|
|
|
ProviderLock $record |
70
|
|
|
): bool { |
71
|
|
|
if (false === $this->existingAssociation($record)) { |
72
|
|
|
return true; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
return (bool)$record->delete(); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
|
79
|
|
|
/******************************************* |
80
|
|
|
* EXISTING |
81
|
|
|
*******************************************/ |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @param ProviderLock $record |
85
|
|
|
* @return bool |
86
|
|
|
*/ |
87
|
|
|
protected function existingAssociation( |
88
|
|
|
ProviderLock $record |
89
|
|
|
): bool { |
90
|
|
|
if (null !== ($existing = $this->lookupAssociation($record))) { |
91
|
|
|
$record->setOldAttributes( |
92
|
|
|
$existing->getOldAttributes() |
93
|
|
|
); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
return $existing !== null; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @param ProviderLock $record |
101
|
|
|
* @return ProviderLock|null |
102
|
|
|
*/ |
103
|
|
|
protected function lookupAssociation( |
104
|
|
|
ProviderLock $record |
105
|
|
|
) { |
106
|
|
|
$model = $this->getQuery() |
107
|
|
|
->where([ |
108
|
|
|
'providerId' => $record->providerId, |
109
|
|
|
'pluginId' => $record->pluginId |
110
|
|
|
])->one(); |
111
|
|
|
|
112
|
|
|
return $model instanceof ProviderLock ? $model : null; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @param int $providerId |
117
|
|
|
* @param int $pluginId |
118
|
|
|
* @return bool |
119
|
|
|
* @throws \Throwable |
120
|
|
|
*/ |
121
|
|
|
public function associateByIds( |
122
|
|
|
int $providerId, |
123
|
|
|
int $pluginId |
124
|
|
|
): bool { |
125
|
|
|
return $this->associate( |
126
|
|
|
$this->create([ |
127
|
|
|
'providerId' => $providerId, |
128
|
|
|
'pluginId' => $pluginId |
129
|
|
|
]) |
130
|
|
|
); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @param int $providerId |
135
|
|
|
* @param int $pluginId |
136
|
|
|
* @return bool |
137
|
|
|
* @throws \Throwable |
138
|
|
|
*/ |
139
|
|
|
public function dissociateByIds( |
140
|
|
|
int $providerId, |
141
|
|
|
int $pluginId |
142
|
|
|
): bool { |
143
|
|
|
return $this->dissociate( |
144
|
|
|
$this->create([ |
145
|
|
|
'providerId' => $providerId, |
146
|
|
|
'pluginId' => $pluginId |
147
|
|
|
]) |
148
|
|
|
); |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
|