Completed
Push — version-4-wip ( 86a2b9...5c3ca2 )
by Craig
03:49
created

Art::artFile()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3.0175

Importance

Changes 0
Metric Value
dl 0
loc 15
ccs 7
cts 8
cp 0.875
rs 9.7666
c 0
b 0
f 0
cc 3
nc 4
nop 1
crap 3.0175
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
     * @return array
34
     */
35 84
    public function settings()
36
    {
37 84
        return ['Art'];
38
    }
39
40
    /**
41
     * Import the Art setting (any directories the user added)
42
     *
43
     * @param \League\CLImate\Settings\Art $setting
44
     */
45 48
    public function importSettingArt($setting)
46
    {
47 48
        foreach ($setting->dirs as $dir) {
48 48
            $this->addDir($dir);
49
        }
50 48
    }
51
52
    /**
53
     * Add a directory to search for art in
54
     *
55
     * @param string $dir
56
     */
57 84
    protected function addDir($dir)
58
    {
59
        // Add any additional directories to the top of the array
60
        // so that the user can override art
61 84
        array_unshift($this->art_dirs, rtrim($dir, '/'));
62
63
        // Keep the array clean
64 84
        $this->art_dirs = array_unique($this->art_dirs);
65 84
        $this->art_dirs = array_filter($this->art_dirs);
66 84
        $this->art_dirs = array_values($this->art_dirs);
67 84
    }
68
69
    /**
70
     * Find a valid art path
71
     *
72
     * @param string $art
73
     *
74
     * @return array
75
     */
76 4
    protected function artDir($art)
77
    {
78 4
        return $this->fileSearch($art, '/*.*');
79
    }
80
81
    /**
82
     * Find a valid art path
83
     *
84
     * @param string $art
85
     *
86
     * @return string
87
     */
88 80
    protected function artFile($art)
89
    {
90 80
        $files = $this->fileSearch($art, '[^' . \DIRECTORY_SEPARATOR . ']*$');
91
92 76
        if (count($files) === 0) {
93 8
            $this->addDir(__DIR__ . '/../../ASCII');
94 8
            $files = $this->fileSearch($this->default_art, '.*');
95
        }
96
97 76
        if (count($files) === 0) {
98
            throw new UnexpectedValueException("Unable to find an art file with the name '{$art}'");
99
        }
100
101 76
        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
     * @return array
112
     */
113 84
    protected function fileSearch($art, $pattern)
114
    {
115 84
        foreach ($this->art_dirs as $dir) {
116 84
            $directory_iterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($dir));
117
118 80
            $paths = [];
119 80
            $regex = '~' . preg_quote($art) . $pattern . '~';
120
121 80
            foreach ($directory_iterator as $file) {
122 80
                if ($file->isDir()) {
123 80
                    continue;
124
                }
125
126
                // Look for anything that has the $art filename
127 80
                if (preg_match($regex, $file)) {
128 80
                    $paths[] = $file->getPathname();
129
                }
130
            }
131
132 80
            asort($paths);
133
134
            // If we've got one, no need to look any further
135 80
            if (!empty($paths)) {
136 80
                return $paths;
137
            }
138
        }
139
140 8
        return [];
141
    }
142
143
    /**
144
     * Parse the contents of the file and return each line
145
     *
146
     * @param string $path
147
     *
148
     * @return array
149
     */
150 80
    protected function parse($path)
151
    {
152 80
        $output = file_get_contents($path);
153 80
        $output = explode("\n", $output);
154 80
        $output = array_map('rtrim', $output);
155
156 80
        return $output;
157
    }
158
}
159