Completed
Push — master ( 393377...32087f )
by Razon
04:23 queued 01:55
created

ArticleCategoryBehavior::events()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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