BaseAuthModel::toSearchableArray()   B
last analyzed

Complexity

Conditions 10
Paths 8

Size

Total Lines 43
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 10
eloc 31
nc 8
nop 0
dl 0
loc 43
rs 7.6666
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Alive2212\LaravelSmartRestful;
4
5
use App\Resources\Reflection\LaravelReflectionHelper;
0 ignored issues
show
Bug introduced by
The type App\Resources\Reflection\LaravelReflectionHelper was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use Fico7489\Laravel\Pivot\Traits\PivotEventTrait;
7
use Illuminate\Notifications\Notifiable;
8
use Laravel\Passport\HasApiTokens;
9
use Laravel\Scout\Searchable;
10
use Illuminate\Foundation\Auth\User as Authenticatable;
11
use Phoenix\EloquentMeta\MetaTrait;
12
13
14
class BaseAuthModel extends Authenticatable
15
{
16
    use Searchable, PivotEventTrait,Notifiable, HasApiTokens, MetaTrait;
0 ignored issues
show
introduced by
The trait Phoenix\EloquentMeta\MetaTrait requires some properties which are not provided by Alive2212\LaravelSmartRestful\BaseAuthModel: $value, $meta_model
Loading history...
introduced by
The trait Illuminate\Notifications\Notifiable requires some properties which are not provided by Alive2212\LaravelSmartRestful\BaseAuthModel: $email, $phone_number
Loading history...
17
18
    /**
19
     * default relations of method
20
     *
21
     * @var array
22
     */
23
    protected $relation = [];
24
25
    /**
26
     * searchable columns in this model
27
     *
28
     * @var array
29
     */
30
    protected $searchableColumns = [];
31
32
    /**
33
     * searchable columns of one model into this model
34
     *
35
     * @var array
36
     */
37
    protected $searchableInnerColumns = [];
38
39
    /**
40
     * searchable columns of many model into this model
41
     *
42
     * @var array
43
     */
44
    protected $searchableManyInnerColumns = [];
45
46
    /**
47
     * Get the indexable data array for the model.
48
     *
49
     * @return array
50
     */
51
    public function toSearchableArray()
52
    {
53
        if (!sizeof($this->searchableColumns)) {
54
            $result = collect($this);
55
        } else {
56
            $result = collect([
57
                'id' => $this['id'],
58
            ]);
59
            foreach ($this->searchableColumns as $searchableColumn) {
60
                $result->put($searchableColumn, collect($this)->get($searchableColumn));
61
            }
62
        }
63
        if (sizeof($this->searchableInnerColumns)) {
64
            foreach ($this->searchableInnerColumns as $searchableInnerColumnKey => $searchableInnerColumnValue) {
65
                $searchableInnerColumnKey = explode('-', $searchableInnerColumnKey)[0];
66
                $innerModels = explode('.', $searchableInnerColumnKey);
67
                $values = $this;
68
                foreach ($innerModels as $innerModel) {
69
                    $values = $values->{$innerModel};
70
                }
71
                $values = collect($values);
72
                $result->put($searchableInnerColumnKey . '-' . $searchableInnerColumnValue,
73
                    collect($values)->get($searchableInnerColumnValue));
74
            }
75
        }
76
        if (sizeof($this->searchableManyInnerColumns)) {
77
            foreach ($this->searchableManyInnerColumns as $searchableInnerColumnKey => $searchableInnerColumnValue) {
78
                $searchableInnerColumnKey = explode('-', $searchableInnerColumnKey)[0];
79
                $innerModels = explode('.', $searchableInnerColumnKey);
80
                $values = $this;
81
                foreach ($innerModels as $innerModel) {
82
                    $values = $values->{$innerModel};
83
                }
84
                $values = collect($values);
85
                $counter = 0;
86
                foreach ($values as $value) {
87
                    $result->put($searchableInnerColumnKey . '-' . $searchableInnerColumnValue . '-' . $counter,
88
                        collect($value)->get($searchableInnerColumnValue));
89
                    $counter++;
90
                }
91
            }
92
        }
93
        return $result->toArray();
94
    }
95
96
    /**
97
     * get methods names of child class in array type
98
     *
99
     * @return array
100
     */
101
    public function getMethods()
102
    {
103
        return (new LaravelReflectionHelper())->getClassMethodsNames($this,\ReflectionMethod::IS_PUBLIC);
104
    }
105
106
    /**
107
     * @return array
108
     */
109
    public function getRelatedMethods()
110
    {
111
        return (new LaravelReflectionHelper())->getClassRelationMethodsNames($this);
112
    }
113
114
    /**
115
     * @return mixed
116
     */
117
    public function withRelation()
118
    {
119
        return $this->with($this->getRelatedMethods());
120
    }
121
122
    /**
123
     * @return mixed
124
     */
125
    public function loadRelation()
126
    {
127
        return $this->load($this->getRelatedMethods());
128
    }
129
}
130