Completed
Push — master ( b819b4...8115f1 )
by Sherif
14:17
created

BaseService::deleted()   A

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 5
1
<?php
2
3
namespace App\Modules\Core\BaseClasses;
4
5
use App\Modules\Core\Interfaces\BaseServiceInterface;
6
7
abstract class BaseService implements BaseServiceInterface
8
{
9
    /**
10
     * @var object
11
     */
12
    public $repo;
13
14
    /**
15
     * Init new object.
16
     *
17
     * @param   mixed  $repo
18
     * @return  void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
19
     */
20
    public function __construct($repo)
21
    {
22
        $this->repo = $repo;
23
    }
24
25
    /**
26
     * Fetch records with relations based on the given params.
27
     *
28
     * @param   array   $relations
29
     * @param   array   $conditions
30
     * @param   integer $perPage
31
     * @param   string  $sortBy
32
     * @param   boolean $desc
33
     * @param   boolean $trashed
34
     * @return collection
35
     */
36
    public function list($relations = [], $conditions = false, $perPage = 15, $sortBy = 'created_at', $desc = true, $trashed = false)
37
    {
38
        $filters = [];
39
        $translatable = $this->repo->model->translatable ?? [];
40
        foreach ($conditions as $key => $value) {
0 ignored issues
show
Bug introduced by
The expression $conditions of type false|array is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
41
            if($value && in_array($key, $this->repo->model->fillable ?? [])) {
42
                $key = in_array($key, $translatable) ? $key . '->' . (\Session::get('locale') == 'all' ? 'en' : \Session::get('locale')) : $key;
43
                $isBool = filter_var($value, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE) !== null;
44
                $filters[$key] = $isBool ? filter_var($value, FILTER_VALIDATE_BOOLEAN) : $value;
45
            }
46
        }
47
        $sortBy = in_array($sortBy, $translatable) ? $sortBy . '->' . \Session::get('locale') : $sortBy;
48
49
        if ($trashed) {
50
            return $this->deleted(['and' => $filters], $perPage ?? 15, $sortBy ?? 'created_at', $desc ?? true);
0 ignored issues
show
Documentation introduced by
$desc ?? true is of type boolean, but the function expects a integer.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
51
        }
52
        
53
        if (count($filters)) {
54
            return $this->paginateBy(['and' => $filters], $perPage ?? 15, $relations, $sortBy ?? 'created_at', $desc ?? true);
0 ignored issues
show
Documentation introduced by
$desc ?? true is of type boolean, but the function expects a integer.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
55
        }
56
57
        return $this->paginate($perPage ?? 15, $relations, $sortBy ?? 'created_at', $desc ?? true);
0 ignored issues
show
Documentation introduced by
$desc ?? true is of type boolean, but the function expects a integer.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
58
    }
59
60
    /**
61
     * Fetch all records with relations from the storage.
62
     *
63
     * @param  array   $relations
64
     * @param  string  $sortBy
65
     * @param  boolean $desc
66
     * @param  array   $columns
67
     * @return collection
68
     */
69
    public function all($relations = [], $sortBy = 'created_at', $desc = 1, $columns = ['*'])
70
    {
71
        return $this->repo->all($relations, $sortBy, $desc, $columns);
72
    }
73
    
74
    /**
75
     * Fetch all records with relations from storage in pages.
76
     *
77
     * @param  integer $perPage
78
     * @param  array   $relations
79
     * @param  string  $sortBy
80
     * @param  boolean $desc
81
     * @param  array   $columns
82
     * @return collection
83
     */
84
    public function paginate($perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = ['*'])
85
    {
86
        return $this->repo->paginate($perPage, $relations, $sortBy, $desc, $columns);
87
    }
88
89
    /**
90
     * Fetch all records with relations based on
91
     * the given condition from storage in pages.
92
     *
93
     * @param  array   $conditions array of conditions
94
     * @param  integer $perPage
95
     * @param  array   $relations
96
     * @param  string  $sortBy
97
     * @param  boolean $desc
98
     * @param  array   $columns
99
     * @return collection
100
     */
101
    public function paginateBy($conditions, $perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = ['*'])
102
    {
103
        return $this->repo->paginateBy($conditions, $perPage, $relations, $sortBy, $desc, $columns);
104
    }
105
    
106
    /**
107
     * Save the given model to the storage.
108
     *
109
     * @param  array $data
110
     * @return mixed
111
     */
112
    public function save(array $data)
113
    {
114
        return $this->repo->save($data);
115
    }
116
117
    /**
118
     * Delete record from the storage based on the given
119
     * condition.
120
     *
121
     * @param  var $value condition value
122
     * @param  string $attribute condition column name
123
     * @return void
124
     */
125
    public function delete($value, $attribute = 'id')
126
    {
127
        return $this->repo->delete($value, $attribute);
128
    }
129
    
130
    /**
131
     * Fetch records from the storage based on the given
132
     * id.
133
     *
134
     * @param  integer $id
135
     * @param  array   $relations
136
     * @param  array   $columns
137
     * @return object
138
     */
139
    public function find($id, $relations = [], $columns = ['*'])
140
    {
141
        return $this->repo->find($id, $relations, $columns);
142
    }
143
    
144
    /**
145
     * Fetch records from the storage based on the given
146
     * condition.
147
     *
148
     * @param  array   $conditions array of conditions
149
     * @param  array   $relations
150
     * @param  string  $sortBy
151
     * @param  boolean $desc
152
     * @param  array   $columns
153
     * @return collection
154
     */
155
    public function findBy($conditions, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = ['*'])
156
    {
157
        return $this->repo->findBy($conditions, $relations, $sortBy, $desc, $columns);
158
    }
159
160
    /**
161
     * Fetch the first record from the storage based on the given
162
     * condition.
163
     *
164
     * @param  array   $conditions array of conditions
165
     * @param  array   $relations
166
     * @param  array   $columns
167
     * @return object
168
     */
169
    public function first($conditions, $relations = [], $columns = ['*'])
170
    {
171
        return $this->repo->first($conditions, $relations, $columns);
172
    }
173
174
    /**
175
     * Return the deleted models in pages based on the given conditions.
176
     *
177
     * @param  array   $conditions array of conditions
178
     * @param  integer $perPage
179
     * @param  string  $sortBy
180
     * @param  boolean $desc
181
     * @param  array   $columns
182
     * @return collection
183
     */
184
    public function deleted($conditions, $perPage = 15, $sortBy = 'created_at', $desc = 1, $columns = ['*'])
185
    {
186
        return $this->repo->deleted($conditions, $perPage, $sortBy, $desc, $columns);
187
    }
188
189
    /**
190
     * Restore the deleted model.
191
     *
192
     * @param  integer $id
193
     * @return void
194
     */
195
    public function restore($id)
196
    {
197
        return $this->repo->restore($id);
198
    }
199
}
200