Completed
Push — master ( 1a94da...33e4a6 )
by Adrian
02:31
created

Local::has()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 3
Bugs 0 Features 1
Metric Value
c 3
b 0
f 1
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 2
eloc 2
nc 2
nop 1
crap 2
1
<?php
2
3
namespace Sirius\Upload\Container;
4
5
class Local implements ContainerInterface
6
{
7
8
    protected $baseDirectory;
9
10 20
    public function __construct($baseDirectory)
11
    {
12 20
        $this->baseDirectory = $this->normalizePath($baseDirectory) . DIRECTORY_SEPARATOR;
13 20
        $this->ensureDirectory($this->baseDirectory);
14 20
    }
15
16 20
    protected function normalizePath($path)
17
    {
18 20
        $path = dirname(rtrim($path, '\\/') . DIRECTORY_SEPARATOR . 'xxx');
19
20 20
        return rtrim($path, DIRECTORY_SEPARATOR);
21
    }
22
23 20
    protected function ensureDirectory($directory)
24
    {
25 20
        if (!is_dir($directory)) {
26 7
            mkdir($directory, 0766, true);
27 7
        }
28
29 20
        return is_dir($directory) && $this->isWritable();
30
    }
31
32
    /**
33
     * Check if the container is writable
34
     */
35 20
    public function isWritable()
36
    {
37 20
        return is_writable($this->baseDirectory);
38
    }
39
40
    /**
41
     * This will check if a file is in the container
42
     *
43
     * @param  string $file
44
     * @return bool
45
     */
46 10
    public function has($file)
47
    {
48 10
        return $file && file_exists($this->baseDirectory . $file);
49
    }
50
51
    /**
52
     * Saves the $content string as a file
53
     *
54
     * @param  string $file
55
     * @param  string $content
56
     * @return bool
57
     */
58 11
    public function save($file, $content)
59
    {
60 11
        $file = $this->normalizePath($file);
61 11
        $dir = dirname($this->baseDirectory . $file);
62 11
        if ($this->ensureDirectory($dir)) {
63 11
            return (bool) file_put_contents($this->baseDirectory . $file, $content);
64
        }
65
66
        return false;
67
    }
68
69
    /**
70
     * Delete the file from the container
71
     *
72
     * @param  string $file
73
     * @return bool
74
     */
75 6
    public function delete($file)
76
    {
77 6
        $file = $this->normalizePath($file);
78 6
        if (file_exists($this->baseDirectory . $file)) {
79 5
            return unlink($this->baseDirectory . $file);
80
        }
81
82 2
        return true;
83
    }
84
85
    /**
86
     * Moves a temporary uploaded file to a destination in the container
87
     *
88
     * @param  string $localFile   local path
89
     * @param  string $destination
90
     * @return bool
91
     */
92 11
    public function moveUploadedFile($localFile, $destination)
93
    {
94 11
        $dir = dirname($this->baseDirectory . $destination);
95 11
        if (file_exists($localFile) && $this->ensureDirectory($dir)) {
96
            /**
97
             * we could use is_uploaded_file() and move_uploaded_file()
98
             * but in case of ajax uploads that would fail
99
             */
100 10
            if (is_readable($localFile)) {
101
                // rename() would be good but this is better because $localFile may become 'unwritable'
102 10
                $result = copy($localFile, $this->baseDirectory . $destination);
103 10
                @unlink($localFile);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
104 10
                return $result;
105
            }
106
        }
107 1
        return false;
108
    }
109
110
}
111