Passed
Push — master ( 1b90b9...7956e0 )
by Gabriel
03:50 queued 11s
created

Nip_File_Upload   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 23
c 1
b 0
f 1
dl 0
loc 43
rs 10
wmc 15

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 2
A upload() 0 3 1
C valid() 0 23 12
1
<?php
2
3
class Nip_File_Upload extends Nip_File
4
{
5
    protected $_error;
6
    protected $_tmp_name;
7
8
    public function __construct($data = [])
9
    {
10
        if ($data) {
11
            $this->_name = $data['name'];
12
            $this->_tmp_name = $data['tmp_name'];
13
14
            parent::__construct(TMP_PATH.$this->_tmp_name);
0 ignored issues
show
Bug introduced by
The constant TMP_PATH was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
15
        }
16
    }
17
18
    public function valid()
19
    {
20
        $result = false;
0 ignored issues
show
Unused Code introduced by
The assignment to $result is dead and can be removed.
Loading history...
21
22
        $max_upload = ini_get('post_max_size');
23
        $unit = strtoupper(substr($max_upload, -1));
24
        $multiplier = ($unit == 'M') ? 1048576 : (($unit == 'K') ? 1024 : (($unit == 'G') ? 1073741824 : 1));
25
26
        if ($max_upload && ((int) $_SERVER['CONTENT_LENGTH'] > $multiplier * (int) $max_upload)) {
27
            return self::ERROR_MAX_POST_SIZE;
28
        }
29
30
        if (!$this->getPath()) {
31
            return false;
32
        } elseif (isset($this->error) && $this->error != 0) {
33
            return false;
34
        } elseif (!isset($this->_tmp_name) || !@is_uploaded_file($this->_tmp_name)) {
35
            return false;
36
        } elseif (!isset($this->_name)) {
37
            return false;
38
        }
39
40
        return true;
41
    }
42
43
    public function upload($path)
44
    {
45
        return $this->move($path);
46
    }
47
}
48