Completed
Branch develop-3.0 (4fe777)
by Mohamed
11:06
created

Repository::setModel()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
/*
3
 * This file is part of the site package.
4
 *
5
 * (c) Mohamed Alsharaf <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Tinyissue\Repository;
12
13
use Illuminate\Database\Eloquent\Model;
14
use Tinyissue\Contracts\Repository\RepositoryUpdater;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Tinyissue\Repository\RepositoryUpdater.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
15
use Tinyissue\Extensions\Auth\LoggedUser;
16
17
abstract class Repository
18
{
19
    use LoggedUser;
20
21
    /**
22
     * @var Model
23
     */
24
    protected $model;
25
26
    public function __construct(Model $model)
27
    {
28
        $this->model = $model;
29
    }
30
31
    /**
32
     * @return Model
33
     */
34
    public function getModel()
35
    {
36
        return $this->model;
37
    }
38
39
    // Declaration of Tinyissue\Repository\Repository::setModel() must be compatible with Tinyissue\Contracts\Repository\RepositoryInterface::setModel(Tinyissue\Contracts\Repository\Model $model)
40
    /**
41
     * @param Model $model
42
     *
43
     * @return $this
44
     */
45
    public function setModel(Model $model)
46
    {
47
        $this->model = $model;
48
49
        return $this;
50
    }
51
52
    protected $updater;
53
    protected $counter;
54
    protected $updaterClass;
55
    protected $counterClass;
56
57
    protected function getRelatedRepository($property, $className)
58
    {
59
        if (is_null($this->$property)) {
60
            $this->$property = new $this->{$className}($this->getModel());
61
        }
62
63
        return $this->$property;
64
    }
65
66
    /**
67
     * @return RepositoryUpdater
68
     */
69
    public function updater()
70
    {
71
        return $this->getRelatedRepository('updater', 'updaterClass');
72
    }
73
74
    /**
75
     * @return RepositoryUpdater
76
     */
77
    public function counter()
78
    {
79
        return $this->getRelatedRepository('counter', 'counterClass');
80
    }
81
82
    public function load($id)
83
    {
84
        return $this->setModel($this->getById($id));
85
    }
86
87
    public function hasModel()
88
    {
89
        return $this->getModel() && $this->getModel()->id > 0;
90
    }
91
92
    public function getById($id)
93
    {
94
        return $this->model->find($id);
95
    }
96
97
    public function requireById($id)
98
    {
99
        $model = $this->getById($id);
100
101
        if (!$model) {
102
            throw new \DomainException('Un');
103
        }
104
105
        return $model;
106
    }
107
108
//    public function getAll()
109
//    {
110
//        return $this->model->all();
111
//    }
112
//
113
//    public function getPaginated($count)
114
//    {
115
//        return $this->model->paginate($count);
116
//    }
117
118
    public function newModel()
119
    {
120
        return $this->getModel()->newInstance();
121
    }
122
123
    /**
124
     * @param array $columns
0 ignored issues
show
Bug introduced by
There is no parameter named $columns. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
125
     *
126
     * @return mixed
127
     */
128
    public function all()
129
    {
130
        return $this->getModel()->orderBy('id')->all();
131
        // TODO: Implement all() method.
132
    }
133
134
    /**
135
     * @param int   $perPage
136
     * @param array $columns
137
     *
138
     * @return mixed
139
     */
140
    public function paginate($perPage = 15, $columns = ['*'])
0 ignored issues
show
Unused Code introduced by
The parameter $perPage 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...
Unused Code introduced by
The parameter $columns 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...
141
    {
142
        // TODO: Implement paginate() method.
143
    }
144
145
    /**
146
     * @param $id
147
     * @param array $columns
148
     *
149
     * @return mixed
150
     */
151
    public function find($id, $columns = ['*'])
0 ignored issues
show
Unused Code introduced by
The parameter $id 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...
Unused Code introduced by
The parameter $columns 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...
152
    {
153
        // TODO: Implement find() method.
154
    }
155
156
    public function __get($name)
157
    {
158
        return $this->getModel()->{$name};
159
    }
160
161
    public function __call($name, $arguments)
162
    {
163
        $caller = false;
164
165
        if (strpos($name, 'count') === 0) {
166
            $caller = $this->counter();
167
        }
168
169
        if (!is_null($this->upater) && method_exists($this->upater, $name)) {
0 ignored issues
show
Bug introduced by
The property upater does not seem to exist. Did you mean updater?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
170
            $caller = $this->updater();
171
        }
172
173
        if (!is_null($this->model) && method_exists($this->model, $name)) {
174
            $caller = $this->getModel();
175
        }
176
177
        if (false !== $caller) {
178
            return $caller->{$name}(...$arguments);
179
        }
180
181
        throw new \DomainException(sprintf('Call to undefined method "%s".', $name));
182
    }
183
}
184