Completed
Push — 0.3 ( da43ab...625b56 )
by Ben
96:17 queued 52:29
created

ViewPath::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
c 1
b 0
f 0
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 10
cc 1
nc 1
nop 3
crap 1
1
<?php
2
3
namespace Thinktomorrow\Chief\Concerns\Viewable;
4
5
use Thinktomorrow\Chief\Relations\ActsAsParent;
6
use Thinktomorrow\Chief\Sets\Set;
7
8
class ViewPath
9
{
10
    /** @var ViewableContract */
11
    private $viewable;
12
13
    /** @var ActsAsParent */
14
    private $parent;
15
16
    /** @var string */
17
    private $viewBasePath;
18
19 12
    private function __construct(ViewableContract $viewable, ActsAsParent $parent = null, string $viewBasePath = null)
20
    {
21 12
        $this->viewable = $viewable;
22 12
        $this->parent = $parent;
23 12
        $this->viewBasePath = $viewBasePath;
24 12
    }
25
26 12
    public static function make(ViewableContract $viewable, ActsAsParent $parent = null, string $viewBasePath = null)
27
    {
28 12
        return new static($viewable, $parent, $viewBasePath);
29
    }
30
31
    /**
32
     * @return string
33
     * @throws NotFoundView
34
     */
35 12
    public function get(): string
36
    {
37 12
        $basePath = $this->viewBasePath ? $this->viewBasePath . '.' : '';
38 12
        $guessedParentViewName = $this->parent ? $this->parent->viewKey() : '';
0 ignored issues
show
Bug introduced by
The method viewKey() does not exist on Thinktomorrow\Chief\Relations\ActsAsParent. Since it exists in all sub-types, consider adding an abstract or default implementation to Thinktomorrow\Chief\Relations\ActsAsParent. ( Ignorable by Annotation )

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

38
        $guessedParentViewName = $this->parent ? $this->parent->/** @scrutinizer ignore-call */ viewKey() : '';
Loading history...
39 12
        $guessedViewName = $this->viewable->viewKey();
40
41
        $viewPaths = [
42 12
            $basePath.$guessedParentViewName.'.'.$guessedViewName,
43 12
            $basePath.$guessedViewName,
44 12
            $basePath.$guessedViewName.'.show',
45 12
            $basePath.'show',
46
        ];
47
48
        // In case the collection set is made out of pages, we'll also allow to use the
49
        // generic collection page view for these sets as well as a fallback view
50 12
        if ($this->viewable instanceof Set && $this->viewable->first() instanceof ViewableContract) {
51 6
            $viewPaths[] = $basePath.$guessedParentViewName.'.'.$this->viewable->first()->viewKey();
52 6
            $viewPaths[] = $basePath.$this->viewable->first()->viewKey();
53
        }
54
55 12
        foreach ($viewPaths as $viewPath) {
56 12
            if (! view()->exists($viewPath)) {
57 12
                continue;
58
            }
59
60
            return $viewPath;
61
        }
62
63 12
        throw new NotFoundView('Viewfile not found for ['.get_class($this->viewable).']. Try adding one with path: '.$basePath.$guessedViewName);
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 145 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
64
    }
65
}
66