Completed
Push — master ( 5d4578...5f67d7 )
by Filipe
02:35
created

EventTriggers::triggerBeforeDelete()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 7
Ratio 100 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 7
loc 7
ccs 5
cts 5
cp 1
rs 9.4285
cc 1
eloc 5
nc 1
nop 1
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 View Code Duplication
    protected function triggerBeforeDelete(EntityInterface $entity)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
78
    {
79 2
        $event = new Delete($entity);
80 2
        $event->setAction(Delete::ACTION_BEFORE_DELETE);
81 2
        return Orm::getEmitter($this->getEntityClassName())
82 2
            ->emit($event);
83
    }
84
85
    /**
86
     * Triggers the after delete event
87
     *
88
     * @param EntityInterface $entity
89
     *
90
     * @return \League\Event\EventInterface|Delete
91
     */
92 2 View Code Duplication
    protected function triggerAfterDelete(EntityInterface $entity)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
93
    {
94 2
        $event = new Delete($entity);
95 2
        $event->setAction(Delete::ACTION_AFTER_DELETE);
96 2
        return Orm::getEmitter($this->getEntityClassName())
97 2
            ->emit($event);
98
    }
99
100
    abstract public function getEntityClassName();
101
}