Completed
Push — master ( 22c257...e714e6 )
by Gabriel
13:14 queued 10:09
created

HasViewFinder::prependPath()   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 0
Metric Value
cc 1
eloc 1
nc 1
nop 2
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Nip\View\ViewFinder;
4
5
use InvalidArgumentException;
6
7
/**
8
 * Trait HasViewFinder
9
 * @package Nip\View\ViewFinder
10
 */
11
trait HasViewFinder
12
{
13
    /**
14
     * The view finder implementation.
15
     *
16
     * @var ViewFinderInterface
17
     */
18
    protected $finder = null;
19
20
21
    /**
22
     * Adds a path where templates are stored.
23
     *
24
     * @param string $path A path where to look for templates
25
     * @param string $namespace A path namespace
26
     *
27
     * @return void
28
     */
29 1
    public function addPath($path, $namespace = ViewFinderInterface::MAIN_NAMESPACE)
30
    {
31 1
        $this->getFinder()->addPath($path, $namespace);
32 1
    }
33
34
    /**
35
     * Prepends a path where templates are stored.
36
     *
37
     * @param string $path A path where to look for templates
38
     * @param string $namespace A path namespace
39
     * @return void
40
     */
41 1
    public function prependPath($path, $namespace = ViewFinderInterface::MAIN_NAMESPACE)
42
    {
43 1
        $this->getFinder()->prependPath($path, $namespace);
44 1
    }
45
46
    /**
47
     * @param $view
48
     * @return bool
49
     */
50
    public function existPath($view)
51
    {
52
        try {
53
            $this->getFinder()->find($view);
54
        } catch (InvalidArgumentException $exception) {
55
            return false;
56
        }
57
        return true;
58
    }
59
60
    /**
61
     * Get the view finder instance.
62
     *
63
     * @return ViewFinderInterface|ViewFinder
64
     */
65 6
    public function getFinder()
66
    {
67 6
        if ($this->finder === null) {
68 6
            $this->initFinder();
69
        }
70 6
        return $this->finder;
71
    }
72
73 6
    public function initFinder()
74
    {
75 6
        $finder = new ViewFinder();
76 6
        $finder->addPath($this->getBasePath());
0 ignored issues
show
Bug introduced by
It seems like getBasePath() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

76
        $finder->addPath($this->/** @scrutinizer ignore-call */ getBasePath());
Loading history...
77 6
        $this->setFinder($finder);
78 6
    }
79
80
    /**
81
     * Set the view finder instance.
82
     *
83
     * @param  ViewFinderInterface $finder
84
     * @return void
85
     */
86 6
    public function setFinder(ViewFinderInterface $finder)
87
    {
88 6
        $this->finder = $finder;
89 6
    }
90
}
91