Art   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 152
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 97.83%

Importance

Changes 0
Metric Value
wmc 15
lcom 1
cbo 2
dl 0
loc 152
ccs 45
cts 46
cp 0.9783
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A settings() 0 4 1
A importSettingArt() 0 6 2
A addDir() 0 11 1
A artDir() 0 4 1
A artFile() 0 15 3
B fileSearch() 0 29 6
A parse() 0 8 1
1
<?php
2
3
namespace League\CLImate\TerminalObject\Helper;
4
5
use League\CLImate\Exceptions\UnexpectedValueException;
6
7
trait Art
8
{
9
    /**
10
     * The directories we should be looking for art in
11
     *
12
     * @var array $art_dirs
13
     */
14
    protected $art_dirs = [];
15
16
    /**
17
     * The default art if we can't find what the user requested
18
     *
19
     * @var string $default_art
20
     */
21
    protected $default_art = '404';
22
23
    /**
24
     * The art requested by the user
25
     *
26
     * @var string $art
27
     */
28
    protected $art = '';
29
30
    /**
31
     * Specify which settings Draw needs to import
32
     *
33 80
     * @return array
34
     */
35 80
    public function settings()
36
    {
37
        return ['Art'];
38
    }
39
40
    /**
41
     * Import the Art setting (any directories the user added)
42
     *
43 44
     * @param \League\CLImate\Settings\Art $setting
44
     */
45 44
    public function importSettingArt($setting)
46 44
    {
47 44
        foreach ($setting->dirs as $dir) {
48 44
            $this->addDir($dir);
49
        }
50
    }
51
52
    /**
53
     * Add a directory to search for art in
54
     *
55 80
     * @param string $dir
56
     */
57
    protected function addDir($dir)
58
    {
59 80
        // Add any additional directories to the top of the array
60
        // so that the user can override art
61
        array_unshift($this->art_dirs, rtrim($dir, '/'));
62 80
63 80
        // Keep the array clean
64 80
        $this->art_dirs = array_unique($this->art_dirs);
65 80
        $this->art_dirs = array_filter($this->art_dirs);
66
        $this->art_dirs = array_values($this->art_dirs);
67
    }
68
69
    /**
70
     * Find a valid art path
71
     *
72
     * @param string $art
73
     *
74 4
     * @return array
75
     */
76 4
    protected function artDir($art)
77
    {
78
        return $this->fileSearch($art, '/*.*');
79
    }
80
81
    /**
82
     * Find a valid art path
83
     *
84
     * @param string $art
85
     *
86 76
     * @return string
87
     */
88 76
    protected function artFile($art)
89
    {
90 76
        $files = $this->fileSearch($art, '.*');
91 8
92 8
        if (count($files) === 0) {
93 8
            $this->addDir(__DIR__ . '/../../ASCII');
94
            $files = $this->fileSearch($this->default_art, '.*');
95 76
        }
96
97
        if (count($files) === 0) {
98
            throw new UnexpectedValueException("Unable to find an art file with the name '{$art}'");
99 76
        }
100
101
        return reset($files);
102
    }
103
104
    /**
105
     * Find a set of files in the current art directories
106
     * based on a pattern
107
     *
108
     * @param string $art
109
     * @param string $pattern
110
     *
111 80
     * @return array
112
     */
113 80
    protected function fileSearch($art, $pattern)
114 80
    {
115
        foreach ($this->art_dirs as $dir) {
116 80
            $directory_iterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($dir));
117 80
118
            $paths = [];
119 80
            $regex = '~' . preg_quote($art) . $pattern . '~';
120 80
121 80
            foreach ($directory_iterator as $file) {
122
                if ($file->isDir()) {
123
                    continue;
124
                }
125 80
126 80
                // Look for anything that has the $art filename
127 80
                if (preg_match($regex, $file)) {
128 80
                    $paths[] = $file->getPathname();
129
                }
130 80
            }
131
132
            asort($paths);
133 80
134 80
            // If we've got one, no need to look any further
135
            if (!empty($paths)) {
136 8
                return $paths;
137
            }
138 8
        }
139
140
        return [];
141
    }
142
143
    /**
144
     * Parse the contents of the file and return each line
145
     *
146
     * @param string $path
147
     *
148 80
     * @return array
149
     */
150 80
    protected function parse($path)
151 80
    {
152 80
        $output = file_get_contents($path);
153
        $output = explode("\n", $output);
154 80
        $output = array_map('rtrim', $output);
155
156
        return $output;
157
    }
158
}
159