| Total Complexity | 9 |
| Total Lines | 46 |
| Duplicated Lines | 0 % |
| Coverage | 92.86% |
| Changes | 0 | ||
| 1 | <?php |
||
| 8 | trait MultiTenantRecord |
||
| 9 | { |
||
| 10 | /** |
||
| 11 | *@inheritdoc |
||
| 12 | */ |
||
| 13 | 1 | public function beforeSave($insert) |
|
| 14 | { |
||
| 15 | 1 | if (!Yii::$app->user->isGuest) { |
|
| 16 | 1 | $identity = Yii::$app->user->identity; |
|
| 17 | 1 | if ($identity instanceof TenantInterface) { |
|
| 18 | 1 | if ($insert && $this->beforeApplyTenant() && !$this->tenant_id) { |
|
| 19 | 1 | $this->tenant_id = $identity->getTenantId(); |
|
|
|
|||
| 20 | 1 | $this->afterApplyTenant(); |
|
| 21 | } |
||
| 22 | } else { |
||
| 23 | throw new NotSupportedException("Identity does not implements TenantInteface"); |
||
| 24 | } |
||
| 25 | } |
||
| 26 | 1 | return parent::beforeSave($insert); |
|
| 27 | } |
||
| 28 | |||
| 29 | /** |
||
| 30 | * {@inheritdoc} |
||
| 31 | */ |
||
| 32 | 2 | public static function find() |
|
| 33 | { |
||
| 34 | 2 | return Yii::createObject(MultiTenantQuery::class, [get_called_class()]); |
|
| 35 | } |
||
| 36 | |||
| 37 | |||
| 38 | /** |
||
| 39 | * This method is invoked before assign the attribute tenant id |
||
| 40 | * You may override this method to do preliminary checks before apply tenant id. |
||
| 41 | * @return boolean whether the tenant id should be assigned. Defaults to true. |
||
| 42 | */ |
||
| 43 | 1 | protected function beforeApplyTenant() |
|
| 46 | } |
||
| 47 | |||
| 48 | /** |
||
| 49 | * This method is invoked after assign the attribute tenant id |
||
| 50 | * You may override this method to do postprocessing after apply tenant id. |
||
| 51 | */ |
||
| 52 | 1 | protected function afterApplyTenant() |
|
| 57 |