AbstractEloquentRepository::destroy()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
c 0
b 0
f 0
rs 9.4285
cc 2
eloc 4
nc 2
nop 1
1
<?php
2
3
/**
4
 * Storgman - Student Organizations Management
5
 * Copyright (C) 2014-2015, Dejan Angelov <[email protected]>
6
 *
7
 * This file is part of Storgman.
8
 *
9
 * Storgman is free software: you can redistribute it and/or modify
10
 * it under the terms of the GNU General Public License as published by
11
 * the Free Software Foundation, either version 3 of the License, or
12
 * (at your option) any later version.
13
 *
14
 * Storgman is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU General Public License
20
 * along with Storgman.  If not, see <http://www.gnu.org/licenses/>.
21
 *
22
 * @package Storgman
23
 * @copyright Copyright (C) 2014-2015, Dejan Angelov <[email protected]>
24
 * @license https://github.com/angelov/storgman/blob/master/LICENSE
25
 * @author Dejan Angelov <[email protected]>
26
 */
27
28
namespace Angelov\Storgman\Core\Repositories;
29
30
use Angelov\Storgman\Core\Exceptions\ResourceNotFoundException;
31
use Illuminate\Database\Eloquent\Model;
32
33
abstract class AbstractEloquentRepository implements RepositoryInterface
34
{
35
    protected $entity;
36
37
    public function __construct(Model $entity)
38
    {
39
        $this->entity = $entity;
40
    }
41
42
    public function getEntity()
43
    {
44
        return $this->entity;
45
    }
46
47
    public function setEntity(Model $entity)
48
    {
49
        $this->entity = $entity;
50
    }
51
52
    public function all(array $withRelationships = [])
53
    {
54
        return $this->entity->with($withRelationships)->get()->all();
0 ignored issues
show
Bug introduced by
The method get does only exist in Illuminate\Database\Eloquent\Builder, but not in Illuminate\Database\Eloquent\Model.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
55
    }
56
57
    public function get($id)
58
    {
59
        $resource = $this->entity->find($id);
60
61
        if ($resource == null) {
62
            throw new ResourceNotFoundException();
63
        }
64
65
        return $resource;
66
    }
67
68
    public function destroy($id)
69
    {
70
        $resource = $this->get($id);
71
72
        if ($resource instanceof Model) {
73
            $resource->delete();
74
        }
75
    }
76
77
    public function getByPage($page = 1, $limit = 20, array $withRelationships = [])
78
    {
79
        $results = new \stdClass();
80
        $results->page = $page;
81
        $results->limit = $limit;
82
        $results->totalItems = 0;
83
        $results->items = array();
84
85
        // This will show a warning in the IDEs, but that's
86
        // because the QueryBuilder uses __call()
87
        $fetched = $this->entity->with($withRelationships)
88
            ->orderBy('id', 'desc')
89
            ->skip($limit * ($page - 1))
90
            ->take($limit)
91
            ->get()->all();
92
93
        $results->totalItems = $this->countAll();
94
        $results->items = $fetched;
95
96
        return $results;
97
    }
98
99
    public function latest($count, array $withRelationships = [], $orderByField = 'date')
100
    {
101
        $fetched = $this->entity->with($withRelationships)
102
            ->orderBy($orderByField, 'desc')
103
            ->take($count)
104
            ->get()->all();
105
106
        return $fetched;
107
    }
108
109
    public function getByIds(array $ids = [])
110
    {
111
        $query = $this->entity->newQuery();
112
        $results = $query->findMany($ids)->all();
113
114
        return $results;
115
    }
116
117
    public function countAll()
118
    {
119
        $eloquentQuery = $this->entity->newQuery();
120
        $queryBuilder = $eloquentQuery->getQuery();
121
122
        return $queryBuilder->count();
0 ignored issues
show
Bug introduced by
The method count does only exist in Illuminate\Database\Query\Builder, but not in Illuminate\Database\Eloquent\Builder.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
123
    }
124
}
125