Completed
Push — master ( 06b50b...ceafbf )
by Vitaly
03:00
created

Base   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 120
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
c 1
b 0
f 0
lcom 1
cbo 1
dl 0
loc 120
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A idsByRelationID() 0 16 2
A byIDs() 0 7 1
A retrieve() 0 10 2
A byRelationID() 0 4 1
A amountByRelationID() 0 4 1
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 Base
19
{
20
    /** Deletion flag field name */
21
    const DELETE_FLAG_FIELD = 'Active';
22
23
    /** @var QueryInterface Database query instance */
24
    protected $query;
25
26
    /** @var string Entity identifier */
27
    protected $identifier;
28
29
    /** @var string Entity primary field name */
30
    protected $primaryField;
31
32
    /** @var string Related entity primary field */
33
    protected $relationPrimary;
34
35
    /** @var string Relation entity identifier */
36
    protected $relationIdentifier;
37
38
    /**
39
     * Entity constructor.
40
     * @param QueryInterface $query Database query instance
41
     * @param string $identifier Entity identifier
42
     * @param string $relationPrimary Relation entity primary field name
43
     * @param string $relationIdentifier Relation entity identifier
44
     */
45
    public function __construct(QueryInterface $query, $identifier, $relationPrimary, $relationIdentifier)
46
    {
47
        $this->query = $query;
48
        $this->identifier = $identifier;
49
        $this->primaryField = $identifier::$_primary;
50
        $this->relationPrimary = $relationPrimary;
51
        $this->relationIdentifier = $relationIdentifier;
52
    }
53
54
    /**
55
     * Get current entity identifiers collection by navigation identifier.
56
     *
57
     * @param string $relationID Relation entity identifier
58
     * @param mixed $relationValue Relation entity value
59
     * @param array $filteringIDs Collection of entity identifiers for filtering query
60
     * @return array Collection of entity identifiers filtered by navigation identifier.
61
     */
62
    public function idsByRelationID($relationID, $relationValue = null, $filteringIDs = null)
63
    {
64
        // Prepare query
65
        $this->query
66
            ->entity($this->relationIdentifier)
67
            ->where($this->relationPrimary, $relationID)
68
            ->where(self::DELETE_FLAG_FIELD, 1);
69
70
        // Add entity identifier filter if passed
71
        if (isset($filteringIDs)) {
72
            $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...
73
        }
74
75
        // Perform database query and get only material identifiers collection
76
        return $this->query->fields($this->primaryField);
77
    }
78
79
    /**
80
     * Get current entity instances collection by their identifiers.
81
     * Method can accept different query executors.
82
     *
83
     * @param string|array $entityIDs Entity identifier or their collection
84
     * @param string $executor Method name for query execution
85
     * @return mixed[] Collection of entity instances
86
     */
87
    public function byIDs($entityIDs, $executor)
88
    {
89
        return $this->query
90
            ->where($this->primaryField, $entityIDs)
0 ignored issues
show
Bug introduced by
It seems like $entityIDs defined by parameter $entityIDs on line 87 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...
91
            ->where(self::DELETE_FLAG_FIELD, 1)
92
            ->$executor();
93
    }
94
95
    /**
96
     * Retrieve entities from database.
97
     *
98
     * @param string|array $relationID Relation entity identifier or collection
99
     * @param mixed $relationValue Relation entity value
100
     * @param string $executor Query execution function name
101
     * @return mixed[] Collection of entity instances for this relation identifier
102
     */
103
    protected function retrieve($relationID, $relationValue, $executor)
104
    {
105
        $return = array();
106
        /** @var array $materialIds Collection of entity identifiers filtered by additional field */
107
        if (sizeof($materialIds = $this->idsByRelationID($relationID, $relationValue))) {
0 ignored issues
show
Bug introduced by
It seems like $relationID defined by parameter $relationID on line 103 can also be of type array; however, samsoncms\api\query\Base::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...
108
            $return = $this->byIDs($materialIds, $executor);
0 ignored issues
show
Documentation introduced by
$materialIds 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...
109
        }
110
111
        return $return;
112
    }
113
114
    /**
115
     * Get current entity instances collection by navigation identifier.
116
     *
117
     * @param string $relationID Relation entity identifier
118
     * @param mixed $relationValue Relation entity value
119
     * @return mixed[] Collection of entity instances
120
     */
121
    public function byRelationID($relationID, $relationValue = null)
122
    {
123
        return $this->retrieve($relationID, $relationValue, 'exec');
124
    }
125
126
    /**
127
     * Get current entity instances amount by navigation identifier.
128
     *
129
     * @param string $relationID Relation entity identifier
130
     * @param mixed $relationValue Relation entity value
131
     * @return integer Amount of entities related to Navigation identifier
132
     */
133
    public function amountByRelationID($relationID, $relationValue = null)
134
    {
135
        return $this->retrieve($relationID, $relationValue, 'count');
136
    }
137
}
138