Completed
Push — master ( ee43b9...6d8f2e )
by Vitaly
06:10
created

FieldsTable::entities()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 9
rs 9.6667
cc 3
eloc 6
nc 2
nop 2
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: VITALYIEGOROV
5
 * Date: 09.12.15
6
 * Time: 09:57
7
 */
8
namespace samsoncms\api;
9
use samsoncms\api\query\FieldNavigation;
10
use samsoncms\api\query\MaterialNavigation;
11
use samsonframework\orm\Condition;
12
use samsonframework\orm\ConditionInterface;
13
use samsonframework\orm\QueryInterface;
14
15
/**
16
 * Material additional fields table.
17
 * @package samsoncms\api
18
 */
19
class FieldsTable
20
{
21
    /** @var integer Navigation identifier for table structure */
22
    protected $navigationID;
23
24
    /** @var integer Table parent material identifier */
25
    protected $materialID;
26
27
    /** @var Field[] Collection field instances that correspond table columns */
28
    protected $fields;
29
30
    /** @var QueryInterface Database query interface */
31
    protected $query;
32
33
    /** @var string Locale identifier */
34
    protected $locale;
35
36
    /** @var array Fields table collection */
37
    protected $collection;
38
39
    /** @return array Get field table column names collection */
40
    public function columns()
41
    {
42
        return array_column($this->fields, Field::F_IDENTIFIER);
43
    }
44
45
    /**
46
     * Get collection of table column values as array.
47
     *
48
     * @param string $fieldID Additional field identifier
49
     * @return array Collection of table column values as array
50
     */
51
    public function values($fieldID)
52
    {
53
        $return = array();
54
        if (isset($this->fields[$fieldID])) {
55
            $return = array_column($this->collection, $fieldID);
56
        }
57
        return $return;
58
    }
59
60
    public function entities($fieldID, $entityIdentifier = Material::ENTITY)
0 ignored issues
show
Unused Code introduced by
The parameter $entityIdentifier 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...
61
    {
62
        $return = array();
63
        $field = &$this->fields[$fieldID];
64
        if (isset($field) && $field->Type) {
65
            $return = array_column($this->collection, $fieldID);
66
        }
67
        return $return;
68
    }
69
70
    /**
71
     * Get field table as multidimensional array.
72
     *
73
     * @return array Field table represented as array
74
     */
75
    public function toArray()
76
    {
77
        return $this->collection;
78
    }
79
80
    /** @return array Collection of table rows(materials) identifiers */
81
    protected function rowIDs()
82
    {
83
        // Get collection of nested materials
84
        return $this->query
85
            ->entity(Material::ENTITY)
86
            ->where(Material::F_DELETION, 1)
87
            ->where(Material::F_PRIMARY, (new MaterialNavigation())->idsByRelationID($this->navigationID))
0 ignored issues
show
Documentation introduced by
(new \samsoncms\api\quer...ID($this->navigationID) is of type boolean|object<samsonfra...rk\orm\RecordInterface>, 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...
88
            ->where(Material::F_PARENT, $this->materialID)
89
            ->orderBy(Material::F_PRIORITY)
90
            ->fields(Material::F_PRIMARY);
91
    }
92
93
    /**
94
     * Build correct localized field request for retrieving additional fields records.
95
     *
96
     * @param Field[] $fields Collection of additional fields
97
     * @return Condition Built condition for query
98
     */
99
    protected function fieldsCondition($fields)
100
    {
101
        // Group fields by localization
102
        $localizedColumns = array();
103
        $notLocalizedColumns = array();
104
        /** @var Field $field Iterate table columns(fields) */
105
        foreach ($fields as $field) {
106
            if ($field->localized()) {
107
                $localizedColumns[] = $field->id;
108
            } else {
109
                $notLocalizedColumns[] = $field->id;
110
            }
111
        }
112
113
        // Create field condition
114
        $fieldsCondition = new Condition(ConditionInterface::DISJUNCTION);
115
        // Create localized condition
116
        if (sizeof($localizedColumns)) {
117
            $localizedCondition = new Condition(ConditionInterface::CONJUNCTION);
118
            $localizedCondition->add(Field::F_PRIMARY, $localizedColumns)
119
                ->add(MaterialField::F_LOCALE, $this->locale);
120
121
            // Add this condition to condition group
122
            $fieldsCondition->addCondition($localizedCondition);
123
        }
124
125
        // Create not localized condition
126
        if (sizeof($notLocalizedColumns)) {
127
            $fieldsCondition->add(Field::F_PRIMARY, $notLocalizedColumns);
128
        }
129
130
        return $fieldsCondition;
131
    }
132
133
    /**
134
     * Fill table with data from database.
135
     */
136
    protected function load()
137
    {
138
        // Get table Fields instances
139
        $this->fields = (new FieldNavigation())->byRelationID($this->navigationID);
0 ignored issues
show
Documentation Bug introduced by
It seems like (new \samsoncms\api\quer...ID($this->navigationID) of type array<integer,*> is incompatible with the declared type array<integer,object<samsoncms\api\Field>> of property $fields.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
140
141
        if (sizeof($rowIDs = $this->rowIDs())) {
142
            /** @var MaterialField $fieldValue Get additional field value instances */
143
            foreach ($this->query->entity(CMS::MATERIAL_FIELD_RELATION_ENTITY)
0 ignored issues
show
Bug introduced by
The expression $this->query->entity(\sa...$this->fields))->exec() of type boolean|object<samsonfra...rk\orm\RecordInterface> is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
Coding Style introduced by
Space found before closing bracket of FOREACH loop
Loading history...
144
                         // Get only needed rows(materials)
145
                         ->where(Material::F_PRIMARY, $rowIDs)
0 ignored issues
show
Documentation introduced by
$rowIDs is of type boolean|object<samsonfra...rk\orm\RecordInterface>, 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...
146
                         ->where(Material::F_DELETION, 1)
147
                         // Get correct localizes field condition for columns
148
                         ->whereCondition($this->fieldsCondition($this->fields))
149
                         ->exec() as $fieldValue
150
            ) {
151
                /** @var Field $field Try to find Field instance by identifier */
152
                $field = &$this->fields[$fieldValue[Field::F_PRIMARY]];
153
                if (isset($field)) {
154
                    /**
155
                     * Store table row(material) as it primary, store columns(Fields)
156
                     * by field primary. Use correct column for value.
157
                     */
158
                    $this->collection[$fieldValue[Material::F_PRIMARY]][$fieldValue[Field::F_PRIMARY]]
159
                        = $fieldValue[$field->valueFieldName()];
160
                }
161
            }
162
        }
163
    }
164
165
    /**
166
     * FieldsTable constructor.
167
     *
168
     * @param QueryInterface $query Database query interface
169
     * @param integer $navigationID Navigation identifier for table structure
170
     * @param integer $materialID Table parent material identifier
171
     * @param string $locale Locale identifier
172
     */
173
    public function __construct(QueryInterface $query, $navigationID, $materialID, $locale = DEFAULT_LOCALE)
174
    {
175
        $this->query = $query;
176
        $this->navigationID = $navigationID;
177
        $this->materialID = $materialID;
178
        $this->locale = $locale;
179
180
        $this->load();
181
    }
182
}
183