Passed
Push — master ( a90c1b...36c150 )
by Razon
02:16
created

ArticleCategoryBehavior::beforeDelete()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 8
c 1
b 0
f 0
dl 0
loc 12
rs 10
cc 2
nc 2
nop 1
1
<?php
2
namespace App\Behavior;
3
4
use App\Model\Article;
5
use App\Model\ArticleCategory;
6
use yii\base\Behavior;
7
use yii\base\Event;
8
9
class ArticleCategoryBehavior extends Behavior
10
{
11
    public function events()
12
    {
13
        return [
14
            ArticleCategory::EVENT_BEFORE_DELETE => 'beforeDelete',
15
        ];
16
    }
17
18
    public function beforeDelete(Event $event)
19
    {
20
        /** @var ArticleCategory $model */
21
        $model = $event->sender;
22
        $count = Article::find()
23
            ->where([
24
                'category_id' => $model->id,
25
                'is_deleted' => 0,
26
            ])
27
            ->count();
28
        if ($count > 0) {
29
            throw new \Exception('Unable to delete category since there are articles belong to it');
30
        }
31
    }
32
}
33