Completed
Push — master ( 39b104...a0772e )
by Sherif
01:57
created

BaseService   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 182
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 1
dl 0
loc 182
rs 10
c 0
b 0
f 0

12 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A list() 0 13 2
A all() 0 4 1
A paginate() 0 4 1
A paginateBy() 0 4 1
A save() 0 4 1
A delete() 0 4 1
A find() 0 4 1
A findBy() 0 4 1
A first() 0 4 1
A deleted() 0 4 1
A restore() 0 4 1
1
<?php
2
3
namespace App\Modules\Core\BaseClasses;
4
5
use App\Modules\Core\Interfaces\BaseServiceInterface;
6
use App\Modules\Core\Decorators\CachingDecorator;
7
8
abstract class BaseService implements BaseServiceInterface
9
{
10
    /**
11
     * @var object
12
     */
13
    protected $repo;
14
15
    /**
16
     * Init new object.
17
     *
18
     * @param   mixed  $repo
19
     * @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...
20
     */
21
    public function __construct($repo)
22
    {
23
        $this->repo = new CachingDecorator($repo, \App::make('Illuminate\Contracts\Cache\Repository'));
24
    }
25
26
    /**
27
     * Fetch records with relations based on the given params.
28
     *
29
     * @param   string  $relations
30
     * @param   array   $conditions
31
     * @param   integer $perPage
32
     * @param   string  $sortBy
33
     * @param   boolean $desc
34
     * @return collection
35
     */
36
    public function list($relations = [], $conditions = false, $perPage = 15, $sortBy = 'created_at', $desc = true)
37
    {
38
        unset($conditions['perPage']);
39
        unset($conditions['sortBy']);
40
        unset($conditions['sort']);
41
        unset($conditions['page']);
42
43
        if (count($conditions)) {
44
            return $this->repo->paginateBy(['and' => $conditions], $perPage ?? 15, $relations, $sortBy ?? 'created_at', $desc ?? true);
45
        }
46
47
        return $this->repo->paginate($perPage ?? 15, $relations, $sortBy ?? 'created_at', $desc ?? true);
48
    }
49
50
    /**
51
     * Fetch all records with relations from the storage.
52
     *
53
     * @param  array   $relations
54
     * @param  string  $sortBy
55
     * @param  boolean $desc
56
     * @param  array   $columns
57
     * @return collection
58
     */
59
    public function all($relations = [], $sortBy = 'created_at', $desc = 1, $columns = ['*'])
60
    {
61
        return $this->repo->all($relations, $sortBy, $desc, $columns);
62
    }
63
    
64
    /**
65
     * Fetch all records with relations from storage in pages.
66
     *
67
     * @param  integer $perPage
68
     * @param  array   $relations
69
     * @param  string  $sortBy
70
     * @param  boolean $desc
71
     * @param  array   $columns
72
     * @return collection
73
     */
74
    public function paginate($perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = ['*'])
75
    {
76
        return $this->repo->paginate($perPage, $relations, $sortBy, $desc, $columns);
77
    }
78
79
    /**
80
     * Fetch all records with relations based on
81
     * the given condition from storage in pages.
82
     *
83
     * @param  array   $conditions array of conditions
84
     * @param  integer $perPage
85
     * @param  array   $relations
86
     * @param  string  $sortBy
87
     * @param  boolean $desc
88
     * @param  array   $columns
89
     * @return collection
90
     */
91
    public function paginateBy($conditions, $perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = ['*'])
92
    {
93
        return $this->repo->paginateBy($conditions, $perPage, $relations, $sortBy, $desc, $columns);
94
    }
95
    
96
    /**
97
     * Save the given model to the storage.
98
     *
99
     * @param  array $data
100
     * @return mixed
101
     */
102
    public function save(array $data)
103
    {
104
        return $this->repo->save($data);
105
    }
106
107
    /**
108
     * Delete record from the storage based on the given
109
     * condition.
110
     *
111
     * @param  var $value condition value
112
     * @param  string $attribute condition column name
113
     * @return void
114
     */
115
    public function delete($value, $attribute = 'id')
116
    {
117
        return $this->repo->save($value, $attribute);
118
    }
119
    
120
    /**
121
     * Fetch records from the storage based on the given
122
     * id.
123
     *
124
     * @param  integer $id
125
     * @param  string[]   $relations
126
     * @param  array   $columns
127
     * @return object
128
     */
129
    public function find($id, $relations = [], $columns = ['*'])
130
    {
131
        return $this->repo->find($id, $relations, $columns);
132
    }
133
    
134
    /**
135
     * Fetch records from the storage based on the given
136
     * condition.
137
     *
138
     * @param  array   $conditions array of conditions
139
     * @param  array   $relations
140
     * @param  string  $sortBy
141
     * @param  boolean $desc
142
     * @param  array   $columns
143
     * @return collection
144
     */
145
    public function findBy($conditions, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = ['*'])
146
    {
147
        return $this->repo->findBy($conditions, $relations, $sortBy, $desc, $columns);
148
    }
149
150
    /**
151
     * Fetch the first record from the storage based on the given
152
     * condition.
153
     *
154
     * @param  array   $conditions array of conditions
155
     * @param  array   $relations
156
     * @param  array   $columns
157
     * @return object
158
     */
159
    public function first($conditions, $relations = [], $columns = ['*'])
160
    {
161
        return $this->repo->first($conditions, $relations, $columns);
162
    }
163
164
    /**
165
     * Return the deleted models in pages based on the given conditions.
166
     *
167
     * @param  array   $conditions array of conditions
168
     * @param  integer $perPage
169
     * @param  string  $sortBy
170
     * @param  boolean $desc
171
     * @param  array   $columns
172
     * @return collection
173
     */
174
    public function deleted($conditions, $perPage = 15, $sortBy = 'created_at', $desc = 1, $columns = ['*'])
175
    {
176
        return $this->repo->deleted($conditions, $perPage, $sortBy, $desc, $columns);
177
    }
178
179
    /**
180
     * Restore the deleted model.
181
     *
182
     * @param  integer $id
183
     * @return void
184
     */
185
    public function restore($id)
186
    {
187
        return $this->repo->restore($id);
188
    }
189
}
190