Test Failed
Push — master ( ca159c...8f4401 )
by Sebastian
03:00
created

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