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

View   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 9
lcom 2
cbo 1
dl 0
loc 89
rs 10
c 1
b 0
f 1

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A setRepository() 0 7 1
A getRepository() 0 4 1
A getWith() 0 4 1
A with() 0 6 1
A getQuery() 0 13 2
A orderBy() 0 8 1
A toArray() 0 6 1
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