Completed
Push — master ( dac1ab...1a6ac1 )
by Vitaly
02:50
created

Relational::byIDs()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 8
rs 9.4286
cc 1
eloc 6
nc 1
nop 2
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: VITALYIEGOROV
5
 * Date: 08.12.15
6
 * Time: 22:14
7
 */
8
namespace samsoncms\api\query;
9
10
use samsoncms\api\Material;
11
use samsonframework\orm\QueryInterface;
12
13
/**
14
 * Generic SamsonCMS query for retrieving entities that have relation between each other
15
 * through other relational entity.
16
 *
17
 * @package samsoncms\api
18
 */
19
class Relational
20
{
21
    /** @var string Related entity primary field */
22
    protected $relationPrimary;
23
24
    /** @var string Relation entity identifier */
25
    protected $relationIdentifier;
26
27
    /** @var QueryInterface Database query instance */
28
    protected $query;
29
30
    /** @var string Entity identifier */
31
    protected $identifier;
32
33
    /** @var string Entity primary field name */
34
    protected $primaryField;
35
36
    /** @var array Collection of entity identifiers for filtering */
37
    protected $filteringIDs = array();
38
39
    /**
40
     * Get current entity instances collection by their identifiers.
41
     * Method can accept different query executors.
42
     *
43
     * @param string|array $entityIDs Entity identifier or their collection
44
     * @param string $executor Method name for query execution
45
     * @return mixed[] Collection of entity instances
46
     */
47
    public function byIDs($entityIDs, $executor)
48
    {
49
        return $this->query
50
            ->entity($this->identifier)
51
            ->where($this->primaryField, $entityIDs)
0 ignored issues
show
Bug introduced by
It seems like $entityIDs defined by parameter $entityIDs on line 47 can also be of type array; however, samsonframework\orm\QueryInterface::where() does only seem to accept string|null, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
52
            ->where(Material::F_DELETION, 1)
53
            ->$executor();
54
    }
55
56
    /**
57
     * Entity constructor.
58
     * @param QueryInterface $query Database query instance
59
     * @param string $identifier Entity identifier
60
     * @param string $primary Entity primary identifier
61
     * @param string $relationPrimary Relation entity primary field name
62
     * @param string $relationIdentifier Relation entity identifier
63
     * @param array $filteringIDs Collection of entity identifiers for filtering
64
     */
65
    public function __construct(
66
        QueryInterface $query,
67
        $identifier,
68
        $primary,
69
        $relationPrimary,
70
        $relationIdentifier,
71
        $filteringIDs = array()
72
    ) {
73
        $this->query = $query;
74
        $this->identifier = $identifier;
75
        $this->relationIdentifier = $relationIdentifier;
76
        $this->relationPrimary = $relationPrimary;
77
        $this->filteringIDs = $filteringIDs;
78
        $this->primaryField = $primary;
79
    }
80
81
    /**
82
     * Get current entity identifiers collection by navigation identifier.
83
     *
84
     * @param string $relationID Relation entity identifier
85
     * @param mixed $relationValue Relation entity value
86
     * @param array $filteringIDs Collection of entity identifiers for filtering query
87
     * @return array Collection of entity identifiers filtered by navigation identifier.
88
     */
89
    public function idsByRelationID($relationID, $relationValue = null, array $filteringIDs = array())
90
    {
91
        // Prepare query
92
        $this->query
93
            ->entity($this->relationIdentifier)
94
            ->where($this->relationPrimary, $relationID)
95
            ->where(Material::F_DELETION, 1);
96
97
        // Add entity identifier filter if passed
98
        if (sizeof($filteringIDs)) {
99
            $this->query->where($this->primaryField, $filteringIDs);
0 ignored issues
show
Documentation introduced by
$filteringIDs is of type array, but the function expects a string|null.

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...
100
        }
101
102
        // Perform database query and get only material identifiers collection
103
        return $this->query->fields($this->primaryField);
104
    }
105
106
    /**
107
     * Retrieve entities from database.
108
     *
109
     * @param string|array $relationID Relation entity identifier or collection
110
     * @param mixed $relationValue Relation entity value
111
     * @param string $executor Query execution function name
112
     * @return mixed[] Collection of entity instances for this relation identifier
113
     */
114
    protected function retrieve($relationID, $relationValue, $executor)
115
    {
116
        $return = array();
117
        /** @var array $ids Collection of entity identifiers filtered by additional field */
118
        if (sizeof($ids = $this->idsByRelationID($relationID, $relationValue, $this->filteringIDs))) {
0 ignored issues
show
Bug introduced by
It seems like $relationID defined by parameter $relationID on line 114 can also be of type array; however, samsoncms\api\query\Relational::idsByRelationID() does only seem to accept string, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
119
            $return = $this->byIDs($ids, $executor);
0 ignored issues
show
Documentation introduced by
$ids is of type boolean|object<samsonfra...rk\orm\RecordInterface>, but the function expects a string|array.

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...
120
        }
121
122
        return $return;
123
    }
124
125
    /**
126
     * Get current entity instances collection by navigation identifier.
127
     *
128
     * @param string $relationID Relation entity identifier
129
     * @param mixed $relationValue Relation entity value
130
     * @return mixed[] Collection of entity instances
131
     */
132
    public function byRelationID($relationID, $relationValue = null)
133
    {
134
        return $this->retrieve($relationID, $relationValue, 'exec');
135
    }
136
137
    /**
138
     * Get current entity instances amount by navigation identifier.
139
     *
140
     * @param string $relationID Relation entity identifier
141
     * @param mixed $relationValue Relation entity value
142
     * @return integer Amount of entities related to Navigation identifier
143
     */
144
    public function amountByRelationID($relationID, $relationValue = null)
145
    {
146
        return $this->retrieve($relationID, $relationValue, 'count');
147
    }
148
}
149