Passed
Push — master ( b6b0b7...148695 )
by Gabriel
03:18
created

ViewPathDetector::keyForController()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 1
c 1
b 0
f 1
dl 0
loc 3
rs 10
ccs 2
cts 2
cp 1
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Nip\Controllers\View;
4
5
use Nip\Controllers\Utility\Path;
6
use Nip\Utility\Traits\SingletonTrait;
7
8
/**
9
 * Class ViewPathDetector
10
 * @package Nip\Controllers\View
11
 */
12
class ViewPathDetector
13
{
14
    use SingletonTrait;
15
16
    /**
17
     * @var array
18
     */
19
    protected $paths = [];
20
21
    /**
22
     * ViewPathDetector constructor.
23
     */
24 1
    public function __construct()
25
    {
26 1
        $this->initFromCache();
27 1
    }
28
29
    /**
30
     * @param $controller
31
     * @return bool
32
     */
33 4
    public static function for($controller)
34
    {
35 4
        return static::instance()->detectForController($controller);
36
    }
37
38
    /**
39
     * @param $controller
40
     * @return bool
41
     */
42 4
    protected function detectForController($controller)
43
    {
44 4
        $key = $this->keyForController($controller);
45 4
        if (!isset($this->paths[$key])) {
46 3
            $this->paths[$key] = $this->generateForController($controller);
47
        }
48
49 4
        return $this->paths[$key];
50
    }
51
52
    /**
53
     * @param $controller
54
     * @return false|string
55
     */
56 4
    protected function keyForController($controller)
57
    {
58 4
        return get_class($controller);
59
    }
60
61
    /**
62
     * @param $controller
63
     * @return bool|string
64
     */
65 3
    protected function generateForController($controller)
66
    {
67 3
        if (method_exists($controller, 'generateViewPath')) {
68 2
            return $controller->generateViewPath();
69
        }
70
71 1
        if (defined('MODULES_PATH')) {
72
            $path = MODULES_PATH . request()->getModuleName() . '/views/';
0 ignored issues
show
Bug introduced by
The constant Nip\Controllers\View\MODULES_PATH was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
73
            if (is_dir($path)) {
74
                return $path;
75
            }
76
        }
77
78 1
        return $this->generateForControllerAutodetect($controller);
79
    }
80
81
    /**
82
     * @param $controller
83
     * @return bool|string
84
     */
85 1
    protected function generateForControllerAutodetect($controller)
86
    {
87 1
        $path = dirname(Path::basePath($controller));
0 ignored issues
show
Bug introduced by
It seems like Nip\Controllers\Utility\...::basePath($controller) can also be of type false; however, parameter $path of dirname() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

87
        $path = dirname(/** @scrutinizer ignore-type */ Path::basePath($controller));
Loading history...
88 1
        $path .= DIRECTORY_SEPARATOR . 'views';
89 1
        if (is_dir($path)) {
90 1
            return $path;
91
        }
92
        return false;
93
    }
94
95 1
    protected function initFromCache()
96
    {
97
//        $this->paths;
98 1
    }
99
}
100