MorphTo::getEagerResults()   A
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 21
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 2
Bugs 2 Features 0
Metric Value
cc 5
eloc 14
c 2
b 2
f 0
nc 5
nop 1
dl 0
loc 21
ccs 0
cts 14
cp 0
crap 30
rs 9.4888
1
<?php
2
3
namespace Nip\Records\Relations;
4
5
use Exception;
6
use MongoDB\Driver\Query;
7
use Nip\Records\AbstractModels\Record;
8
use Nip\Records\Collections\Collection;
9
use Nip\Records\AbstractModels\RecordManager;
10
use Nip\Records\Relations\Exceptions\ModelNotLoadedInRelation;
11
use Nip\Records\Relations\Traits\HasMorphTypeTrait;
12
use Nip\HelperBroker;
13
use Nip_Helper_Arrays as ArraysHelper;
14
15
/**
16
 * Class MorphToMany
17
 * @package Nip\Records\Relations
18
 */
19
class MorphTo extends BelongsTo
20
{
21
    use HasMorphTypeTrait;
22
23
    /** @noinspection PhpMissingParentCallCommonInspection
24
     * @return string
25
     * @throws ModelNotLoadedInRelation
26
     */
27 3
    public function getWithClass()
28
    {
29 3
        $type = $this->getMorphType();
30 2
        $typePlural = inflector()->pluralize($type);
31 2
        return $typePlural;
32
    }
33
34
    /**
35
     * @return mixed
36
     * @throws ModelNotLoadedInRelation
37
     */
38 3
    public function getMorphType()
39
    {
40 3
        if ($this->getItem() instanceof Record) {
0 ignored issues
show
introduced by
$this->getItem() is always a sub-type of Nip\Records\AbstractModels\Record.
Loading history...
41 2
            return $this->getItem()->{$this->getMorphTypeField()};
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->getMorphTypeField() targeting Nip\Records\Relations\MorphTo::getMorphTypeField() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
42
        }
43 1
        throw new ModelNotLoadedInRelation(
44 1
            $this->debugString()
45
        );
46
    }
47
48
    /**
49
     * @param $params
50
     * @throws Exception
51
     */
52 1
    public function addParams($params)
53
    {
54 1
        $this->checkParamMorphPrefix($params);
55 1
        $this->checkParamMorphTypeField($params);
56 1
        parent::addParams($params);
57 1
    }
58
59
    /**
60
     * @inheritdoc
61
     */
62
    public function getEagerResults($collection)
63
    {
64
        if ($collection->count() < 1) {
65
            if ($this->getItem() instanceof Record) {
0 ignored issues
show
introduced by
$this->getItem() is always a sub-type of Nip\Records\AbstractModels\Record.
Loading history...
66
                return $this->getWith()->newCollection();
67
            } else {
68
                return new Collection();
69
            }
70
        }
71
        $types = $this->getTypesFromCollection($collection);
72
        $results = new Collection();
73
        foreach ($types as $type) {
74
            $manager = $this->getModelManagerInstance($type);
75
            $query = $this->getEagerQueryType($collection, $manager);
76
            $typeCollection = $manager->findByQuery($query);
0 ignored issues
show
Bug introduced by
$query of type MongoDB\Driver\Query is incompatible with the type Nip\Database\Query\AbstractQuery expected by parameter $query of Nip\Records\AbstractMode...dManager::findByQuery(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

76
            $typeCollection = $manager->findByQuery(/** @scrutinizer ignore-type */ $query);
Loading history...
77
            foreach ($typeCollection as $item) {
78
                $results->add($item);
79
            }
80
        }
81
82
        return $results;
83
    }
84
85
    /**
86
     * @param Collection $collection
87
     * @param $manager
88
     * @return Query
89
     * @throws Exception
90
     */
91 2
    public function getEagerQueryType(Collection $collection, $manager)
92
    {
93 2
        $fkList = $this->getEagerFkListType($collection, $manager);
94 2
        $query = $manager->newQuery();
95 2
        $query->where($manager->getPrimaryKey() . ' IN ?', $fkList);
96 2
        return $query;
97
    }
98
99
    /**
100
     * @param Collection $collection
101
     * @param RecordManager $manager
102
     * @return array
103
     */
104 2
    public function getEagerFkListType(Collection $collection, $manager)
105
    {
106 2
        $foreignKey = $this->getFK();
107 2
        $typeField = $this->getMorphTypeField();
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $typeField is correct as $this->getMorphTypeField() targeting Nip\Records\Relations\MorphTo::getMorphTypeField() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
108 2
        $type = $manager->getMorphName();
0 ignored issues
show
Bug introduced by
The method getMorphName() does not exist on Nip\Records\AbstractModels\RecordManager. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

108
        /** @scrutinizer ignore-call */ 
109
        $type = $manager->getMorphName();
Loading history...
109 2
        $return = [];
110
111 2
        foreach ($collection as $item) {
112 2
            if ($item->{$typeField} == $type) {
113 2
                $return[] = $item->{$foreignKey};
114
            }
115
        }
116
117 2
        return array_unique($return);
118
    }
119
120
    /** @noinspection PhpMissingParentCallCommonInspection
121
     * @param Record $record
122
     * @return array
123
     * @throws \Exception
124
     */
125
    protected function getDictionaryKey(Record $record)
126
    {
127
        $key = $record->getManager()->getMorphName();
128
        $key .= '-' . $record->getPrimaryKey();
129
130
        return $key;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $key returns the type string which is incompatible with the documented return type array.
Loading history...
131
    }
132
133
    /**
134
     * @param $dictionary
135
     * @param $collection
136
     * @param $record
137
     * @return mixed
138
     */
139
    public function getResultsFromCollectionDictionary($dictionary, $collection, $record)
140
    {
141
        $dictionaryKey = $record->{$this->getMorphTypeField()};
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->getMorphTypeField() targeting Nip\Records\Relations\MorphTo::getMorphTypeField() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
142
        $dictionaryKey .= '-' . $record->{$this->getFK()};
143
        if (isset($dictionary[$dictionaryKey])) {
144
            return $dictionary[$dictionaryKey];
145
        }
146
147
        return null;
148
    }
149
150
    /**
151
     * @param Query $query
152
     * @param array $fkList
153
     * @return Query
154
     * @throws Exception
155
     */
156
    protected function populateEagerQueryFromFkList($query, $fkList)
157
    {
158
        return $query;
159
    }
160
161
    /**
162
     * @param $collection
163
     * @return array
164
     */
165
    public function getTypesFromCollection($collection)
166
    {
167
        $type = $this->getMorphTypeField();
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $type is correct as $this->getMorphTypeField() targeting Nip\Records\Relations\MorphTo::getMorphTypeField() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
168
169
        /** @var ArraysHelper $arrayHelper */
170
        $arrayHelper = HelperBroker::get('Arrays');
171
        return $arrayHelper->pluck($collection, $type);
172
    }
173
}
174