Completed
Push — master ( b5c7fa...23b509 )
by Taosikai
13:59
created

Local   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 33
c 0
b 0
f 0
wmc 6
lcom 1
cbo 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 2
A upload() 0 9 3
A getFilePath() 0 4 1
1
<?php
2
3
/*
4
 * This file is part of the slince/upload package.
5
 *
6
 * (c) Slince <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Slince\Upload\Filesystem;
13
14
use Symfony\Component\HttpFoundation\File\UploadedFile;
15
16
class Local implements FilesystemInterface
17
{
18
    /**
19
     * @var string
20
     */
21
    protected $savePath;
22
23
    public function __construct($savePath)
24
    {
25
        if (!is_dir($savePath)) {
26
            throw new \InvalidArgumentException(sprintf('The directory "%s" is invalid', $savePath));
27
        }
28
        $this->savePath = rtrim($savePath, '\\/');
29
    }
30
31
    /**
32
     * {@inheritdoc}
33
     */
34
    public function upload($key, UploadedFile $file, $overwrite = false)
35
    {
36
        $filePath = $this->getFilePath($key);
37
        if (file_exists($filePath) && !$overwrite) {
38
            throw new \RuntimeException(sprintf('The file with key "%s" is exists.', $key));
39
        }
40
41
        return $file->move(dirname($filePath), basename($filePath));
42
    }
43
44
    protected function getFilePath($key)
45
    {
46
        return "{$this->savePath}/{$key}";
47
    }
48
}