Completed
Push — master ( 646730...43a697 )
by Vitaly
05:45
created

Relational::retrieve()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 10
rs 9.4286
cc 2
eloc 5
nc 2
nop 3
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 samsonframework\orm\QueryInterface;
11
12
/**
13
 * Generic SamsonCMS query for retrieving entities that have relation between each other
14
 * through other relational entity.
15
 *
16
 * @package samsoncms\api
17
 */
18
class Relational extends Base
19
{
20
    /** @var string Related entity primary field */
21
    protected $relationPrimary;
22
23
    /** @var string Relation entity identifier */
24
    protected $relationIdentifier;
25
26
    /**
27
     * Entity constructor.
28
     * @param QueryInterface $query Database query instance
29
     * @param string $identifier Entity identifier
30
     * @param string $relationPrimary Relation entity primary field name
31
     * @param string $relationIdentifier Relation entity identifier
32
     * @param array $filteringIDs Collection of entity identifiers for filtering
33
     */
34
    public function __construct(
35
        QueryInterface $query,
36
        $identifier,
37
        $relationPrimary,
38
        $relationIdentifier,
39
        $filteringIDs = array()
40
    ) {
41
        parent::__construct($query, $identifier, $filteringIDs);
42
43
        $this->relationPrimary = $relationPrimary;
44
        $this->relationIdentifier = $relationIdentifier;
45
    }
46
47
    /**
48
     * Get current entity identifiers collection by navigation identifier.
49
     *
50
     * @param string $relationID Relation entity identifier
51
     * @param mixed $relationValue Relation entity value
52
     * @param array $filteringIDs Collection of entity identifiers for filtering query
53
     * @return array Collection of entity identifiers filtered by navigation identifier.
54
     */
55
    public function idsByRelationID($relationID, $relationValue = null, array $filteringIDs = array())
56
    {
57
        // Prepare query
58
        $this->query
59
            ->entity($this->relationIdentifier)
60
            ->where($this->relationPrimary, $relationID)
61
            ->where(self::DELETE_FLAG_FIELD, 1);
62
63
        // Add entity identifier filter if passed
64
        if (sizeof($filteringIDs)) {
65
            $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...
66
        }
67
68
        // Perform database query and get only material identifiers collection
69
        return $this->query->fields($this->primaryField);
70
    }
71
72
    /**
73
     * Retrieve entities from database.
74
     *
75
     * @param string|array $relationID Relation entity identifier or collection
76
     * @param mixed $relationValue Relation entity value
77
     * @param string $executor Query execution function name
78
     * @return mixed[] Collection of entity instances for this relation identifier
79
     */
80
    protected function retrieve($relationID, $relationValue, $executor)
81
    {
82
        $return = array();
83
        /** @var array $ids Collection of entity identifiers filtered by additional field */
84
        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 80 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...
85
            $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...
86
        }
87
88
        return $return;
89
    }
90
91
    /**
92
     * Get current entity instances collection by navigation identifier.
93
     *
94
     * @param string $relationID Relation entity identifier
95
     * @param mixed $relationValue Relation entity value
96
     * @return mixed[] Collection of entity instances
97
     */
98
    public function byRelationID($relationID, $relationValue = null)
99
    {
100
        return $this->retrieve($relationID, $relationValue, 'exec');
101
    }
102
103
    /**
104
     * Get current entity instances amount by navigation identifier.
105
     *
106
     * @param string $relationID Relation entity identifier
107
     * @param mixed $relationValue Relation entity value
108
     * @return integer Amount of entities related to Navigation identifier
109
     */
110
    public function amountByRelationID($relationID, $relationValue = null)
111
    {
112
        return $this->retrieve($relationID, $relationValue, 'count');
113
    }
114
}
115