FileSystem::upload()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 26
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 18
nc 3
nop 1
dl 0
loc 26
rs 9.6666
c 1
b 0
f 0
1
<?php
2
3
/**
4
 * Platine Upload
5
 *
6
 * Platine Upload provides a flexible file uploads with extensible
7
 * validation and storage strategies.
8
 *
9
 * This content is released under the MIT License (MIT)
10
 *
11
 * Copyright (c) 2020 Platine Upload
12
 *
13
 * @author      Josh Lockhart <[email protected]>
14
 * @copyright   2012 Josh Lockhart
15
 * @link        http://www.joshlockhart.com
16
 * @version     2.0.0
17
 *
18
 * Permission is hereby granted, free of charge, to any person obtaining a copy
19
 * of this software and associated documentation files (the "Software"), to deal
20
 * in the Software without restriction, including without limitation the rights
21
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
22
 * copies of the Software, and to permit persons to whom the Software is
23
 * furnished to do so, subject to the following conditions:
24
 *
25
 * The above copyright notice and this permission notice shall be included in all
26
 * copies or substantial portions of the Software.
27
 *
28
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
29
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
30
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
31
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
32
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
33
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
34
 * SOFTWARE.
35
 */
36
37
/**
38
 *  @file FileSystem.php
39
 *
40
 *  The Upload File system storage class
41
 *
42
 *  @package    Platine\Upload\Storage
43
 *  @author Platine Developers Team
44
 *  @copyright  Copyright (c) 2020
45
 *  @license    http://opensource.org/licenses/MIT  MIT License
46
 *  @link   https://www.platine-php.com
47
 *  @version 1.0.0
48
 *  @filesource
49
 */
50
51
declare(strict_types=1);
52
53
namespace Platine\Upload\Storage;
54
55
use InvalidArgumentException;
56
use Platine\Upload\Exception\StorageException;
57
use Platine\Upload\Exception\UploadException;
58
use Platine\Upload\File\File;
59
use Platine\Upload\File\UploadFileInfo;
60
61
/**
62
 * @class FileSystem
63
 * @package Platine\Upload\Storage
64
 */
65
class FileSystem implements StorageInterface
66
{
67
    /**
68
     * Path to move uploaded files
69
     * @var string
70
     */
71
    protected string $path;
72
73
    /**
74
     * Whether to overwrite existing file
75
     * @var bool
76
     */
77
    protected bool $overwrite = false;
78
79
    /**
80
     * Create new instance
81
     * @param string $path
82
     * @param bool $overwrite
83
     */
84
    public function __construct(string $path, bool $overwrite)
85
    {
86
        $this->overwrite = $overwrite;
87
        $directory = $this->normalizePath($path);
88
89
        if (is_dir($directory) === false || is_writable($directory) === false) {
90
            throw new InvalidArgumentException(sprintf(
91
                'Directory [%s] does not exist or is not writable',
92
                $directory
93
            ));
94
        }
95
96
        $this->path = $directory;
97
    }
98
99
    /**
100
     * {@inheritdoc}
101
     */
102
    public function upload(File $file): UploadFileInfo
103
    {
104
        $destinationFile = $this->path . $file->getFullName();
105
        if ($this->overwrite === false && file_exists($destinationFile)) {
106
            throw new StorageException(sprintf(
107
                'File [%s] already exists',
108
                $destinationFile
109
            ));
110
        }
111
112
        $uploaded = $this->moveUploadedFile($file->getPathname(), $destinationFile);
113
        if ($uploaded) {
114
            return new UploadFileInfo(
115
                $destinationFile,
116
                $file->getMimeType(),
117
                $file->getError(),
118
                $file->getSize(),
119
                $file->getMD5(),
120
                $file->getClientName()
121
            );
122
        }
123
124
        throw new UploadException(sprintf(
125
            'Error occured when move uploaded file [%s] to [%s]',
126
            $file->getPathname(),
127
            $destinationFile
128
        ));
129
    }
130
131
    /**
132
     * Move the uploaded file to final destination
133
     * @param string $source
134
     * @param string $destination
135
     * @return bool
136
     */
137
    protected function moveUploadedFile(string $source, string $destination): bool
138
    {
139
        return copy($source, $destination);
140
    }
141
142
    /**
143
     * Normalize the directory path
144
     * @param string $path
145
     * @return string
146
     */
147
    protected function normalizePath(string $path): string
148
    {
149
        $directory = rtrim($path, '\\/');
150
151
        return realpath($directory) . DIRECTORY_SEPARATOR;
152
    }
153
}
154