Test Failed
Push — master ( 067306...cbca33 )
by Taosikai
02:30
created

UploadHandlerBuilder   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 186
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 19
eloc 48
dl 0
loc 186
rs 10
c 3
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A overwrite() 0 4 1
A sizeBetween() 0 9 2
A getHandler() 0 14 3
A naming() 0 7 2
A addConstraint() 0 4 1
A allowMimeTypes() 0 13 3
A saveTo() 0 3 1
A addProcessor() 0 7 2
A setFilesystem() 0 4 1
A allowExtensions() 0 13 3
1
<?php
2
3
namespace Slince\Upload;
4
5
use Slince\Upload\Constraint\ConstraintInterface;
6
use Slince\Upload\Constraint\ExtensionConstraint;
7
use Slince\Upload\Constraint\MimeTypeConstraint;
8
use Slince\Upload\Constraint\SizeConstraint;
9
use Slince\Upload\Filesystem\FilesystemInterface;
10
use Slince\Upload\Filesystem\Local;
11
use Slince\Upload\Naming\GenericNamer;
12
use Slince\Upload\Naming\NamerInterface;
13
use Slince\Upload\Naming\ClosureNamer;
14
use Slince\Upload\Processor\ChainProcessor;
15
use Slince\Upload\Processor\ClosureProcessor;
16
use Slince\Upload\Processor\ProcessorInterface;
17
18
class UploadHandlerBuilder
19
{
20
    /**
21
     * @var boolean
22
     */
23
    protected $overwrite = false;
24
25
    /**
26
     * @var NamerInterface
27
     */
28
    protected $namer;
29
30
    /**
31
     * @var FilesystemInterface
32
     */
33
    protected $filesystem;
34
35
    /**
36
     * @var ConstraintInterface[]
37
     */
38
    protected $constraints = [];
39
40
    /**
41
     * @var ProcessorInterface[]
42
     */
43
    protected $processors = [];
44
45
    /**
46
     * Sets overwrite mode.
47
     *
48
     * @param boolean $overwrite
49
     * @return $this
50
     */
51
    public function overwrite(bool $overwrite = true): self
52
    {
53
        $this->overwrite = $overwrite;
54
        return $this;
55
    }
56
57
    /**
58
     * Set allowed mime types.
59
     *
60
     * @param array|string $mimeTypes
61
     * @param string|null $errorMessageTemplate
62
     * @return $this
63
     */
64
    public function allowMimeTypes($mimeTypes, ?string $errorMessageTemplate = null): self
65
    {
66
        if (!is_array($mimeTypes)) {
67
            $mimeTypes = [$mimeTypes];
68
        }
69
70
        $constraint = new MimeTypeConstraint($mimeTypes);
71
        if ($errorMessageTemplate) {
72
            $constraint->setErrorMessage($errorMessageTemplate);
73
        }
74
75
        $this->constraints[] = $constraint;
76
        return $this;
77
    }
78
79
    /**
80
     * Set size range
81
     *
82
     * @param string|int|null $from
83
     * @param string|int|null $to
84
     * @param string|null $errorMessageTemplate
85
     * @return $this
86
     */
87
    public function sizeBetween($from, $to, ?string $errorMessageTemplate = null): self
88
    {
89
        $constraint = new SizeConstraint($from, $to);
90
        if (null !== $errorMessageTemplate) {
91
            $constraint->setErrorMessage($errorMessageTemplate);
92
        }
93
94
        $this->constraints[] = $constraint;
95
        return $this;
96
    }
97
98
    /**
99
     * Set allowed extensions
100
     *
101
     * @param array|string $extensions
102
     * @param string|null $errorMessageTemplate
103
     * @return $this
104
     */
105
    public function allowExtensions($extensions, ?string $errorMessageTemplate = null): self
106
    {
107
        if (!is_array($extensions)) {
108
            $extensions = [$extensions];
109
        }
110
111
        $constraint = new ExtensionConstraint($extensions);
112
        if (null !== $errorMessageTemplate) {
113
            $constraint->setErrorMessage($errorMessageTemplate);
114
        }
115
116
        $this->constraints[] = $constraint;
117
        return $this;
118
    }
119
120
    /**
121
     * Sets namer.
122
     *
123
     * @param \Closure|NamerInterface $namer
124
     * @return $this
125
     */
126
    public function naming($namer): self
127
    {
128
        if ($namer instanceof \Closure) {
129
            $namer = new ClosureNamer($namer);
130
        }
131
        $this->namer = $namer;
132
        return $this;
133
    }
134
135
    /**
136
     * Set filesystem.
137
     *
138
     * @param FilesystemInterface $filesystem
139
     * @return $this
140
     */
141
    public function setFilesystem(FilesystemInterface $filesystem): self
142
    {
143
        $this->filesystem = $filesystem;
144
        return $this;
145
    }
146
147
    /**
148
     * Sets save path
149
     *
150
     * @param string $path
151
     * @return $this
152
     */
153
    public function saveTo(string $path): self
154
    {
155
        return $this->setFilesystem(new Local($path));
156
    }
157
158
    /**
159
     * Add a constraint
160
     *
161
     * @param ConstraintInterface $constraint
162
     * @return $this
163
     */
164
    public function addConstraint(ConstraintInterface $constraint): self
165
    {
166
        $this->constraints[] = $constraint;
167
        return $this;
168
    }
169
170
    /**
171
     * Add to processor list
172
     *
173
     * @param \Closure|ProcessorInterface $processor
174
     * @return $this
175
     */
176
    public function addProcessor($processor): self
177
    {
178
        if ($processor instanceof \Closure) {
179
            $processor = new ClosureProcessor($processor);
180
        }
181
        $this->processors[] = $processor;
182
        return $this;
183
    }
184
185
    /**
186
     * Make upload handler
187
     *
188
     * @return UploadHandler
189
     */
190
    public function getHandler(): UploadHandler
191
    {
192
        if ($this->namer === null) {
193
            $this->namer = new GenericNamer();
194
        }
195
196
        if ($this->filesystem === null) {
197
            throw new \LogicException('You should set a filesystem for the builder.');
198
        }
199
200
        return new UploadHandler($this->filesystem, $this->namer,
201
            new Validator($this->constraints),
202
            new ChainProcessor($this->processors),
203
            $this->overwrite
204
        );
205
    }
206
}
207