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

ArticleCategoryBehavior   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 21
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 3
eloc 11
c 1
b 0
f 0
dl 0
loc 21
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A events() 0 4 1
A beforeDelete() 0 12 2
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