NestedExtension::implementedEvents()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 8
rs 10
c 0
b 0
f 0
ccs 5
cts 5
cp 1
crap 1
1
<?php
2
/**
3
 * Copyright 2016 - 2018, Cake Development Corporation (http://cakedc.com)
4
 *
5
 * Licensed under The MIT License
6
 * Redistributions of files must retain the above copyright notice.
7
 *
8
 * @copyright Copyright 2016 - 2018, Cake Development Corporation (http://cakedc.com)
9
 * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
10
 */
11
12
namespace CakeDC\Api\Service\Action\Extension;
13
14
use Cake\Event\Event;
15
use Cake\Event\EventListenerInterface;
16
use Cake\ORM\Entity;
17
use Cake\ORM\Query;
18
19
/**
20
 * Class NestedExtension
21
 *
22
 * @package CakeDC\Api\Service\Action\Extension
23
 */
24
class NestedExtension extends Extension implements EventListenerInterface
25
{
26
27
    /**
28
     * Returns a list of events this object is implementing. When the class is registered
29
     * in an event manager, each individual method will be associated with the respective event.
30
     *
31
     * @return array
32
     */
33 9
    public function implementedEvents()
34
    {
35
        return [
36 9
            'Action.Crud.onFindEntities' => 'findEntities',
37 9
            'Action.Crud.onFindEntity' => 'findEntity',
38 9
            'Action.Crud.onPatchEntity' => 'patchEntity',
39 9
        ];
40
    }
41
42
    /**
43
     * On find entities.
44
     *
45
     * @param Event $event An Event instance
46
     * @return Query
47
     */
48 2
    public function findEntities(Event $event)
49
    {
50 2
        $action = $event->getSubject();
51 2
        $query = $event->getData('query');
52 2
        $foreignKey = $action->getParentId();
53 2
        $field = $action->getParentIdName();
54 2
        if ($field !== null) {
55 2
            $query->where([$field => $foreignKey]);
56 2
        }
57 2
        if ($event->result) {
58 1
            $query = $event->result;
59 1
        }
60
61 2
        return $query;
62
    }
63
64
    /**
65
     * On find entity.
66
     *
67
     * @param Event $event An Event instance
68
     * @return Entity
69
     */
70 6
    public function findEntity(Event $event)
71
    {
72 6
        $action = $event->getSubject();
73 6
        $query = $event->getData('query');
74 6
        $foreignKey = $action->getParentId();
75 6
        $field = $action->getParentIdName();
76 6
        if ($field !== null) {
77 6
            $query->where([$field => $foreignKey]);
78 6
        }
79 6
        if ($event->result) {
80
            $query = $event->result;
81
        }
82
83 6
        return $query;
84
    }
85
86
    /**
87
     * On patch entity.
88
     *
89
     * @param Event $event An Event instance
90
     * @return Entity
91
     */
92 2
    public function patchEntity(Event $event)
93
    {
94 2
        $action = $event->getSubject();
95 2
        $entity = $event->getData('entity');
96 2
        if ($event->result) {
97
            $entity = $event->result;
98
        }
99 2
        $foreignKey = $action->getParentId();
100 2
        $field = $action->getParentIdName();
101 2
        if ($field !== null) {
102 2
            $entity->set($field, $foreignKey);
103 2
        }
104
105 2
        return $entity;
106
    }
107
}
108