Completed
Push — master ( c3faac...8e8eee )
by wen
13:29
created

View::getApplies()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
dl 0
loc 4
rs 10
c 1
b 0
f 1
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace Sco\Admin\View;
4
5
use Illuminate\Contracts\Support\Arrayable;
6
use Illuminate\Database\Eloquent\Builder;
7
use Sco\Admin\Contracts\RepositoryInterface;
8
use Sco\Admin\Contracts\View\Extensions\ExtensionInterface;
9
use Sco\Admin\Contracts\View\ViewInterface;
10
use Sco\Admin\View\Extensions\Applies;
11
use Sco\Admin\View\Extensions\Filters;
12
use Sco\Admin\View\Extensions\Scopes;
13
14
abstract class View implements ViewInterface, Arrayable
15
{
16
    /**
17
     * @var array
18
     */
19
    protected $with = [];
20
21
    /**
22
     * @var RepositoryInterface
23
     */
24
    protected $repository;
25
26
    protected $type;
27
28
    protected $extensions;
29
30
    public function __construct()
31
    {
32
        $this->extensions = new Extensions();
33
34
        $this->extend('scopes', new Scopes());
35
        $this->extend('applies', new Applies());
36
        $this->extend('filters', new Filters());
37
    }
38
39
    public function initialize()
40
    {
41
        $this->extensions->initialize();
42
    }
43
44
    public function extend($name, ExtensionInterface $extension)
45
    {
46
        $this->extensions->put($name, $extension);
47
    }
48
49
    public function setRepository(RepositoryInterface $repository)
50
    {
51
        $this->repository = $repository;
52
        $this->repository->with($this->getWith());
53
54
        return $this;
55
    }
56
57
    public function getRepository()
58
    {
59
        return $this->repository;
60
    }
61
62
    /**
63
     * @return string[]
64
     */
65
    public function getWith()
66
    {
67
        return $this->with;
68
    }
69
70
    /**
71
     * {@inheritdoc}
72
     */
73
    public function with($relations)
74
    {
75
        $this->with = array_flatten(func_get_args());
76
77
        return $this;
78
    }
79
80
    public function getQuery()
81
    {
82
        $repository = $this->getRepository();
83
84
        $builder = $repository->getQuery();
85
86
        if ($repository->isRestorable()) {
87
            $builder->withTrashed();
88
        }
89
90
        return $builder;
91
    }
92
93
    /**
94
     * Add an "order by" clause to the query.
95
     *
96
     * @param  string $column
97
     * @param  string $direction
98
     *
99
     * @return $this
100
     */
101
    public function orderBy($column, $direction = 'asc')
102
    {
103
        $this->scopes['orderBy'] = function (Builder $builder) use ($column, $direction) {
0 ignored issues
show
Bug introduced by
The property scopes does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
104
            $builder->orderBy($column, $direction);
0 ignored issues
show
Bug introduced by
The method orderBy() does not exist on Illuminate\Database\Eloquent\Builder. Did you maybe mean enforceOrderBy()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
105
        };
106
107
        return $this;
108
    }
109
110
    public function toArray()
111
    {
112
        return [
113
            'type' => $this->type,
114
        ];
115
    }
116
117
    public function __call($name, $parameters)
118
    {
119
        dump($name, $parameters);
120
        $method = substr($name, 3);
121
122
        dump($method, str_plural($method));
123
        if (starts_with($name, 'get') && $this->extensions->has(str_plural($method))) {
124
            return $this->extensions->get($method);
125
        }
126
127
        if (starts_with($name, 'set') && $this->extensions->has($method)) {
128
            $extension = $this->extensions->get($method);
0 ignored issues
show
Unused Code introduced by
$extension is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
129
130
        }
131
132
133
    }
134
}
135