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

File   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 5
c 2
b 0
f 1
lcom 1
cbo 1
dl 0
loc 32
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A dataToDatabase() 0 8 2
A upload() 0 10 3
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