Completed
Push — master ( e44baf...dee4cd )
by Gabriel
12:09 queued 11s
created

Nip_File::move()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 15

Duplication

Lines 15
Ratio 100 %

Importance

Changes 0
Metric Value
dl 15
loc 15
c 0
b 0
f 0
rs 9.7666
cc 3
nc 4
nop 1
1
<?php
2
3
class Nip_File_Exception extends Exception
4
{
5
}
6
7
class Nip_File extends Nip_Object
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 View Code Duplication
    public function move($target)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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 View Code Duplication
    public function copy($target)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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");
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();
0 ignored issues
show
Bug introduced by
Consider using $this->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
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();
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