Test Failed
Push — master ( 9a4759...0f3564 )
by Sebastian
04:15 queued 59s
created

FolderInfo::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 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AppUtils\FileHelper;
6
7
use AppUtils\FileHelper;
8
use AppUtils\FileHelper_Exception;
9
use DirectoryIterator;
10
11
/**
12
 * @method FolderInfo requireReadable(?int $errorCode = null)
13
 * @method FolderInfo requireExists(?int $errorCode = null)
14
 * @method FolderInfo requireWritable(?int $errorCode = null)
15
 */
16
class FolderInfo extends AbstractPathInfo
17
{
18
    /**
19
     * @var array<string,FolderInfo>
20
     */
21
    private static $infoCache = array();
22
23
    /**
24
     * @param string|PathInfoInterface|DirectoryIterator $path
25
     * @return FolderInfo
26
     * @throws FileHelper_Exception
27
     */
28
    public static function factory($path) : FolderInfo
29
    {
30
        $pathString = AbstractPathInfo::type2string($path);
31
32
        if(!isset(self::$infoCache[$pathString]))
33
        {
34
            self::$infoCache[$pathString] = new FolderInfo($pathString);
35
        }
36
37
        return self::$infoCache[$pathString];
38
    }
39
40
    /**
41
     * Clears the folder cache that keeps track of any folders
42
     * created via {@see FolderInfo::factory()} for performance
43
     * reasons.
44
     *
45
     * @return void
46
     */
47
    public static function clearCache() : void
48
    {
49
        self::$infoCache = array();
50
    }
51
52
    /**
53
     * @param string $path
54
     * @throws FileHelper_Exception
55
     * @see FileHelper::ERROR_PATH_IS_NOT_A_FOLDER
56
     */
57
    public function __construct(string $path)
58
    {
59
        parent::__construct($path);
60
61
        if(!self::is_dir($this->path))
62
        {
63
            throw new FileHelper_Exception(
64
                'Not a folder',
65
                sprintf('The path is not a folder: [%s].', $this->path),
66
                FileHelper::ERROR_PATH_IS_NOT_A_FOLDER
67
            );
68
        }
69
    }
70
71
    /**
72
     * Detects if the target path is a folder. If the folder
73
     * does not exist, returns true if the path does not
74
     * contain a file extension.
75
     *
76
     * @param string $path
77
     * @return bool
78
     */
79
    public static function is_dir(string $path) : bool
80
    {
81
        $path = trim($path);
82
83
        if($path === '' || $path === '.' || $path === '..')
84
        {
85
            return false;
86
        }
87
88
        if(is_dir($path))
89
        {
90
            return true;
91
        }
92
93
        $path = FileHelper::normalizePath($path);
94
95
        return pathinfo($path, PATHINFO_EXTENSION) === '';
96
    }
97
98
    /**
99
     * @return $this
100
     *
101
     * @throws FileHelper_Exception
102
     * @see FileHelper::ERROR_CANNOT_DELETE_FOLDER
103
     */
104
    public function delete() : FolderInfo
105
    {
106
        if(!$this->exists())
107
        {
108
            return $this;
109
        }
110
111
        if(rmdir($this->path))
112
        {
113
            return $this;
114
        }
115
116
        throw new FileHelper_Exception(
117
            sprintf(
118
                'Cannot delete folder [%s].',
119
                $this->getName()
120
            ),
121
            sprintf(
122
                'The folder could not be deleted at path: [%s]',
123
                $this->getPath()
124
            ),
125
            FileHelper::ERROR_CANNOT_DELETE_FOLDER
126
        );
127
    }
128
129
    /**
130
     * @return $this
131
     *
132
     * @throws FileHelper_Exception
133
     * @see FileHelper::ERROR_CANNOT_CREATE_FOLDER
134
     */
135
    public function create() : FolderInfo
136
    {
137
        if(is_dir($this->path) || mkdir($this->path, 0777, true) || is_dir($this->path))
138
        {
139
            return $this;
140
        }
141
142
        throw new FileHelper_Exception(
143
            sprintf(
144
                'Could not create target folder [%s].',
145
                $this->getName()
146
            ),
147
            sprintf(
148
                'Tried to create the folder in path [%s].',
149
                $this->getPath()
150
            ),
151
            FileHelper::ERROR_CANNOT_CREATE_FOLDER
152
        );
153
    }
154
155
    public function getRelativeTo(FolderInfo $folder) : string
156
    {
157
        return FileHelper::relativizePath($this->getPath(), $folder->getPath());
158
    }
159
160
    public function createFolderFinder() : FolderFinder
161
    {
162
        return new FolderFinder($this);
163
    }
164
165
    public function getIterator() : DirectoryIterator
166
    {
167
        $this->requireExists()->requireIsFolder();
168
169
        return new DirectoryIterator($this->getPath());
170
    }
171
172
    public function getExtension(bool $lowercase = true) : string
173
    {
174
        return '';
175
    }
176
}
177