UploadException::codeToMessage()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * BEdita, API-first content management framework
4
 * Copyright 2018 ChannelWeb Srl, Chialab Srl
5
 *
6
 * This file is part of BEdita:you can redistribute it and/or modify
7
 * it under the terms of the GNU Lesser General Public License as published
8
 * by the Free Software Foundation, either version 3 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * See LICENSE.LGPL or <http://gnu.org/licenses/lgpl-3.0.html> for more details.
12
 */
13
14
namespace App\Core\Exception;
15
16
use Cake\Core\Exception\CakeException;
17
18
/**
19
 * Exception raised when uploading from form fails
20
 */
21
class UploadException extends CakeException
22
{
23
    /**
24
     * Array to map upload error codes with proper message string
25
     *
26
     * @var array
27
     */
28
    protected $messagesMap = [
29
        UPLOAD_ERR_INI_SIZE => 'The uploaded file exceeds current max size of {0}',
30
        UPLOAD_ERR_FORM_SIZE => 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form',
31
        UPLOAD_ERR_PARTIAL => 'The uploaded file was only partially uploaded',
32
        UPLOAD_ERR_NO_FILE => 'No file was uploaded',
33
        UPLOAD_ERR_NO_TMP_DIR => 'Missing a temporary folder',
34
        UPLOAD_ERR_CANT_WRITE => 'Failed to write file to disk',
35
        UPLOAD_ERR_EXTENSION => 'File upload stopped by extension',
36
    ];
37
38
    /**
39
     * {@inheritDoc}
40
     *
41
     * @codeCoverageIgnore
42
     */
43
    public function __construct(?string $message, int $code, $previous = null)
44
    {
45
        $message = $this->codeToMessage($code);
46
        parent::__construct($message, $code, $previous);
47
    }
48
49
    /**
50
     * Php code to message for exception
51
     *
52
     * @see http://php.net/manual/en/features.file-upload.errors.php for details
53
     * @param int $code The php code
54
     * @return string
55
     */
56
    private function codeToMessage(int $code): string
57
    {
58
        if (in_array($code, array_keys($this->messagesMap))) {
59
            return __($this->messagesMap[$code], ini_get('upload_max_filesize'));
60
        }
61
62
        return __('Unknown upload error');
63
    }
64
}
65