RelatedlistTrait::getRelatedListRecordIds()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 7
c 0
b 0
f 0
nc 1
nop 2
dl 0
loc 14
rs 10
1
<?php
2
3
namespace Uccello\Core\Support\Traits;
4
5
use Illuminate\Database\Eloquent\Builder;
6
use Illuminate\Support\Collection;
7
use Uccello\Core\Models\Relatedlist;
8
9
trait RelatedlistTrait
10
{
11
    /**
12
     * Returns record label
13
     * Default: id
14
     *
15
     * @return string
16
     */
17
    public function getRecordLabelAttribute() : string
18
    {
19
        return $this->getKey();
0 ignored issues
show
Bug introduced by
It seems like getKey() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

19
        return $this->/** @scrutinizer ignore-call */ getKey();
Loading history...
20
    }
21
22
    /**
23
     * Retrieves related records linked by an entity field
24
     *
25
     * @param Relatedlist $relatedList
26
     * @param integer $recordId
27
     * @param Builder|null $query
28
     * @param int $start
29
     * @return Collection
30
     */
31
    public function getDependentList(Relatedlist $relatedList, int $recordId, ?Builder $query = null)
32
    {
33
        // Related field
34
        $relatedField = $relatedList->relatedField;
35
        $filter = ['order' => request('order')];
36
37
        $query = $query->where(function (Builder $_query) use ($recordId, $relatedList, $relatedField) {
0 ignored issues
show
Bug introduced by
The method where() does not exist on null. ( Ignorable by Annotation )

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

37
        $query = $query->/** @scrutinizer ignore-call */ where(function (Builder $_query) use ($recordId, $relatedList, $relatedField) {

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
38
            // Search by id
39
            $_query->where($relatedField->column, $recordId);
40
41
            // Search by uuid
42
            $record = ucrecord($recordId, $relatedList->module->model_class);
0 ignored issues
show
Bug introduced by
The property model_class does not seem to exist on Uccello\Core\Models\Module. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
43
            if ($record->uuid ?? false) {
44
                $_query->orWhere($relatedField->column, $record->uuid);
45
            }
46
        })
47
        ->filterBy($filter);
48
49
        return $query;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $query also could return the type Illuminate\Database\Eloquent\Builder which is incompatible with the documented return type Illuminate\Support\Collection.
Loading history...
50
    }
51
52
    /**
53
     * Counts related records linked by an entity field
54
     *
55
     * @param Relatedlist $relatedList
56
     * @param integer $recordId
57
     * @return int
58
     */
59
    public function getDependentListCount(Relatedlist $relatedList, int $recordId) : int
60
    {
61
        // Model
62
        $relatedModel = $relatedList->relatedModule->model_class;
0 ignored issues
show
Bug introduced by
The property model_class does not seem to exist on Uccello\Core\Models\Module. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
63
64
        // Related field
65
        $relatedField = $relatedList->relatedField;
66
67
        return $relatedModel::where(function (Builder $_query) use ($recordId, $relatedList, $relatedField) {
68
            // Search by id
69
            $_query->where($relatedField->column, $recordId);
70
71
            // Search by uuid
72
            $record = ucrecord($recordId, $relatedList->module->model_class);
0 ignored issues
show
Bug introduced by
The property model_class does not seem to exist on Uccello\Core\Models\Module. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
73
            if ($record->uuid ?? false) {
74
                $_query->orWhere($relatedField->column, $record->uuid);
75
            }
76
        })
77
        ->count();
78
    }
79
80
    /**
81
     * Get ids of related records for n-1 relations
82
     *
83
     * @param Relatedlist $relatedList
84
     * @param integer $recordId
85
     * @return Collection
86
     */
87
    public function getDependentListRecordIds(Relatedlist $relatedList, int $recordId) : Collection
88
    {
89
        // Get record
90
        $modelClass = $relatedList->module->model_class;
0 ignored issues
show
Bug introduced by
The property model_class does not seem to exist on Uccello\Core\Models\Module. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
91
        $record = $modelClass::find($recordId);
92
93
        // Get related key name
94
        $relatedModel = new $relatedList->relatedModule->model_class;
95
        $relatedTable = $relatedModel->table;
96
        $relatedPrimaryKey = $relatedModel->getKeyName();
97
98
        // Get related records ids
99
        $relationName = $relatedList->relationName;
100
        return $record->$relationName()->pluck("$relatedTable.$relatedPrimaryKey"); //TODO: Check if it works with uuid
101
    }
102
103
    /**
104
     * Retrieves related records for n-n relations
105
     *
106
     * @param Relatedlist $relatedList
107
     * @param integer $recordId
108
     * @param Builder|null $query
109
     * @param int $start
110
     * @return Collection
111
     */
112
    public function getRelatedList(Relatedlist $relatedList, int $recordId, ?Builder $query = null)
113
    {
114
        $modelClass = $relatedList->module->model_class;
0 ignored issues
show
Bug introduced by
The property model_class does not seem to exist on Uccello\Core\Models\Module. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
115
        $relationName = $relatedList->relationName;
116
117
        $record = $modelClass::find($recordId);
118
        $filter = ['order' => request('order')];
119
120
        $query = $record->$relationName()
121
                        ->filterBy($filter);
122
123
        return $query;
124
    }
125
126
    /**
127
     * Counts related records for n-n relations
128
     *
129
     * @param Relatedlist $relatedList
130
     * @param integer $recordId
131
     * @return int
132
     */
133
    public function getRelatedListCount(Relatedlist $relatedList, int $recordId) : int
134
    {
135
        return $this->getRelatedList($relatedList, $recordId)->count();
136
    }
137
138
    /**
139
     * Get ids of related records for n-n relations
140
     *
141
     * @param Relatedlist $relatedList
142
     * @param integer $recordId
143
     * @return Collection
144
     */
145
    public function getRelatedListRecordIds(Relatedlist $relatedList, int $recordId) : Collection
146
    {
147
        // Get record
148
        $modelClass = $relatedList->module->model_class;
0 ignored issues
show
Bug introduced by
The property model_class does not seem to exist on Uccello\Core\Models\Module. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
149
        $record = $modelClass::find($recordId);
150
151
        // Get related key name
152
        $relatedModel = new $relatedList->relatedModule->model_class;
153
        $relatedTable = $relatedModel->table;
154
        $relatedPrimaryKey = $relatedModel->getKeyName();
155
156
        // Get related records ids
157
        $relationName = $relatedList->relationName;
158
        return $record->$relationName()->pluck("$relatedTable.$relatedPrimaryKey");
159
    }
160
}
161