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

Local::getFilePath()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
rs 10
cc 1
nc 1
nop 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
}