Passed
Push — master ( 5ccd86...1b90b9 )
by Gabriel
03:29
created

Nip_File::getExtension()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
class Nip_File_Exception extends Exception
4
{
5
}
6
7
class Nip_File extends Nip_Object
0 ignored issues
show
Bug introduced by
The type Nip_Object was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
{
9
    protected $_path;
10
    protected $_name;
11
    protected $_extension;
12
13
    public function __construct($path = false)
14
    {
15
        if ($path) {
16
            $this->setPath($path);
17
        }
18
    }
19
20
    public function move($target)
21
    {
22
        $dir = dirname($target);
23
        if (!is_dir($target)) {
24
            mkdir($dir, 0755, true);
25
        }
26
27
        if (rename($this->getPath(), $target)) {
28
            $this->setPath($target);
29
        } else {
30
            throw new Nip_File_Exception("Cannot move $this->_path file to $target");
31
        }
32
33
        return $this;
34
    }
35
36
    public function copy($target)
37
    {
38
        $dir = dirname($target);
39
        if (!is_dir($target)) {
40
            mkdir($dir, 0755, true);
41
        }
42
43
        if (copy($this->getPath(), $target)) {
44
            $this->setPath($target);
45
        } else {
46
            throw new Nip_File_Exception("Cannot copy $this->_path file to $target");
47
        }
48
49
        return $this;
50
    }
51
52
    /**
53
     * @param string $target
54
     *
55
     * @return Nip_Process
56
     */
57
    public function unzip($target)
58
    {
59
        if (!is_dir($target)) {
60
            mkdir($target, 0755, true);
61
        }
62
63
        $process = new Nip_Process("unzip {$this->_path}  -d $target");
0 ignored issues
show
Bug introduced by
The type Nip_Process was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
64
        $process->run();
65
66
        return $process;
67
    }
68
69
    public function download($filename = false, $contentType = false)
70
    {
71
        if (!$filename) {
72
            $filename = $this->getName();
73
        }
74
        if (!$contentType) {
75
            $contentType = 'application/force-download';
76
        }
77
78
        header('Pragma: public');
79
        header('Expires: 0');
80
        header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
81
        header('Cache-Control: private', false);
82
        header("Content-Type: $contentType");
83
        header("Content-Disposition: attachment; filename=\"$filename\"");
84
        header("Content-Length: {$this->getSize()}");
85
        header('Content-Transfer-Encoding: binary');
86
87
        readfile($this->getPath());
88
        exit();
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
89
    }
90
91
    public function delete()
92
    {
93
        unlink($this->getPath());
94
    }
95
96
    public function setPath($path)
97
    {
98
        $this->_name = basename($path);
99
        $this->_extension = pathinfo($path, PATHINFO_EXTENSION);
100
        $this->_path = $path;
101
102
        return $this;
103
    }
104
105
    public function getName()
106
    {
107
        return $this->_name;
108
    }
109
110
    public function getExtension()
111
    {
112
        return $this->_extension;
113
    }
114
115
    public function getSize()
116
    {
117
        return filesize($this->getPath());
118
    }
119
120
    public function getTime()
121
    {
122
        return filemtime($this->getPath());
123
    }
124
125
    public function getPath()
126
    {
127
        return $this->_path;
128
    }
129
130
    public function getMimeType()
131
    {
132
        if (function_exists('mime_content_type')) {
133
            return mime_content_type($this->getPath());
134
        } elseif (function_exists('finfo_open')) {
135
            $finfo = finfo_open(FILEINFO_MIME);
136
            $mimetype = finfo_file($finfo, $this->getPath());
137
            finfo_close($finfo);
138
139
            return $mimetype;
140
        }
141
142
        return 'unknown';
143
    }
144
}
145