Passed
Branch dev (152e5d)
by compolom
02:01
created

FSHelper   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 90
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
eloc 21
c 2
b 0
f 1
dl 0
loc 90
rs 10
wmc 7

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getFilesSize() 0 8 2
A getFiles() 0 5 1
A getInfoDirectory() 0 6 1
A getAll() 0 15 2
A getDirectories() 0 5 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Compolomus\FSHelper;
6
7
use CallbackFilterIterator;
8
use Compolomus\FSHelper\Exceptions\PathNotFoundException;
9
use FilesystemIterator;
10
use RecursiveDirectoryIterator;
11
use RecursiveIteratorIterator;
12
13
class FSHelper
14
{
15
    /**
16
     * Return all object with information about all files and folder by provided path
17
     *
18
     * @param string $directoryPath Absolute or relative path
19
     *
20
     * @return \RecursiveIteratorIterator
21
     * @throws \Compolomus\FSHelper\Exceptions\PathNotFoundException
22
     */
23
    public static function getAll(string $directoryPath): RecursiveIteratorIterator
24
    {
25
        $directoryPathReal = realpath($directoryPath);
26
27
        // Throw an exception if the path does not exist
28
        if (!$directoryPathReal) {
29
            throw new PathNotFoundException($directoryPath);
30
        }
31
32
        return new RecursiveIteratorIterator(
33
            new RecursiveDirectoryIterator(
34
                $directoryPathReal,
35
                FilesystemIterator::CURRENT_AS_FILEINFO | FilesystemIterator::SKIP_DOTS
36
            ),
37
            RecursiveIteratorIterator::CHILD_FIRST
38
        );
39
    }
40
41
    /**
42
     * Return objects of all directories by provided path
43
     *
44
     * @param string $directoryPath Absolute or relative path
45
     *
46
     * @return \CallbackFilterIterator
47
     * @throws \Compolomus\FSHelper\Exceptions\PathNotFoundException
48
     */
49
    public static function getDirectories(string $directoryPath): CallbackFilterIterator
50
    {
51
        $all = self::getAll($directoryPath);
52
53
        return new CallbackFilterIterator($all, static fn($iterator) => $iterator->isDir());
54
    }
55
56
    /**
57
     * Return objects of all files by provided path
58
     *
59
     * @param string $directoryPath Absolute or relative path
60
     *
61
     * @return \CallbackFilterIterator
62
     * @throws \Compolomus\FSHelper\Exceptions\PathNotFoundException
63
     */
64
    public static function getFiles(string $directoryPath): CallbackFilterIterator
65
    {
66
        $all = self::getAll($directoryPath);
67
68
        return new CallbackFilterIterator($all, static fn($iterator) => $iterator->isFile());
69
    }
70
71
    /**
72
     * Get total size of all files by provided path (in bytes)
73
     *
74
     * @param string $directoryPath Absolute or relative path
75
     *
76
     * @return int
77
     * @throws \Compolomus\FSHelper\Exceptions\PathNotFoundException
78
     */
79
    public static function getFilesSize(string $directoryPath): int
80
    {
81
        $size = 0;
82
        foreach (self::getFiles($directoryPath) as $fileInfo) {
83
            $size += $fileInfo->getSize();
84
        }
85
86
        return $size;
87
    }
88
89
    /**
90
     * Return meta information about files and directories
91
     *
92
     * @param string $directoryPath Absolute or relative path
93
     *
94
     * @return array
95
     * @throws \Compolomus\FSHelper\Exceptions\PathNotFoundException
96
     */
97
    public static function getInfoDirectory(string $directoryPath): array
98
    {
99
        return [
100
            'directories' => iterator_count(self::getDirectories($directoryPath)),
101
            'files'       => iterator_count(self::getFiles($directoryPath)),
102
            'size'        => self::getFilesSize($directoryPath),
103
        ];
104
    }
105
}
106