UploadController::defaultDirHandler()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
c 0
b 0
f 0
nc 2
nop 0
dl 0
loc 12
rs 9.4285
1
<?php
2
namespace samsonphp\upload;
3
4
use samson\core\CompressableExternalModule;
5
6
/**
7
 * SamsonPHP Upload module
8
 *
9
 * @package SamsonPHP
10
 * @author Vitaly Iegorov <[email protected]>
11
 */
12
class UploadController extends CompressableExternalModule
13
{
14
    /** @var string Module identifier */
15
    public $id = 'upload';
16
17
    /** @var  callable External handler to build relative file path */
18
    public $uploadDirHandler;
19
20
    /** @var string Prefix for image path saving in db */
21
    public $pathPrefix = '/';
22
23
    /** @var callable External handler to build file name */
24
    public $fileNameHandler;
25
26
    /** @var \samsonphp\fs\FileService Pointer to file system module */
27
    public $fs;
28
29
    /**
30
     * Init current file system module and server requests handler
31
     */
32
    protected function initFileSystem()
33
    {
34
        /** @var \samsonphp\fs\FileService $fs */
35
        $fs = !isset($this->fs) ? $this->system->module('fs') : $this->fs;
0 ignored issues
show
Documentation introduced by
The property system does not exist on object<samsonphp\upload\UploadController>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
36
37
        // Store pointer to file system module
38
        $this->fs = $fs;
39
    }
40
41
    /**
42
     * Initialize module
43
     * @param array $params Collection of module parameters
44
     * @return bool True if module successfully initialized
45
     */
46
    public function init(array $params = array())
47
    {
48
        // Init FileSystem and ServerHandler
49
        $this->initFileSystem();
50
51
        // If no valid handlers are passed - use generic handlers
52
        if (!isset($this->uploadDirHandler) || !is_callable($this->uploadDirHandler)) {
53
            $this->uploadDirHandler = array($this, 'defaultDirHandler');
54
        }
55
56
        // Call parent initialization
57
        parent::init($params);
58
    }
59
60
    /**
61
     * Default relative path builder handler
62
     * @return string Relative path for uploading
63
     */
64
    public function defaultDirHandler()
65
    {
66
        // Default file path
67
        $path = 'upload';
68
69
        // Create upload dir if it does not present
70
        if (!$this->fs->exists($path)) {
71
            $this->fs->mkDir($path);
72
        }
73
74
        return $path;
75
    }
76
77
    /**
78
     * Returns a file size limit in bytes based on the PHP upload_max_filesize and post_max_size
79
     * @return float Size value
80
     */
81
    public function __async_maxfilesize()
0 ignored issues
show
Coding Style introduced by
Method name "UploadController::__async_maxfilesize" is not in camel caps format
Loading history...
82
    {
83
        $sizeString = ini_get('post_max_size');
84
        // Start with post_max_size.
85
        $maxSize = $this->parseSize($sizeString);
86
87
        // If upload_max_size is less, then reduce. Except if upload_max_size is
88
        // zero, which indicates no limit.
89
        $uploadMax = $this->parseSize(ini_get('upload_max_filesize'));
90
        if ($uploadMax > 0 && $uploadMax < $maxSize) {
91
            $maxSize = $uploadMax;
92
            $sizeString = ini_get('upload_max_filesize');
93
        }
94
        return array('status' => true, 'maxSize' => $maxSize, 'sizeString' => $sizeString);
95
    }
96
97
    /**
98
     * Function to get size in bytes
99
     * @param string $size File size string
100
     * @return float Size number
101
     */
102
    private function parseSize($size)
103
    {
104
        // Remove the non-unit characters from the size.
105
        $unit = preg_replace('/[^bkmgtpezy]/i', '', $size);
106
        // Remove the non-numeric characters from the size.
107
        $size = preg_replace('/[^0-9\.]/', '', $size);
108
        if ($unit) {
109
            // Find the position of the unit in the ordered
110
            // string which is the power of magnitude to multiply a kilobyte by.
111
            return round($size * pow(1024, stripos('bkmgtpezy', $unit[0])));
112
        } else {
113
            return round($size);
114
        }
115
    }
116
}
117