Completed
Push — master ( b2dc0b...f20ddf )
by Vitaly
04:49
created

Generic::findByAdditionalField()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 6
rs 9.4286
cc 1
eloc 4
nc 1
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
    /**
29
     * Add condition to current query.
30
     *
31
     * @param string $fieldName Entity field name
32
     * @param string $fieldValue Value
33
     * @return self Chaining
34
     */
35
    public function where($fieldName, $fieldValue = null)
36
    {
37
        // Try to find entity additional field
38
        if (property_exists(static::$identifier, $fieldName)) {
39
            // Store additional field filter value
40
            $this->fieldFilter[$fieldName] = $fieldValue;
41
        }
42
43
        return $this;
44
    }
45
46
    /**
47
     * Get collection of entity identifiers filtered by navigation identifiers.
48
     *
49
     * @param array $entityIDs Additional collection of entity identifiers for filtering
50
     * @return array Collection of material identifiers by navigation identifiers
51
     */
52
    protected function findByNavigationIDs($entityIDs)
53
    {
54
        return (new MaterialNavigation($entityIDs))->idsByRelationID(static::$navigationIDs);
55
    }
56
57
    /**
58
     * Get collection of entity identifiers filtered by additional field and its value.
59
     *
60
     * @param array $additionalFields Collection of additional field identifiers => values
61
     * @param array $entityIDs Additional collection of entity identifiers for filtering
62
     * @return array Collection of material identifiers by navigation identifiers
63
     */
64
    protected function findByAdditionalFields($additionalFields, $entityIDs = array())
65
    {
66
        // Iterate all additional fields needed for filter entity
67
        foreach ($additionalFields as $fieldID => $fieldValue) {
68
            // Get collection of entity identifiers passing already found identifiers
69
            $entityIDs = (new MaterialField($entityIDs))->idsByRelationID($fieldID, $fieldValue);
70
71
            // Stop execution if we have no entities found at this step
72
            if (!sizeof($entityIDs)) {
73
                break;
74
            }
75
        }
76
77
        return $entityIDs;
78
    }
79
80
    /**
81
     * Perform SamsonCMS query and get entities collection.
82
     *
83
     * @return mixed[] Collection of found entities
84
     */
85
    public function find()
86
    {
87
        // TODO: Find and describe approach with maximum generic performance
88
        $entityIDs = $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...
89
        $entityIDs = $this->findByAdditionalFields($this->fieldFilter, $entityIDs);
0 ignored issues
show
Documentation introduced by
$entityIDs 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...
90
91
        return (new Material(static::$identifier))->byIDs($entityIDs, 'exec');
92
    }
93
}
94