Completed
Push — master ( 576435...3e25f9 )
by Ronaldo
05:23
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
        $this->setAdapter($this->file_system->getAdapter());
63
        $this->setError($this->upload->getError());
64
    }
65
66
    /**
67
     * @param array $upload
68
     * @param AdapterInterface $adapter
69
     * @param Translator|null $translator
70
     * @return self
71
     */
72
    public static function create(array $upload, AdapterInterface $adapter, Translator $translator = null)
73
    {
74
        return new self($upload, $adapter, $translator);
75
    }
76
77
    /**
78
     * @param $name
79
     * @return self
80
     */
81
    public function setName($name)
82
    {
83
        $this->newName = pathinfo($name, PATHINFO_FILENAME);
84
        return $this;
85
    }
86
87
    /**
88
     * @return string
89
     */
90
    public function getName()
91
    {
92
        return $this->newName;
93
    }
94
95
    /**
96
     * @param array $ext
97
     * @return self
98
     */
99
    public function setAllowedExtensions(array $ext)
100
    {
101
        $this->allowed_extensions = array_map(function ($value) {
102
            return mb_strtolower($value);
103
        }, $ext);
104
105
        return $this;
106
    }
107
108
    /**
109
     * @return File
110
     */
111
    public function send()
112
    {
113
        return FileFactory::createFromObject($this->sendUpload());
114
    }
115
116
    /**
117
     * @return Filesystem
118
     */
119
    public function getFileSystem()
120
    {
121
        return $this->file_system;
122
    }
123
124
    /**
125
     * @return bool
126
     */
127
    private function isValid()
128
    {
129
        if (!$this->extensionIsValid($this->upload->getFileExtension(), $this->allowed_extensions)) {
130
            unlink($this->upload->getTmpName());
131
132
            $msg = sprintf(
133
                $this->translator->getMessage('validations.invalidExtension'),
134
                implode(', ', $this->allowed_extensions)
135
            );
136
137
            throw new InvalidArgumentException($msg, 400);
138
        }
139
140
        if ($this->uploadWithError()) {
141
            $msg = $this->translator->getMessage('errors.upload.'.$this->upload->getError());
142
            throw new RuntimeException($msg, 500);
143
        }
144
145
        return true;
146
    }
147
148
    /**
149
     * @return string
150
     */
151
    public function getNewNameFile()
152
    {
153
        $fileName = (is_null($this->newName)) ? Text::slug($this->upload->getFileName()) : $this->getName();
154
        return $fileName . '.' . mb_strtolower($this->upload->getFileExtension());
155
    }
156
157
    /**
158
     * @return self
159
     */
160
    private function sendUpload()
161
    {
162
        if ($this->file_system->has($this->getNewNameFile())) {
163
            $this->file_system->delete($this->getNewNameFile());
164
        }
165
166
        if ($this->isValid()) {
167
            $content = file_get_contents($this->upload->getTmpName());
168
            $this->file_system->write($this->getNewNameFile(), $content);
169
        }
170
171
        return $this;
172
    }
173
}
174