CrudRelationsExtension::_attachAssociations()   A
last analyzed

Complexity

Conditions 5
Paths 4

Size

Total Lines 35

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 26
CRAP Score 5

Importance

Changes 0
Metric Value
cc 5
nc 4
nop 2
dl 0
loc 35
rs 9.0488
c 0
b 0
f 0
ccs 26
cts 26
cp 1
crap 5
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 CakeDC\Api\Service\Action\Action;
15
use CakeDC\Api\Service\Action\CrudAction;
16
use Cake\Event\Event;
17
use Cake\Event\EventListenerInterface;
18
use Cake\ORM\Association;
19
use Cake\ORM\Query;
20
use Cake\Utility\Inflector;
21
22
/**
23
 * Class CrudRelationsExtension
24
 *
25
 * Allow to include relations for fetched entities to return by index, view or edit crud actions.
26
 * Currently limited only by HasOne and BelongsTo associations.
27
 *
28
 * @package CakeDC\Api\Service\Action\Extension
29
 */
30
class CrudRelationsExtension extends Extension implements EventListenerInterface
31
{
32
33
    /**
34
     * Returns a list of events this object is implementing. When the class is registered
35
     * in an event manager, each individual method will be associated with the respective event.
36
     *
37
     * @return array
38
     */
39 4
    public function implementedEvents()
40
    {
41
        return [
42 4
            'Action.Crud.onFindEntities' => 'findEntities',
43 4
            'Action.Crud.onFindEntity' => 'findEntity',
44 4
        ];
45
    }
46
47
    /**
48
     * On find entity
49
     *
50
     * @param Event $event An Event instance.
51
     * @return \Cake\ORM\Query
52
     */
53 4
    public function findEntity(Event $event)
54
    {
55 4
        return $this->_attachAssociations($event->getSubject(), $event->getData('query'));
0 ignored issues
show
Documentation introduced by
$event->getSubject() is of type object|null, but the function expects a object<CakeDC\Api\Service\Action\CrudAction>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
56
    }
57
58
    /**
59
     * On find entities.
60
     *
61
     * @param Event $event An Event instance.
62
     * @return \Cake\ORM\Query
63
     */
64
    public function findEntities(Event $event)
65
    {
66
        return $this->_attachAssociations($event->getSubject(), $event->getData('query'));
0 ignored issues
show
Documentation introduced by
$event->getSubject() is of type object|null, but the function expects a object<CakeDC\Api\Service\Action\CrudAction>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
67
    }
68
69
    /**
70
     * Checks if endpoint returns additional associations.
71
     *
72
     * @param CrudAction $action A CrudAction instance.
73
     * @return array|bool
74
     */
75 4
    protected function _includeAssociations(CrudAction $action)
76
    {
77 4
        $data = $action->getData();
78 4
        if (!empty($data['include_associations']) && empty($data['include_relations'])) {
79
            $data['include_relations'] = $data['include_associations'];
80
        }
81 4
        $exists = (is_array($data) && !empty($data['include_relations']));
82 4
        if (!$exists) {
83 3
            return false;
84
        }
85 1
        $associations = $data['include_relations'];
86 1
        if (!is_array($associations)) {
87 1
            return explode(',', $associations);
88
        }
89
        if (is_array($associations) && count($associations) > 0) {
90
            return $associations;
91
        }
92
93
        return false;
94
    }
95
96
    /**
97
     * Checks if endpoint returns direct associations, i.e. all belongsTo and all hasOne.
98
     *
99
     * @param CrudAction $action An CrudAction instance.
100
     * @return bool
101
     */
102 3
    protected function _includeDirectAssociations(CrudAction $action)
103
    {
104 3
        $data = $action->getData();
105
106 3
        return (is_array($data) && !empty($data['include_direct']));
107
    }
108
109
    /**
110
     * @param CrudAction $action An Action instance.
111
     * @param Query $query A Query instance.
112
     * @return mixed
113
     */
114 4
    protected function _attachAssociations(CrudAction $action, Query $query)
115
    {
116 4
        $associations = $this->_includeAssociations($action);
117 4
        if (empty($associations) && $this->_includeDirectAssociations($action)) {
118
            $relations = $action
119 1
                ->getTable()
120 1
                ->associations()
121 1
                ->getByType(['HasOne', 'BelongsTo']);
122 1
            $associations = collection($relations)
123
                ->map(function (Association $relation) {
124 1
                    return $relation->getTarget()->getTable();
125 1
                })
126 1
                ->toArray();
127 1
        }
128 4
        if (empty($associations)) {
129 2
            return $query;
130
        }
131
132 2
        $tables = collection($associations)
133
            ->map(function ($name) {
134 2
                return Inflector::camelize($name);
135 2
            })
136 2
            ->toArray();
137
138 2
        collection($tables)->each(function ($name) use ($query, $action) {
139 2
            $assoc = $action->getTable()->getAssociation($name);
140 2
            if ($assoc !== null) {
141 2
                $query->select($assoc);
0 ignored issues
show
Documentation introduced by
$assoc is of type object<Cake\ORM\Association>, but the function expects a callable.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
142 2
            }
143 2
        });
144 2
        $query->select($action->getTable());
0 ignored issues
show
Documentation introduced by
$action->getTable() is of type object<Cake\ORM\Table>, but the function expects a callable.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
145 2
        $query->contain($tables);
146
147 2
        return $query;
148
    }
149
}
150