Completed
Push — master ( 52556f...0998e9 )
by wen
14:52
created

View::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 1
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\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
    protected $type;
25
26
    public function __construct()
27
    {
28
29
    }
30
31
    public function setRepository(RepositoryInterface $repository)
32
    {
33
        $this->repository = $repository;
34
        $this->repository->with($this->getWith());
35
36
        return $this;
37
    }
38
39
    public function getRepository()
40
    {
41
        return $this->repository;
42
    }
43
44
    /**
45
     * @return string[]
46
     */
47
    public function getWith()
48
    {
49
        return $this->with;
50
    }
51
52
    /**
53
     * {@inheritdoc}
54
     */
55
    public function with($relations)
56
    {
57
        $this->with = array_flatten(func_get_args());
58
59
        return $this;
60
    }
61
62
    public function getQuery()
63
    {
64
        $repository = $this->getRepository();
65
        $repository->addGlobalScope($this->scopes);
66
67
        $builder = $repository->getQuery();
68
69
        if ($repository->isRestorable()) {
70
            $builder->withTrashed();
71
        }
72
73
        return $builder;
74
    }
75
76
    /**
77
     * Add an "order by" clause to the query.
78
     *
79
     * @param  string  $column
80
     * @param  string  $direction
81
     * @return $this
82
     */
83
    public function orderBy($column, $direction = 'asc')
84
    {
85
        $this->scopes['orderBy'] = function (Builder $builder) use ($column, $direction) {
86
            $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...
87
        };
88
89
        return $this;
90
    }
91
92
    public function toArray()
93
    {
94
        return [
95
            'type' => $this->type,
96
        ];
97
    }
98
}
99