AbstractFileService::mkDir()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
dl 0
loc 1
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: egorov
5
 * Date: 03.12.2014
6
 * Time: 20:26
7
 */
8
namespace samsonphp\fs;
9
10
/**
11
 * Abstract IFileService implementation
12
 * with higher level functions implemented
13
 * @package samsonphp\fs
14
 */
15
abstract class AbstractFileService implements IFileSystem
16
{
17
    /** @var array Collection of mime => extension */
18
    public static $mimes = array
19
    (
20
        'text/css' 					=> 'css',
21
        'application/x-font-woff' 	=> 'woff',
22
        'application/x-javascript' 	=> 'js',
23
        'text/html;charset=utf-8'	=>'htm',
24
        'text/x-component' 		=> 'htc',
25
        'image/jpeg' 			=> 'jpg',
26
        'image/pjpeg' 			=> 'jpg',
27
        'image/png' 			=> 'png',
28
        'image/x-png' 			=> 'png',
29
        'image/jpg' 			=> 'jpg',
30
        'image/gif' 			=> 'gif',
31
        'text/plain' 			=> 'txt',
32
        'application/pdf' 		=> 'pdf',
33
        'application/zip' 		=> 'zip',
34
        'application/rtf' 		=> 'rtf',
35
        'application/msword' 	=> 'doc',
36
        'application/msexcel' 	=> 'xls',
37
        'application/vnd.ms-excel'  => 'xls',
38
        'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' => 'xlsx',
39
        'application/vnd.openxmlformats-officedocument.wordprocessingml.document' => 'docx',
40
        'application/octet-stream' 	=> 'sql',
41
        'audio/mpeg'	=> 'mp3',
42
        'text/x-c++'    => 'php',
43
    );
44
45
    /**
46
     * File service initialization stage
47
     */
48
    public function initialize()
49
    {
50
        // Should be overloaded
51
    }
52
53
    /**
54
     * Get file mime type in current file system
55
     * @param $filePath string Path to file
56
     * @return false|integer|string false if mime not found, otherwise file mime type
57
     */
58
    public function mime($filePath)
59
    {
60
        // Get file extension from path
61
        $extension = $this->extension($filePath);
62
63
        // Search mimes array and return mime for file otherwise false
64
        return array_search($extension, self::$mimes);
65
    }
66
67
    /**
68
     * Get relative path from $path
69
     * @param string $fullPath  Full file path
70
     * @param string $fileName  File name
71
     * @param string $basePath  Base path, must end WITHOUT '/', if not passed
72
     *                          $fullPath one level top directory is used.
73
     * @return string Relative path to file
74
     */
75
    public function relativePath($fullPath, $fileName, $basePath = null)
76
    {
77
        // If no basePath is passed consider that we must go ne level up from $fullPath
78
        $basePath = !isset($basePath) ? dirname($fullPath) : $basePath;
79
80
        // Get dir from path and remove file name of it if no dir is present
81
        return str_replace($basePath.'/', '', str_replace($fileName, '', $fullPath));
82
    }
83
84
    /**
85
     * Copy folder to selected location.
86
     * Copy can be performed from file($filePath) to file($newPath),
87
     * also copy can be performed from folder($filePath) to folder($newPath),
88
     * currently copying from file($filePath) to folder($newPath) is not supported.
89
     *
90
     * @param string $filePath      Source path or file path
91
     * @param string $newPath       New path or file path
92
     * @return bool|null False if failed otherwise true if file/folder has been copied
93
     */
94
    protected function copyFolder($filePath, $newPath)
95
    {
96
        // Read directory
97
        foreach ($this->dir($filePath) as $file) {
98
            // Get file name
99
            $fileName = basename($file);
100
            // Read source file and write to new location
101
            $this->write(
102
                $this->read($file, $fileName),
103
                $fileName,
104
                $newPath
105
            );
106
        }
107
    }
108
109
    /**
110
     * Copy file to selected location.
111
     * Copy can be performed from file($filePath) to file($newPath),
112
     * also copy can be performed from folder($filePath) to folder($newPath),
113
     * currently copying from file($filePath) to folder($newPath) is not supported.
114
     *
115
     * @param string $filePath      Source path or file path
116
     * @param string $newPath       New path or file path
117
     */
118
    protected function copyFile($filePath, $newPath)
119
    {
120
        // Get file name
121
        $fileName = basename($newPath);
122
123
        // Read and write file
124
        $this->write(
125
            $this->read($filePath, $fileName),
126
            $fileName,
127
            dirname($newPath)
128
        );
129
    }
130
131
    /**
132
     * Copy file/folder to selected location.
133
     * Copy can be performed from file($filePath) to file($newPath),
134
     * also copy can be performed from folder($filePath) to folder($newPath),
135
     * currently copying from file($filePath) to folder($newPath) is not supported.
136
     *
137
     * @param string $filePath      Source path or file path
138
     * @param string $newPath       New path or file path
139
     * @return boolean False if failed otherwise true if file/folder has been copied
140
     */
141
    public function copyPath($filePath, $newPath)
142
    {
143
        // Check if source file exists
144
        if ($this->exists($filePath)) {
145
            // If this is directory
146
            if ($this->isDir($filePath)) {
147
                $this->copyFolder($filePath, $newPath);
148
            } else { // Read source file and write to new location
149
                $this->copyFile($filePath, $newPath);
150
            }
151
152
            // Return success
153
            return true;
154
        } else { // Signal error
155
            return e(
0 ignored issues
show
Deprecated Code introduced by
The function e() has been deprecated with message: Use custom exceptions

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
156
                'Cannot copy file[##] to [##] - Source file does not exists',
157
                E_SAMSON_CORE_ERROR,
158
                array($filePath, $newPath)
159
            );
160
        }
161
    }
162
163
    /**
164
     * Create catalog in selected location
165
     * @param string    $path   Path for new catalog
166
     * @return boolean  Result of catalog creating
167
     */
168
    abstract public function mkDir($path);
169
}
170