HttpFormFile::generateTargetFilePath()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 2
crap 1
1
<?php
2
3
/*
4
 * Copyright (c) 2011-2015, Celestino Diaz <[email protected]>
5
 *
6
 * Permission is hereby granted, free of charge, to any person obtaining a copy
7
 * of this software and associated documentation files (the "Software"), to deal
8
 * in the Software without restriction, including without limitation the rights
9
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
 * copies of the Software, and to permit persons to whom the Software is
11
 * furnished to do so, subject to the following conditions:
12
 *
13
 * The above copyright notice and this permission notice shall be included in
14
 * all copies or substantial portions of the Software.
15
 *
16
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22
 * THE SOFTWARE.
23
 */
24
25
namespace Brickoo\Component\Http;
26
27
use Brickoo\Component\Common\Assert;
28
use Brickoo\Component\Http\Exception\HttpFormFileNotFoundException;
29
use Brickoo\Component\Http\Exception\UnableToMoveFormFileException;
30
31
/**
32
 * HttpFormFile
33
 *
34
 * Implements a http form file.
35
 * @author Celestino Diaz <[email protected]>
36
 */
37
class HttpFormFile {
38
39
    /** @var string */
40
    private $name;
41
42
    /** @var string */
43
    private $filePath;
44
45
    /** @var integer */
46
    private $fileSize;
47
48
    /** @var integer */
49
    private $errorCode;
50
51
    /**
52
     * Class constructor.
53
     * @param string $name
54
     * @param string $filePath
55
     * @param integer $fileSize
56
     * @param integer $errorCode
57
     * @throws \InvalidArgumentException
58
     */
59 1
    public function __construct($name, $filePath, $fileSize, $errorCode) {
60 1
        Assert::isString($name);
61 1
        Assert::isString($filePath);
62 1
        Assert::isInteger($fileSize);
63 1
        Assert::isInteger($errorCode);
64 1
        $this->name = $name;
65 1
        $this->filePath = $filePath;
66 1
        $this->fileSize = $fileSize;
67 1
        $this->errorCode = $errorCode;
68 1
    }
69
70
    /**
71
     * Check if the file did produce an error.
72
     * @return boolean check result
73
     */
74 1
    public function hasError() {
75 1
        return $this->errorCode !== UPLOAD_ERR_OK;
76
    }
77
78
    /**
79
     * Return the error code.
80
     * @return integer
81
     */
82 1
    public function getErrorCode() {
83 1
        return $this->errorCode;
84
    }
85
86
    /**
87
     * Return the original file name.
88
     * @return string
89
     */
90 1
    public function getName() {
91 1
        return $this->name;
92
    }
93
94
    /**
95
     * Return the file sie in bytes.
96
     * @return integer
97
     */
98 1
    public function getSize() {
99 1
        return $this->fileSize;
100
    }
101
102
    /**
103
     * Move the form file to a target location.
104
     * @param string $targetPath
105
     * @param string $targetFileName
106
     * @throws \InvalidArgumentException
107
     * @throws \Brickoo\Component\Http\Exception\HttpFormFileNotFoundException
108
     * @throws \Brickoo\Component\Http\Exception\UnableToMoveFormFileException
109
     * @return string target file path
110
     */
111 3
    public function moveTo($targetPath, $targetFileName) {
112 3
        Assert::isString($targetPath);
113 3
        Assert::isString($targetFileName);
114
115 3
        if (!file_exists($this->filePath)) {
116 1
            throw new HttpFormFileNotFoundException($this->filePath);
117
        }
118
119 2
        $targetFilePath = $this->generateTargetFilePath($targetPath, $targetFileName);
120
121 2
        if ((!is_writable(dirname($targetFilePath)))
122 2
            || (!move_uploaded_file($this->filePath, $targetFilePath))) {
123 1
                throw new UnableToMoveFormFileException($this->filePath, $targetFilePath);
124
        }
125
126 1
        return $targetFilePath;
127
    }
128
129
    /**
130
     * Generate an upload target file path.
131
     * @param string $targetPath
132
     * @param string $targetFileName
133
     * @return string the generated target file path
134
     */
135 1
    private function generateTargetFilePath($targetPath, $targetFileName) {
136 1
        return rtrim($targetPath, "\\/").DIRECTORY_SEPARATOR.substr(utf8_encode($targetFileName), 0, 255);
137
    }
138
139
}
140