| Total Complexity | 10 |
| Total Lines | 55 |
| Duplicated Lines | 0 % |
| Coverage | 95.24% |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
| 1 | <?php |
||
| 9 | class MultiTenantRecord extends ActiveRecord |
||
| 10 | { |
||
| 11 | /** |
||
| 12 | *@inheritdoc |
||
| 13 | */ |
||
| 14 | 1 | public function beforeSave($insert) |
|
| 15 | { |
||
| 16 | 1 | if (!Yii::$app->user->isGuest) { |
|
| 17 | 1 | $identity = Yii::$app->user->identity; |
|
| 18 | 1 | if ($identity instanceof TenantInterface) { |
|
| 19 | 1 | if ($insert && $this->beforeApplyTenant() && $this->{TenantInterface::ATTRIBUTE_NAME} === null) { |
|
| 20 | 1 | $this->{TenantInterface::ATTRIBUTE_NAME} = $identity->getTenantId(); |
|
| 21 | 1 | $this->afterApplyTenant(); |
|
| 22 | 1 | } |
|
| 23 | 1 | } else { |
|
| 24 | throw new NotSupportedException("Identity does not implements TenantInteface"); |
||
| 25 | } |
||
| 26 | 1 | } |
|
| 27 | 1 | return parent::beforeSave($insert); |
|
| 28 | } |
||
| 29 | |||
| 30 | /** |
||
| 31 | * {@inheritdoc} |
||
| 32 | */ |
||
| 33 | 3 | public static function find() |
|
| 36 | } |
||
| 37 | |||
| 38 | /** |
||
| 39 | * {@inheritdoc} |
||
| 40 | */ |
||
| 41 | 1 | protected function resolveFields(array $fields, array $expand) |
|
| 46 | } |
||
| 47 | |||
| 48 | /** |
||
| 49 | * This method is invoked before assign the attribute tenant id |
||
| 50 | * You may override this method to do preliminary checks before apply tenant id. |
||
| 51 | * @return boolean whether the tenant id should be assigned. Defaults to true. |
||
| 52 | */ |
||
| 53 | 1 | protected function beforeApplyTenant() |
|
| 56 | } |
||
| 57 | |||
| 58 | /** |
||
| 59 | * This method is invoked after assign the attribute tenant id |
||
| 60 | * You may override this method to do postprocessing after apply tenant id. |
||
| 61 | */ |
||
| 62 | 1 | protected function afterApplyTenant() |
|
| 67 |