File::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 21
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 21
rs 9.3142
c 0
b 0
f 0
cc 1
eloc 19
nc 1
nop 9

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php namespace Nord\Lumen\FileManager\Doctrine\ODM;
2
3
use Carbon\Carbon;
4
use MongoDate;
5
use Nord\Lumen\FileManager\Contracts\File as FileContract;
6
use Nord\Lumen\FileManager\Facades\FileManager;
7
8
class File implements FileContract
9
{
10
11
    /**
12
     * @var \MongoId
13
     */
14
    private $id;
15
16
    /**
17
     * @var string
18
     */
19
    private $fileId;
20
21
    /**
22
     * @var string
23
     */
24
    private $name;
25
26
    /**
27
     * @var string
28
     */
29
    private $extension;
30
31
    /**
32
     * @var string
33
     */
34
    private $path;
35
36
    /**
37
     * @var string
38
     */
39
    private $mimeType;
40
41
    /**
42
     * @var int
43
     */
44
    private $byteSize;
45
46
    /**
47
     * @var array
48
     */
49
    private $data;
50
51
    /**
52
     * @var string
53
     */
54
    private $disk;
55
56
    /**
57
     * @var Carbon
58
     */
59
    private $savedAt;
60
61
62
    /**
63
     * File constructor.
64
     *
65
     * @param string $id
66
     * @param string $name
67
     * @param string $extension
68
     * @param string $path
69
     * @param string $mimeType
70
     * @param int    $byteSize
71
     * @param array  $data
72
     * @param string $disk
73
     * @param Carbon $savedAt
74
     */
75
    public function __construct(
76
        $id,
77
        $name,
78
        $extension,
79
        $path,
80
        $mimeType,
81
        $byteSize,
82
        array $data,
83
        $disk,
84
        Carbon $savedAt
85
    ) {
86
        $this->setFileId($id);
87
        $this->setName($name);
88
        $this->setExtension($extension);
89
        $this->setPath($path);
90
        $this->setMimeType($mimeType);
91
        $this->setByteSize($byteSize);
92
        $this->setData($data);
93
        $this->setDisk($disk);
94
        $this->setSavedAt($savedAt);
95
    }
96
97
98
    /**
99
     * @return \MongoId
100
     */
101
    public function getId()
102
    {
103
        return $this->id;
104
    }
105
106
    /**
107
     * @return string
108
     */
109
    public function getFileId()
110
    {
111
        return $this->fileId;
112
    }
113
114
115
    /**
116
     * @return string
117
     */
118
    public function getName()
119
    {
120
        return $this->name;
121
    }
122
123
    /**
124
     * @return string
125
     */
126
    public function getExtension()
127
    {
128
        return $this->extension;
129
    }
130
131
132
    /**
133
     * @return string
134
     */
135
    public function getMimeType()
136
    {
137
        return $this->mimeType;
138
    }
139
140
141
    /**
142
     * @return int
143
     */
144
    public function getByteSize()
145
    {
146
        return $this->byteSize;
147
    }
148
149
150
    /**
151
     * @return array
152
     */
153
    public function getData()
154
    {
155
        return $this->data;
156
    }
157
158
159
    /**
160
     * @return string
161
     */
162
    public function getDisk()
163
    {
164
        return $this->disk;
165
    }
166
167
168
    /**
169
     * @return Carbon
170
     */
171
    public function getSavedAt()
172
    {
173
        if ($this->savedAt instanceof MongoDate) {
174
            return Carbon::createFromTimestamp($this->savedAt->sec);
175
        }
176
        return $this->savedAt;
177
    }
178
179
180
    /**
181
     * @return string
182
     */
183
    public function getSavedAtAsTimestamp()
184
    {
185
        return $this->getSavedAt()->getTimestamp();
186
    }
187
188
189
    /**
190
     * @return string
191
     */
192
    public function getFilename()
193
    {
194
        return $this->name . '-' . $this->fileId . '.' . $this->extension;
195
    }
196
197
198
    /**
199
     * @inheritdoc
200
     */
201
    public function getFilePath()
202
    {
203
        return $this->getPath() . $this->getFilename();
204
    }
205
206
207
    /**
208
     * @inheritdoc
209
     */
210
    public function getUrl(array $options = [])
211
    {
212
        return FileManager::getFileUrl($this, $options);
213
    }
214
215
    /**
216
     * @param string $fileId
217
     */
218
    private function setFileId($fileId)
219
    {
220
        $this->fileId = $fileId;
221
    }
222
223
224
    /**
225
     * @param string $name
226
     *
227
     * @throws \Exception
228
     */
229
    private function setName($name)
230
    {
231
        if (empty($name)) {
232
            throw new \Exception('File name cannot be empty.');
233
        }
234
235
        $this->name = $name;
236
    }
237
238
239
    /**
240
     * @param string $extension
241
     *
242
     * @throws \Exception
243
     */
244
    private function setExtension($extension)
245
    {
246
        if (empty($extension)) {
247
            throw new \Exception('File extension cannot be empty.');
248
        }
249
250
        $this->extension = $extension;
251
    }
252
253
254
    /**
255
     * @param string $path
256
     */
257
    private function setPath($path)
258
    {
259
        $this->path = $path;
260
    }
261
262
263
    /**
264
     * @param string $mimeType
265
     */
266
    private function setMimeType($mimeType)
267
    {
268
        $this->mimeType = $mimeType;
269
    }
270
271
272
    /**
273
     * @param int $byteSize
274
     */
275
    private function setByteSize($byteSize)
276
    {
277
        $this->byteSize = $byteSize;
278
    }
279
280
281
    /**
282
     * @param array $data
283
     */
284
    private function setData(array $data)
285
    {
286
        $this->data = $data;
287
    }
288
289
290
    /**
291
     * @param string $storage
292
     */
293
    private function setDisk($storage)
294
    {
295
        $this->disk = $storage;
296
    }
297
298
299
    /**
300
     * @param Carbon $savedAt
301
     */
302
    private function setSavedAt(Carbon $savedAt)
303
    {
304
        $this->savedAt = new MongoDate($savedAt->getTimeStamp());
0 ignored issues
show
Documentation Bug introduced by
It seems like new \MongoDate($savedAt->getTimeStamp()) of type object<MongoDate> is incompatible with the declared type object<Carbon\Carbon> of property $savedAt.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
305
    }
306
307
308
    /**
309
     * @return string
310
     */
311
    private function getPath()
312
    {
313
        return isset($this->path) ? $this->path . '/' : '';
314
    }
315
}
316