Completed
Push — master ( a59805...adecaf )
by Filipe
02:59
created

EventTriggers::triggerDelete()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 8
ccs 7
cts 7
cp 1
rs 9.4285
cc 1
eloc 6
nc 1
nop 2
crap 1
1
<?php
2
3
/**
4
 * This file is part of slick/orm package
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
namespace Slick\Orm\Mapper;
11
12
use Slick\Database\Sql\Insert;
13
use Slick\Orm\EntityInterface;
14
use Slick\Orm\Event\Delete;
15
use Slick\Orm\Event\Save;
16
use Slick\Orm\Orm;
17
18
/**
19
 * ORM entity event triggers methods
20
 *
21
 * @package Slick\Orm\Mapper
22
 * @author  Filipe Silva <[email protected]>
23
 */
24
trait EventTriggers
25
{
26
27
    /**
28
     * Triggers the before save event
29
     *
30
     * @param Sql/Update|Sql/Insert $query
0 ignored issues
show
Documentation introduced by
The doc-type Sql/Update|Sql/Insert could not be parsed: Unknown type name "Sql/Update" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
31
     * @param EntityInterface $entity
32
     * @param array $data
33
     *
34
     * @return Save
35
     */
36 4
    protected function triggerBeforeSave(
37
        $query, EntityInterface $entity, array $data
38
    ) {
39
        $save = $query instanceof Insert
40 4
            ? (new Save($entity, $data))
41 2
                ->setAction(Save::ACTION_BEFORE_INSERT)
42 4
            : (new Save($entity, $data))
43 4
                ->setAction(Save::ACTION_BEFORE_UPDATE);
44
45
        /** @var Save $save */
46 4
        return Orm::getEmitter($this->getEntityClassName())->emit($save);
47
    }
48
49
    /**
50
     * Triggers the after save event
51
     *
52
     * @param Save $saveEvent
53
     * @param EntityInterface $entity
54
     *
55
     * @return \League\Event\EventInterface|Save
56
     */
57 4
    protected function triggerAfterSave(
58
        Save $saveEvent, EntityInterface $entity
59
    ) {
60 4
        $afterSave = clone $saveEvent;
61 4
        $action = $afterSave->getName() == Save::ACTION_BEFORE_INSERT
62 4
            ? Save::ACTION_AFTER_INSERT
63 4
            : Save::ACTION_AFTER_UPDATE;
64
65 4
        $afterSave->setEntity($entity)->setAction($action);
66 4
        return Orm::getEmitter($this->getEntityClassName())
67 4
            ->emit($afterSave);
68
    }
69
70
    /**
71
     * Triggers the before delete event
72
     *
73
     * @param EntityInterface $entity
74
     *
75
     * @return \League\Event\EventInterface|Delete
76
     */
77 2
    protected function triggerBeforeDelete(EntityInterface $entity)
78
    {
79 2
        return $this->triggerDelete($entity);
80
    }
81
82
    /**
83
     * Triggers the after delete event
84
     *
85
     * @param EntityInterface $entity
86
     *
87
     * @return \League\Event\EventInterface|Delete
88
     */
89 2
    protected function triggerAfterDelete(EntityInterface $entity)
90
    {
91 2
        return $this->triggerDelete($entity, Delete::ACTION_AFTER_DELETE);
92
    }
93
94
    /**
95
     * Triggers the delete event with provided action
96
     *
97
     * @param EntityInterface $entity
98
     * @param string $action
99
     * @return \League\Event\EventInterface|Delete
100
     */
101 2
    protected function triggerDelete(
102 1
        EntityInterface $entity, $action = Delete::ACTION_BEFORE_DELETE
103 1
    ) {
104 2
        $event = new Delete($entity);
105 2
        $event->setAction($action);
106 2
        return Orm::getEmitter($this->getEntityClassName())
107 2
            ->emit($event);
108
    }
109
110
    /**
111
     * @return string
112
     */
113
    abstract public function getEntityClassName();
114
}