FileManager::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
rs 9.4285
cc 1
eloc 3
nc 1
nop 2
1
<?php
2
namespace Balloon\Manager;
3
4
use Balloon\Reader\IFileReader;
5
6
/**
7
 * Class FileManager
8
 * @package Balloon\Manager
9
 * @author Raphaël Lefebvre <[email protected]>
10
 */
11
class FileManager implements IFileManager
12
{
13
    /**
14
     * @var IFileReader
15
     */
16
    private $fileReader;
17
18
    /**
19
     * @var string
20
     */
21
    private $primaryKey;
22
23
    /**
24
     * @param IFileReader $fileReader
25
     * @param string $primaryKey
26
     */
27
    public function __construct(IFileReader $fileReader, $primaryKey = '')
28
    {
29
        $this->setFileReader($fileReader);
30
        $this->setPrimaryKey($primaryKey);
31
    }
32
33
    /**
34
     * @param mixed $id
35
     * @return bool
36
     */
37
    public function has($id)
38
    {
39
        return array_key_exists($id, $this->getAll());
40
    }
41
42
    /**
43
     * @param mixed $id
44
     * @return mixed|null
45
     */
46
    public function get($id)
47
    {
48
        if($this->has($id)){
49
            return $this->getAll()[$id];
50
        }
51
        return null;
52
    }
53
54
    /**
55
     * @param array $ids
56
     * @return mixed
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use object|array.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
57
     */
58 View Code Duplication
    public function getList(array $ids)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
59
    {
60
        $all = $this->getAll();
61
        $result = array_intersect_key((array)$all, $ids);
62
        if(is_object($all)){
63
            return new $all($result);
64
        }
65
        return $result;
66
    }
67
68
    /**
69
     * @param callable $filter
0 ignored issues
show
Documentation introduced by
Should the type for parameter $filter not be null|callable? Also, consider making the array more specific, something like array<String>, or String[].

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive. In addition it looks for parameters that have the generic type array and suggests a stricter type like array<String>.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
70
     * @return mixed
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use object|array.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
71
     */
72 View Code Duplication
    public function find(callable $filter = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
73
    {
74
        $all = $this->getAll();
75
        $result = array_filter((array)$all, $filter);
76
        if(is_object($all)){
77
            return new $all($result);
78
        }
79
        return $result;
80
    }
81
82
    /**
83
     * @return mixed
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use array|object.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
84
     */
85
    public function getAll()
86
    {
87
        $result = $this->fileReader->read();
88
        if(is_null($result)){
89
            return [];
90
        }
91
        if(is_scalar($result)){
92
            return (array)$result;
93
        }
94
        return $result;
95
    }
96
97
    /**
98
     * @param mixed $dataList
99
     * @return int
100
     */
101
    public function set($dataList)
102
    {
103
        return $this->fileReader->write($dataList);
104
    }
105
106
    /**
107
     * @param mixed $data
108
     * @return int
109
     */
110
    public function add($data)
111
    {
112
        $dataList = $this->getAll();
113
        $key = $this->retrieveKey($data);
114
        if($key){
115
            $dataList[$key] = $data;
116
        }else{
117
            $dataList[] = $data;
118
        }
119
        return $this->set($dataList);
120
    }
121
122
    /**
123
     * @param mixed $data
124
     * @return mixed
125
     */
126
    private function retrieveKey($data)
127
    {
128
        if($this->getPrimaryKey()){
129
            $pk = $this->getPrimaryKey();
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $pk. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
130
            if(is_array($data)){
131
                if(array_key_exists($pk, $data)) {
132
                    return $data[$pk];
133
                }
134
            }elseif(is_object($data)){
135
                if(method_exists($data, 'get'.$pk)){
136
                    return $data->{'get'.$pk}();
137
                }
138
                if(property_exists($data, $pk)) {
139
                    return $data->{$pk};
140
                }
141
            }
142
        }
143
        return null;
144
    }
145
146
    /**
147
     * @param mixed $dataList
148
     * @return int
149
     */
150
    public function addList($dataList)
151
    {
152
        $result = 0;
153
        foreach($dataList as $data){
154
            $result = $this->add($data);
155
        }
156
        return $result;
157
    }
158
159
    /**
160
     * @param mixed $id
161
     * @param mixed $data
162
     * @return int
163
     */
164
    public function modify($id, $data)
165
    {
166
        $dataList = $this->getAll();
167
        if(isset($dataList[$id])){
168
            $dataList[$id] = $data;
169
            return $this->set($dataList);
170
        }
171
        return 0;
172
    }
173
174
    /**
175
     * @param mixed $dataList
176
     * @return int
177
     */
178
    public function modifyList($dataList)
179
    {
180
        $result = 0;
181
        foreach($dataList as $id => $data){
182
            $result = $this->modify($id, $data);
183
        }
184
        return $result;
185
    }
186
187
    /**
188
     * @param mixed $id
189
     * @return int
190
     */
191
    public function remove($id)
192
    {
193
        $dataList = $this->getAll();
194
        unset($dataList[$id]);
195
        return $this->set($dataList);
196
    }
197
198
    /**
199
     * @param array $ids
200
     * @return int
201
     */
202
    public function removeList(array $ids)
203
    {
204
        $result = 0;
205
        foreach($ids as $id){
206
            $result = $this->remove($id);
207
        }
208
        return $result;
209
    }
210
211
    /**
212
     * @return int
213
     */
214
    public function removeAll()
215
    {
216
        return $this->set(null);
217
    }
218
219
    /**
220
     * Getter of $fileReader
221
     *
222
     * @return IFileReader
223
     */
224
    protected function getFileReader()
225
    {
226
        return $this->fileReader;
227
    }
228
229
    /**
230
     * Getter of $primaryKey
231
     *
232
     * @return string
233
     */
234
    protected function getPrimaryKey()
235
    {
236
        return $this->primaryKey;
237
    }
238
239
    /**
240
     * Setter of $primaryKey
241
     *
242
     * @param string $primaryKey
243
     */
244
    private function setPrimaryKey($primaryKey)
245
    {
246
        $this->primaryKey = (string)$primaryKey;
247
    }
248
249
    /**
250
     * Setter of $fileReader
251
     *
252
     * @param IFileReader $fileReader
253
     */
254
    private function setFileReader(IFileReader $fileReader)
255
    {
256
        $this->fileReader = $fileReader;
257
    }
258
}
259