SimpleUpload   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 161
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 17
lcom 1
cbo 9
dl 0
loc 161
ccs 43
cts 43
cp 1
rs 10
c 0
b 0
f 0

11 Methods

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