Completed
Push — master ( 2a113e...b2dc0b )
by Vitaly
04:31
created

Generic::find()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 16
rs 9.4286
cc 3
eloc 9
nc 3
nop 0
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: VITALYIEGOROV
5
 * Date: 08.12.15
6
 * Time: 23:11
7
 */
8
namespace samsoncms\api\query;
9
10
use samson\activerecord\dbQuery;
11
use samsonframework\orm\Query;
12
13
/**
14
 * Material with additional fields query.
15
 * @package samsoncms\api
16
 */
17
class Generic
18
{
19
    /** @var string Entity identifier */
20
    protected static $identifier;
21
22
    /** @var string Entity navigation identifiers */
23
    protected static $navigationIDs;
24
25
    /** @var array Collection of entity field filter */
26
    protected $fieldFilter;
27
28
    /** @var array Collection of matching entity identifiers */
29
    protected $entityIDs;
30
31
    /**
32
     * Add condition to current query.
33
     *
34
     * @param string $fieldName Entity field name
35
     * @param string $fieldValue Value
36
     * @param string $relation Relation between field name and its value
37
     * @return self Chaining
38
     */
39
    public function where($fieldName, $fieldValue = null, $relation = '=')
0 ignored issues
show
Unused Code introduced by
The parameter $relation is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
40
    {
41
        // Try to find entity additional field
42
        if (property_exists(static::$identifier, $fieldName)) {
43
            // Store additional field filter value
44
            $this->fieldFilter[$fieldName] = $fieldValue;
45
        }
46
47
        return $this;
48
    }
49
50
    /**
51
     * Get collection of entity identifiers filtered by navigation identifiers.
52
     *
53
     * @param array $entityIDs Additional collection of entity identifiers for filtering
54
     * @return array Collection of material identifiers by navigation identifiers
55
     */
56
    protected function findByNavigationIDs($entityIDs)
57
    {
58
        return (new MaterialNavigation($entityIDs))->idsByRelationID(static::$navigationIDs);
59
    }
60
61
    /**
62
     * Get collection of entity identifiers filtered by additional field and its value.
63
     *
64
     * @param array $additionalFields Collection of additional field identifiers => values
65
     * @param array $entityIDs Additional collection of entity identifiers for filtering
66
     * @return array Collection of material identifiers by navigation identifiers
67
     */
68
    protected function findByAdditionalFields($additionalFields, $entityIDs = array())
69
    {
70
        // Iterate all additional fields needed for filter entity
71
        foreach ($additionalFields as $fieldID => $fieldValue) {
72
            // Get collection of entity identifiers passing already found identifiers
73
            $entityIDs = (new MaterialField($entityIDs))->idsByRelationID($fieldID, $fieldValue);
74
75
            // Stop execution if we have no entities found at this step
76
            if (!sizeof($entityIDs)) {
77
                break;
78
            }
79
        }
80
81
        return $entityIDs;
82
    }
83
84
    /** @return array Collection of material identifiers by navigation identifiers */
85
    protected function findByAdditionalField()
86
    {
87
        $return = (new MaterialField($idsByNavigation))
0 ignored issues
show
Bug introduced by
The variable $idsByNavigation does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Unused Code introduced by
$return is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
88
            ->byRelationID($this->fieldFilter[]);
89
        return (new MaterialNavigation())->idsByRelationID(static::$navigationIDs);
90
    }
91
92
    /**
93
     * Perform SamsonCMS query and get entities collection.
94
     *
95
     * @return mixed[] Collection of found entities
96
     */
97
    public function find()
98
    {
99
        $return = array();
100
        /** @var array $idsByNavigation First step - filter by navigation */
101
        if (sizeof($idsByNavigation = $this->findByNavigationIDs())) {
0 ignored issues
show
Bug introduced by
The call to findByNavigationIDs() misses a required argument $entityIDs.

This check looks for function calls that miss required arguments.

Loading history...
102
            // Second step filter by additional field value
103
            if (sizeof($this->fieldFilter)) {
104
                $return = (new MaterialField($idsByNavigation))
0 ignored issues
show
Documentation introduced by
$idsByNavigation is of type boolean|object<samsonfra...rk\orm\RecordInterface>, but the function expects a 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...
105
                    ->byRelationID($this->fieldFilter[]);
106
            } else { // Just return entities filtered by navigation
107
                return (new Material($idsByNavigation, static::$identifier))->byIDs($idsByNavigation, 'exec');
0 ignored issues
show
Documentation introduced by
$idsByNavigation is of type boolean|object<samsonfra...rk\orm\RecordInterface>, but the function expects a 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...
Documentation introduced by
$idsByNavigation 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...
108
            }
109
        }
110
111
        return $return;
112
    }
113
}
114