BaseRepository   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 95
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 18
dl 0
loc 95
ccs 0
cts 42
cp 0
rs 10
c 0
b 0
f 0
wmc 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A pluck() 0 3 1
A all() 0 3 1
1
<?php
2
3
namespace App\Repositories\Eloquent;
4
5
6
use App\Repositories\Contracts\RepositoryInterface;
7
use App\Repositories\Exceptions\RepositoryException;
8
use Illuminate\Container\Container as Application;
9
use Illuminate\Database\Eloquent\Model;
10
11
abstract class BaseRepository implements RepositoryInterface
12
{
13
    /**
14
     * @var Application
15
     */
16
    protected $app;
17
    /**
18
     * @var Model
19
     */
20
    protected $model;
21
22
    /**
23
     * @param Application $app
24
     */
25
    public function __construct(Application $app)
26
    {
27
        $this->app = $app;
28
        $this->makeModel();
29
30
    }
31
32
    /**
33
     * @return Model
34
     * @throws RepositoryException
35
     */
36
    public function makeModel()
37
    {
38
        $model = $this->app->make($this->model());
39
        if (!$model instanceof Model) {
40
            throw new RepositoryException("Class {$this->model()} must be an instance of Illuminate\\Database\\Eloquent\\Model");
41
        }
42
        return $this->model = $model;
43
    }
44
45
    /**
46
     * Specify Model class name
47
     *
48
     * @return string
49
     */
50
    abstract public function model();
51
52
    public function all($columns = array('*'))
53
    {
54
        return $this->model->get($columns);
55
    }
56
57
    public function create(array $columns)
58
    {
59
        return $this->model->create($columns);
60
    }
61
62
    public function findByField($field = null, $value = null, $columns = ['*'])
63
    {
64
        return $this->model->where($field, '=', $value)->get($columns);
65
66
    }
67
68
    public function find($value = null, $columns = ['*'])
69
    {
70
        return $this->model->where('id', '=', $value)->first($columns);
71
72
    }
73
74
    /**
75
     * Retrieve data array for populate field select
76
     *
77
     * @param string $column
78
     * @param string|null $key
79
     *
80
     * @return \Illuminate\Support\Collection|array
81
     */
82
    public function pluck($column, $key = null)
83
    {
84
        return $this->model->pluck($column, $key);
85
    }
86
87
88
    /**
89
     * Load relations
90
     *
91
     * @param array|string $relations
92
     *
93
     * @return $this
94
     */
95
    public function with($relations)
96
    {
97
        $this->model = $this->model->with($relations);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->model->with($relations) of type Illuminate\Database\Eloquent\Builder is incompatible with the declared type Illuminate\Database\Eloquent\Model of property $model.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
98
        return $this;
99
    }
100
101
102
    public function orderBy($column, $direction = 'asc')
103
    {
104
        $this->model = $this->model->orderBy($column, $direction);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->model->orderBy($column, $direction) can also be of type Illuminate\Database\Eloquent\Builder or Illuminate\Database\Query\Builder. However, the property $model is declared as type Illuminate\Database\Eloquent\Model. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
105
        return $this;
106
    }
107
108
}