Art   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 152
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 97.83%

Importance

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