Completed
Push — master ( e6659e...855352 )
by Ronaldo
13:10 queued 01:13
created

SimpleUpload::getError()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace WSW\SimpleUpload\Services;
4
5
use InvalidArgumentException;
6
use League\Flysystem\AdapterInterface;
7
use League\Flysystem\Filesystem;
8
use RuntimeException;
9
use WSW\SimpleUpload\Entities\File;
10
use WSW\SimpleUpload\Entities\Upload;
11
use WSW\SimpleUpload\Factories\Entities\FileFactory;
12
use WSW\SimpleUpload\Factories\Entities\UploadFactory;
13
use WSW\SimpleUpload\Support\Text;
14
use WSW\SimpleUpload\Traits\Upload\Path;
15
use WSW\SimpleUpload\Traits\Upload\Validations;
16
17
/**
18
 * Class SimpleUpload
19
 * @package WSW\SimpleUpload\Services
20
 */
21
class SimpleUpload
22
{
23
    use Validations;
24
    use Path;
25
26
    /**
27
     * @var Upload
28
     */
29
    private $upload;
30
31
    /**
32
     * @var Filesystem
33
     */
34
    private $file_system;
35
36
    /**
37
     * @var Translator
38
     */
39
    private $translator;
40
41
    /**
42
     * @var string
43
     */
44
    private $newName;
45
46
    /**
47
     * @var array
48
     */
49
    private $allowed_extensions = [];
50
51
    /**
52
     * @param array $upload
53
     * @param AdapterInterface $adapter
54
     * @param Translator|null $translator
55
     */
56
    public function __construct(array $upload, AdapterInterface $adapter, Translator $translator = null)
57
    {
58
        $this->translator  = $translator ?: Translator::locate();
59
        $this->file_system = new Filesystem($adapter);
60
        $this->upload      = UploadFactory::createFromArray($upload, $this->translator);
61
    }
62
63
    /**
64
     * @param array $upload
65
     * @param AdapterInterface $adapter
66
     * @param Translator|null $translator
67
     * @return self
68
     */
69
    public static function create(array $upload, AdapterInterface $adapter, Translator $translator = null)
70
    {
71
        return new self($upload, $adapter, $translator);
72
    }
73
74
    /**
75
     * @return AdapterInterface
76
     */
77
    public function getAdapter()
78
    {
79
        return $this->file_system->getAdapter();
80
    }
81
82
    /**
83
     * @return int
84
     */
85
    public function getError()
86
    {
87
        return $this->upload->getError();
88
    }
89
90
    /**
91
     * @param $name
92
     * @return self
93
     */
94
    public function setName($name)
95
    {
96
        $this->newName = pathinfo($name, PATHINFO_FILENAME);
97
        return $this;
98
    }
99
100
    /**
101
     * @return string
102
     */
103
    public function getName()
104
    {
105
        return $this->newName;
106
    }
107
108
    /**
109
     * @param array $ext
110
     * @return self
111
     */
112
    public function setAllowedExtensions(array $ext)
113
    {
114
        $this->allowed_extensions = array_map(function ($value) {
115
            return mb_strtolower($value);
116
        }, $ext);
117
118
        return $this;
119
    }
120
121
    /**
122
     * @return File
123
     */
124
    public function send()
125
    {
126
        return FileFactory::createFromObject($this->sendUpload());
127
    }
128
129
    /**
130
     * @return Filesystem
131
     */
132
    public function getFileSystem()
133
    {
134
        return $this->file_system;
135
    }
136
137
    /**
138
     * @return bool
139
     */
140
    private function isValid()
141
    {
142
        if (!$this->extensionIsValid($this->upload->getFileExtension(), $this->allowed_extensions)) {
143
            unlink($this->upload->getTmpName());
144
145
            $msg = sprintf(
146
                $this->translator->getMessage('validations.invalidExtension'),
147
                implode(', ', $this->allowed_extensions)
148
            );
149
150
            throw new InvalidArgumentException($msg, 400);
151
        }
152
153
        if ($this->uploadWithError()) {
154
            $msg = $this->translator->getMessage('errors.upload.'.$this->upload->getError());
155
            throw new RuntimeException($msg, 500);
156
        }
157
158
        return true;
159
    }
160
161
    /**
162
     * @return string
163
     */
164
    public function getNewNameFile()
165
    {
166
        $fileName = (is_null($this->newName)) ? Text::slug($this->upload->getFileName()) : $this->getName();
167
        return $fileName . '.' . mb_strtolower($this->upload->getFileExtension());
168
    }
169
170
    /**
171
     * @return self
172
     */
173
    private function sendUpload()
174
    {
175
        if ($this->file_system->has($this->getNewNameFile())) {
176
            $this->file_system->delete($this->getNewNameFile());
177
        }
178
179
        if ($this->isValid()) {
180
            $content = file_get_contents($this->upload->getTmpName());
181
            $this->file_system->write($this->getNewNameFile(), $content);
182
        }
183
184
        return $this;
185
    }
186
}
187