Completed
Push — develop ( 8e38e7...02bdee )
by Nate
02:16
created

AbstractSaveableConnection::beforeDelete()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 0
cts 6
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
1
<?php
2
3
/**
4
 * @copyright  Copyright (c) Flipbox Digital Limited
5
 * @license    https://github.com/flipboxfactory/craft-integration/blob/master/LICENSE
6
 * @link       https://github.com/flipboxfactory/craft-integration/
7
 */
8
9
namespace flipbox\craft\integration\connections;
10
11
use craft\events\ModelEvent;
12
use yii\base\Model;
13
14
/**
15
 * @author Flipbox Factory <[email protected]>
16
 * @since 2.2.0
17
 */
18
abstract class AbstractSaveableConnection extends Model implements SavableConnectionInterface
19
{
20
    /**
21
     * @event ModelEvent The event that is triggered before the component is saved
22
     * You may set [[ModelEvent::isValid]] to `false` to prevent the component from getting saved.
23
     */
24
    const EVENT_BEFORE_SAVE = 'beforeSave';
25
26
    /**
27
     * @event ModelEvent The event that is triggered after the component is saved
28
     */
29
    const EVENT_AFTER_SAVE = 'afterSave';
30
31
    /**
32
     * @event ModelEvent The event that is triggered before the component is deleted
33
     * You may set [[ModelEvent::isValid]] to `false` to prevent the component from getting deleted.
34
     */
35
    const EVENT_BEFORE_DELETE = 'beforeDelete';
36
37
    /**
38
     * @event \yii\base\Event The event that is triggered after the component is deleted
39
     */
40
    const EVENT_AFTER_DELETE = 'afterDelete';
41
42
    /**
43
     * @inheritdoc
44
     */
45
    public function beforeSave(bool $isNew): bool
46
    {
47
        $event = new ModelEvent([
48
            'isNew' => $isNew,
49
        ]);
50
        $this->trigger(self::EVENT_BEFORE_SAVE, $event);
51
52
        return $event->isValid;
53
    }
54
55
    /**
56
     * @inheritdoc
57
     */
58
    public function afterSave(bool $isNew, array $changedAttributes)
59
    {
60
        if ($this->hasEventHandlers(self::EVENT_AFTER_SAVE)) {
61
            $this->trigger(self::EVENT_AFTER_SAVE, new ModelEvent([
62
                'isNew' => $isNew,
63
            ]));
64
        }
65
    }
66
67
    /**
68
     * @inheritdoc
69
     */
70
    public function beforeDelete(): bool
71
    {
72
        $event = new ModelEvent();
73
        $this->trigger(self::EVENT_BEFORE_DELETE, $event);
74
75
        return $event->isValid;
76
    }
77
78
    /**
79
     * @inheritdoc
80
     */
81
    public function afterDelete()
82
    {
83
        if ($this->hasEventHandlers(self::EVENT_AFTER_DELETE)) {
84
            $this->trigger(self::EVENT_AFTER_DELETE);
85
        }
86
    }
87
}
88