Completed
Push — master ( 29472a...0fa5c2 )
by Richan
04:47 queued 03:37
created

HtmlFileFinder::__construct()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
cc 4
nc 4
nop 1
1
<?php
2
3
namespace Suitmedia\LighthouseAudit;
4
5
use LogicException;
6
7
class HtmlFileFinder
8
{
9
    /**
10
     * Path to find HTML files.
11
     *
12
     * @var string
13
     */
14
    protected $path;
15
16
    /**
17
     * HtmlFileFinder constructor.
18
     *
19
     * @param mixed $path
20
     */
21
    public function __construct($path)
22
    {
23
        $path = is_string($path) ? realpath($path) : realpath('.');
24
25
        if ($path === false || !is_dir($path)) {
26
            throw new LogicException('The given path is not a directory or directory is not exists.');
27
        }
28
29
        $this->path = $path;
30
    }
31
32
    /**
33
     * Get the HTML files.
34
     *
35
     * @return array
36
     */
37
    public function getFiles() :array
38
    {
39
        $dir = opendir($this->path);
40
        $files = [];
41
42
        if ($dir === false) {
43
            return $files;
44
        }
45
46
        while (($file = readdir($dir)) !== false) {
47
            if (is_dir($this->path . $file) || !$this->isHtmlFile($file)) {
48
                continue;
49
            }
50
51
            $files[] = $file;
52
        }
53
54
        closedir($dir);
55
56
        return $files;
57
    }
58
59
    /**
60
     * Check if the given file is an HTML file.
61
     *
62
     * @param mixed $path
63
     * @return bool
64
     */
65
    protected function isHtmlFile($path) :bool
66
    {
67
        if (!is_string($path)) {
68
            return false;
69
        }
70
        $extension = pathinfo($path, PATHINFO_EXTENSION);
71
72
        return in_array($extension, ['php', 'html']);
73
    }
74
}
75