Passed
Push — master ( 6dae54...a3bb83 )
by Babak
02:18
created

BaseAuthLumenModel   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 114
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 41
dl 0
loc 114
rs 10
c 0
b 0
f 0
wmc 14

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getRelatedMethods() 0 3 1
A loadRelation() 0 3 1
A withRelation() 0 3 1
A getMethods() 0 3 1
B toSearchableArray() 0 43 10
1
<?php
2
3
namespace Alive2212\LaravelSmartRestful;
4
5
use Illuminate\Auth\Authenticatable;
6
use Laravel\Lumen\Auth\Authorizable;
0 ignored issues
show
Bug introduced by
The type Laravel\Lumen\Auth\Authorizable 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...
7
use Illuminate\Database\Eloquent\Model;
8
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
9
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
10
use Laravel\Scout\Searchable;
11
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...
12
13
class BaseAuthLumenModel extends Model implements AuthenticatableContract, AuthorizableContract
14
{
15
    use Authenticatable, Authorizable,Searchable;
16
17
    /**
18
     * default relations of method
19
     *
20
     * @var array
21
     */
22
    protected $relation = [];
23
24
    /**
25
     * searchable columns in this model
26
     *
27
     * @var array
28
     */
29
    protected $searchableColumns = [];
30
31
    /**
32
     * searchable columns of one model into this model
33
     *
34
     * @var array
35
     */
36
    protected $searchableInnerColumns = [];
37
38
    /**
39
     * searchable columns of many model into this model
40
     *
41
     * @var array
42
     */
43
    protected $searchableManyInnerColumns = [];
44
45
    /**
46
     * Get the indexable data array for the model.
47
     *
48
     * @return array
49
     */
50
    public function toSearchableArray()
51
    {
52
        if (!sizeof($this->searchableColumns)) {
53
            $result = collect($this);
54
        } else {
55
            $result = collect([
56
                'id' => $this['id'],
57
            ]);
58
            foreach ($this->searchableColumns as $searchableColumn) {
59
                $result->put($searchableColumn, collect($this)->get($searchableColumn));
60
            }
61
        }
62
        if (sizeof($this->searchableInnerColumns)) {
63
            foreach ($this->searchableInnerColumns as $searchableInnerColumnKey => $searchableInnerColumnValue) {
64
                $searchableInnerColumnKey = explode('-', $searchableInnerColumnKey)[0];
65
                $innerModels = explode('.', $searchableInnerColumnKey);
66
                $values = $this;
67
                foreach ($innerModels as $innerModel) {
68
                    $values = $values->{$innerModel};
69
                }
70
                $values = collect($values);
71
                $result->put($searchableInnerColumnKey . '-' . $searchableInnerColumnValue,
72
                    collect($values)->get($searchableInnerColumnValue));
73
            }
74
        }
75
        if (sizeof($this->searchableManyInnerColumns)) {
76
            foreach ($this->searchableManyInnerColumns as $searchableInnerColumnKey => $searchableInnerColumnValue) {
77
                $searchableInnerColumnKey = explode('-', $searchableInnerColumnKey)[0];
78
                $innerModels = explode('.', $searchableInnerColumnKey);
79
                $values = $this;
80
                foreach ($innerModels as $innerModel) {
81
                    $values = $values->{$innerModel};
82
                }
83
                $values = collect($values);
84
                $counter = 0;
85
                foreach ($values as $value) {
86
                    $result->put($searchableInnerColumnKey . '-' . $searchableInnerColumnValue . '-' . $counter,
87
                        collect($value)->get($searchableInnerColumnValue));
88
                    $counter++;
89
                }
90
            }
91
        }
92
        return $result->toArray();
93
    }
94
95
    /**
96
     * get methods names of child class in array type
97
     *
98
     * @return array
99
     */
100
    public function getMethods()
101
    {
102
        return (new LaravelReflectionHelper())->getClassMethodsNames($this,\ReflectionMethod::IS_PUBLIC);
103
    }
104
105
    /**
106
     * @return array
107
     */
108
    public function getRelatedMethods()
109
    {
110
        return (new LaravelReflectionHelper())->getClassRelationMethodsNames($this);
111
    }
112
113
    /**
114
     * @return mixed
115
     */
116
    public function withRelation()
117
    {
118
        return $this->with($this->getRelatedMethods());
119
    }
120
121
    /**
122
     * @return mixed
123
     */
124
    public function loadRelation()
125
    {
126
        return $this->load($this->getRelatedMethods());
127
    }
128
}
129
130
131
132
133