Passed
Push — master ( 6c10ed...067306 )
by Taosikai
05:29 queued 02:54
created

UploadHandlerBuilder::addProcess()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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