Repository::getId()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace PragmaRX\Tracker\Data\Repositories;
4
5
abstract class Repository implements RepositoryInterface
6
{
7
    protected $builder;
8
9
    protected $model;
10
11
    protected $result;
12
13
    protected $connection;
14
15
    protected $className;
16
17
    protected $relations;
18
19
    /**
20
     * @var \PragmaRX\Tracker\Support\Cache
21
     */
22
    protected $cache;
23
24
    public function __construct($model)
25
    {
26
        $this->model = $model;
27
28
        $this->className = get_class($model);
29
30
        $this->connection = $this->getModel()->getConnectionName();
31
32
        $this->cache = app('tracker.cache');
33
    }
34
35
    public function where($key, $operation, $value = null)
0 ignored issues
show
Unused Code introduced by
The parameter $value is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
36
    {
37
        $this->builder = $this->builder ?: $this->newQuery();
38
39
        $this->builder = $this->builder->where($key, $operation, $value = null);
40
41
        return $this;
42
    }
43
44
    public function first()
45
    {
46
        $this->result = $this->builder->first();
47
48
        return $this->result ? $this : null;
49
    }
50
51
    public function find($id)
52
    {
53
        list($model, $cacheKey) = $this->cache->findCached($id, null, $this->className);
54
55
        if (!$model) {
56
            $model = $this->newQuery();
57
58
            if ($this->relations) {
59
                $model->with($this->relations);
60
            }
61
62
            if ($model = $model->find($id)) {
63
                $this->cache->cachePut($cacheKey, $model);
64
            }
65
        }
66
67
        $this->model = $model;
68
        $this->result = $model;
69
70
        return $model;
71
    }
72
73
    public function create($attributes, $model = null)
74
    {
75
        $model = $model && !$model->exists() ? $model : $this->newModel($model);
76
77
        foreach ($attributes as $attribute => $value) {
78
            if (in_array($attribute, $model->getFillable())) {
79
                $model->{$attribute} = $value;
80
            }
81
        }
82
83
        $model->save();
84
85
        return $model;
86
    }
87
88
    public function getId()
89
    {
90
        return $this->getAttribute('id');
91
    }
92
93
    /**
94
     * @param string $attribute
95
     */
96
    public function getAttribute($attribute)
97
    {
98
        return $this->result ? $this->result->{$attribute} : null;
99
    }
100
101
    public function setAttribute($attribute, $value)
102
    {
103
        return $this->result->{$attribute} = $value;
104
    }
105
106
    public function save()
107
    {
108
        return $this->result->save();
109
    }
110
111
    /**
112
     * @param string[] $keys
113
     */
114
    public function findOrCreate($attributes, $keys = null, &$created = false, $otherModel = null)
115
    {
116
        list($model, $cacheKey) = $this->cache->findCached($attributes, $keys, $this->className);
117
118
        if (!$model) {
119
            $model = $this->newQuery($otherModel);
120
121
            $keys = $keys ?: array_keys($attributes);
122
123
            foreach ($keys as $key) {
124
                $model = $model->where($key, $attributes[$key]);
125
            }
126
127
            if (!$model = $model->first()) {
128
                $model = $this->create($attributes, $otherModel);
129
130
                $created = true;
131
            }
132
133
            $this->cache->cachePut($cacheKey, $model);
134
        }
135
136
        $this->model = $model;
137
138
        return $model->id;
139
    }
140
141
    public function getModel()
142
    {
143
        if ($this->model instanceof Illuminate\Database\Eloquent\Builder) {
0 ignored issues
show
Bug introduced by
The class PragmaRX\Tracker\Data\Re...tabase\Eloquent\Builder does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
144
            $this->model = new $this->className();
145
        }
146
147
        if ($this->connection) {
148
            $this->model->setConnection($this->connection);
149
        }
150
151
        return $this->model;
152
    }
153
154
    public function newModel($model = null)
155
    {
156
        $className = $this->className;
157
158
        if ($model) {
159
            $className = get_class($model);
160
        }
161
162
        $this->model = new $className();
163
164
        return $this->getModel();
165
    }
166
167
    public function newQuery($model = null)
168
    {
169
        $className = $this->className;
170
171
        if ($model) {
172
            $className = get_class($model);
173
        }
174
175
        $this->builder = new $className();
176
177
        if ($this->connection) {
178
            $this->builder = $this->builder->on($this->connection);
179
        }
180
181
        return $this->builder->newQuery();
182
    }
183
}
184