Completed
Push — master ( be363a...be363a )
by
unknown
12:37
created

View::getQuery()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
dl 0
loc 13
rs 9.4285
c 1
b 0
f 1
cc 2
eloc 7
nc 2
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\ViewInterface;
9
10
abstract class View implements ViewInterface, Arrayable
11
{
12
    /**
13
     * @var array
14
     */
15
    protected $with = [];
16
17
    /**
18
     * @var RepositoryInterface
19
     */
20
    protected $repository;
21
22
    protected $scopes = [];
23
24
    public function setRepository(RepositoryInterface $repository)
25
    {
26
        $this->repository = $repository;
27
        $this->repository->with($this->getWith());
28
        return $this;
29
    }
30
31
    public function getRepository()
32
    {
33
        return $this->repository;
34
    }
35
36
    /**
37
     * @return string[]
38
     */
39
    public function getWith()
40
    {
41
        return $this->with;
42
    }
43
44
    /**
45
     * {@inheritdoc}
46
     */
47
    public function with($relations)
48
    {
49
        $this->with = array_flatten(func_get_args());
50
51
        return $this;
52
    }
53
54
    public function getQuery()
55
    {
56
        $repository = $this->getRepository();
57
        $repository->addGlobalScope($this->scopes);
58
59
        $builder = $repository->getQuery();
60
61
        if ($repository->isRestorable()) {
62
            $builder->withTrashed();
63
        }
64
65
        return $builder;
66
    }
67
68
    /**
69
     * Add an "order by" clause to the query.
70
     *
71
     * @param  string  $column
72
     * @param  string  $direction
73
     * @return $this
74
     */
75
    public function orderBy($column, $direction = 'asc')
76
    {
77
        $this->scopes['orderBy'] = function (Builder $builder) use ($column, $direction) {
78
            $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...
79
        };
80
    }
81
82
    public function toArray()
83
    {
84
        return [];
85
    }
86
}
87