Completed
Push — master ( b5c7fa...23b509 )
by Taosikai
13:59
created

UploadHandlerBuilder::getHandler()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
c 0
b 0
f 0
rs 9.8666
cc 3
nc 4
nop 0
1
<?php
2
3
/*
4
 * This file is part of the slince/upload package.
5
 *
6
 * (c) Slince <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Slince\Upload;
13
14
15
use Slince\Upload\Constraint\ConstraintInterface;
16
use Slince\Upload\Constraint\ExtensionConstraint;
17
use Slince\Upload\Constraint\MimeTypeConstraint;
18
use Slince\Upload\Constraint\SizeConstraint;
19
use Slince\Upload\Filesystem\FilesystemInterface;
20
use Slince\Upload\Filesystem\Local;
21
use Slince\Upload\Naming\GenericNamer;
22
use Slince\Upload\Naming\NamerInterface;
23
use Slince\Upload\Naming\ClosureNamer;
24
25
final class UploadHandlerBuilder
26
{
27
    /**
28
     * @var boolean
29
     */
30
    protected $overwrite;
31
32
    /**
33
     * @var NamerInterface
34
     */
35
    protected $namer;
36
37
    /**
38
     * @var FilesystemInterface
39
     */
40
    protected $filesystem;
41
42
    /**
43
     * @var ConstraintInterface[]
44
     */
45
    protected $constraints = [];
46
47
    /**
48
     * Sets overwrite mode.
49
     *
50
     * @param boolean $overwrite
51
     * @return $this
52
     */
53
    public function overwrite($overwrite = true)
54
    {
55
        $this->overwrite = $overwrite;
56
        return $this;
57
    }
58
59
    /**
60
     * Set allowed mime types.
61
     *
62
     * @param array|string $mimeTypes
63
     * @return $this
64
     */
65
    public function allowMimeTypes($mimeTypes)
66
    {
67
        if (!is_array($mimeTypes)) {
68
            $mimeTypes = [$mimeTypes];
69
        }
70
        $this->constraints[] = new MimeTypeConstraint($mimeTypes);
71
        return $this;
72
    }
73
74
    /**
75
     * Set size range
76
     *
77
     * @param string|int|null $from
78
     * @param string|int|null $to
79
     * @return $this
80
     */
81
    public function sizeBetween($from, $to)
82
    {
83
        $this->constraints[] = new SizeConstraint($from, $to);
84
        return $this;
85
    }
86
87
    /**
88
     * Set allowed extensions
89
     *
90
     * @param array|string $extensions
91
     * @return $this
92
     */
93
    public function allowExtensions($extensions)
94
    {
95
        if (!is_array($extensions)) {
96
            $extensions = [$extensions];
97
        }
98
        $this->constraints[] = new ExtensionConstraint($extensions);
99
        return $this;
100
    }
101
102
    /**
103
     * Sets namer.
104
     *
105
     * @param \Closure|NamerInterface $namer
106
     * @return $this
107
     */
108
    public function naming($namer)
109
    {
110
        if ($namer instanceof \Closure) {
111
            $namer = new ClosureNamer($namer);
112
        }
113
        $this->namer = $namer;
114
        return $this;
115
    }
116
117
    /**
118
     * Set filesystem.
119
     *
120
     * @param FilesystemInterface $filesystem
121
     * @return $this
122
     */
123
    public function setFilesystem(FilesystemInterface $filesystem)
124
    {
125
        $this->filesystem = $filesystem;
126
        return $this;
127
    }
128
129
    /**
130
     * Sets save path
131
     *
132
     * @param string $path
133
     * @return $this
134
     */
135
    public function saveTo($path)
136
    {
137
        return $this->setFilesystem(new Local($path));
138
    }
139
140
    /**
141
     * Add a constraint
142
     *
143
     * @param ConstraintInterface $constraint
144
     * @return $this
145
     */
146
    public function addConstraint(ConstraintInterface $constraint)
147
    {
148
        $this->constraints[] = $constraint;
149
        return $this;
150
    }
151
152
    /**
153
     * Make upload handler
154
     *
155
     * @return UploadHandler
156
     */
157
    public function getHandler()
158
    {
159
        if ($this->namer === null) {
160
            $this->namer = new GenericNamer();
161
        }
162
        $handler = new UploadHandler($this->filesystem, $this->namer, $this->overwrite);
163
        $validator = $handler->getValidator();
164
        foreach ($this->constraints as $constraint) {
165
            $validator->addConstraint($constraint);
166
        }
167
        return $handler;
168
    }
169
}