Completed
Push — master ( bcfab5...a273fd )
by Oscar
02:55
created

File::upload()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 10
rs 9.4286
cc 3
eloc 5
nc 2
nop 1
1
<?php
2
3
namespace SimpleCrud\Fields;
4
5
use RuntimeException;
6
7
/**
8
 * To save files.
9
 */
10
class File extends Field
11
{
12
    protected $config = [
13
        'uploader' => null,
14
    ];
15
16
    /**
17
     * {@inheritdoc}
18
     */
19
    public function dataToDatabase($data)
20
    {
21
        if (!is_string($data)) {
22
            return $this->upload($data);
23
        }
24
25
        return $data;
26
    }
27
28
    /**
29
     * Upload the file and return the value.
30
     */
31
    private function upload($file)
32
    {
33
        $uploader = $this->config['uploader'];
34
35
        if (empty($uploader) || !is_callable($uploader)) {
36
            throw new RuntimeException('Not valid uploader');
37
        }
38
39
        return $uploader($file);
40
    }
41
}
42