BaseRouter::addExtension()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 1
1
<?php
2
3
namespace League\CLImate\TerminalObject\Router;
4
5
abstract class BaseRouter implements RouterInterface
6
{
7
    protected $extensions = [];
8
9
    /**
10
     * Add a custom extension to CLImate
11
     *
12
     * @param string $key
13
     * @param string $class
14
     */
15 28
    public function addExtension($key, $class)
16
    {
17 28
        $this->extensions[$key] = $class;
18 28
    }
19
20
    /**
21
     * Get the full path for the class based on the key
22
     *
23
     * @param string $class
24
     *
25
     * @return string
26
     */
27 580
    public function path($class)
28
    {
29 580
        return $this->getExtension($class) ?: $this->getPath($this->shortName($class));
30
    }
31
32
    /**
33
     * Determines if the requested class is a
34
     * valid terminal object class
35
     *
36
     * @param  string  $class
37
     *
38
     * @return boolean
39
     */
40 580
    public function exists($class)
41
    {
42 580
        $class = $this->path($class);
43
44 580
        return (is_object($class) || class_exists($class));
45
    }
46
47
    /**
48
     * Get the full path for the terminal object class
49
     *
50
     * @param  string $class
51
     *
52
     * @return string
53
     */
54 564
    protected function getPath($class)
55
    {
56 564
        return 'League\CLImate\TerminalObject\\' . $this->pathPrefix() . '\\' . $class;
57
    }
58
59
    /**
60
     * Get an extension by its key
61
     *
62
     * @param string $key
63
     *
64
     * @return string|false Full class path to extension
65
     */
66 580
    protected function getExtension($key)
67
    {
68 580
        if (array_key_exists($key, $this->extensions)) {
69 28
            return $this->extensions[$key];
70
        }
71
72 564
        return false;
73
    }
74
75
    /**
76
     * Get the class short name
77
     *
78
     * @param string $name
79
     *
80
     * @return string
81
     */
82 564
    protected function shortName($name)
83
    {
84 564
        $name = str_replace('_', ' ', $name);
85 564
        $name = ucwords($name);
86
87 564
        return str_replace(' ', '', $name);
88
    }
89
}
90