Passed
Push — 0.5 ( a6305a...082d3b )
by Ben
06:55
created

Viewable::viewPath()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
ccs 0
cts 2
cp 0
rs 10
cc 1
nc 1
nop 0
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Thinktomorrow\Chief\Concerns\Viewable;
6
7
use Thinktomorrow\Chief\Modules\Module;
8
use Thinktomorrow\Chief\Pages\Page;
9
use Thinktomorrow\Chief\Relations\ActsAsParent;
10
use Thinktomorrow\Chief\Sets\Set;
11
use Thinktomorrow\Chief\Management\ManagedModel;
12
13
trait Viewable
14
{
15
    protected $viewParent;
16
17 11
    public function renderView(): string
18
    {
19 11
        $renderedView = $this->renderRawView();
20
21
        // In case of snippets, we inject these after the initial view rendering
22 11
        if (method_exists($this, 'shouldParseWithSnippets') && $this->shouldParseWithSnippets($renderedView)) {
23 3
            $renderedView = $this->parseWithSnippets($renderedView);
0 ignored issues
show
Bug introduced by
It seems like parseWithSnippets() 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

23
            /** @scrutinizer ignore-call */ 
24
            $renderedView = $this->parseWithSnippets($renderedView);
Loading history...
24
        }
25
26 11
        return $renderedView;
27
    }
28
29 11
    private function renderRawView(): string
30
    {
31
        try {
32 11
            return view($this->viewPath(), $this->viewData())->render();
0 ignored issues
show
Bug Best Practice introduced by
The expression return view($this->viewP...->viewData())->render() could return the type array which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
33 11
        } catch (NotFoundView $e) {
34 11
            if (config('thinktomorrow.chief.strict')) {
35
                throw $e;
36
            }
37
        }
38
39
        // If no view has been created for this model, we try once again to fetch the content value if any. This will silently fail
40
        // if no content value is present. We consider the 'content' attribute to be a default for our copy.
41 11
        return isset($this->content) ? (string)$this->content : '';
42
    }
43
44 12
    public function setViewParent(ActsAsParent $parent): ViewableContract
45
    {
46 12
        $this->viewParent = $parent;
47
48 12
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type Thinktomorrow\Chief\Concerns\Viewable\Viewable which is incompatible with the type-hinted return Thinktomorrow\Chief\Conc...ewable\ViewableContract.
Loading history...
49
    }
50
51
    /**
52
     * This is the model's view identifier. This key is used to determine the full view
53
     * path of the model. By default this is based on the morphKey value of the model.
54
     *
55
     * @return string
56
     * @throws NotFoundViewKey
57
     */
58 11
    public function viewKey(): string
59
    {
60 11
        if (property_exists($this, 'viewKey') && isset($this->viewKey)) {
61 6
            return $this->viewKey;
62
        }
63
64 10
        if ($this instanceof ManagedModel) {
65 10
            return $this->managedModelKey();
66
        }
67
68
        if (config('thinktomorrow.chief.strict')) {
69
            throw new NotFoundViewKey('Missing view key. Please add a [viewKey] property to ' . get_class($this));
70
        }
71
72
        return '';
73
    }
74
75
    /**
76
     * Group identifier for a page set.
77
     *
78
     * @return string
79
     * @throws NotFoundViewKey
80
     */
81
    public function setKey(): string
82
    {
83 11
        return $this->viewKey();
84
    }
85 11
86
    /**
87
     * This is the full path reference for this model's view file. This is relative to your main view folder
88
     * e.g. key 'articles.show'. A sensible default is set and determined based on the viewKey value.
89
     * But you are free to override this and change it to another value to fit your own logic.
90
     *
91
     * @return string
92
     * @throws NotFoundView
93
     */
94
    protected function viewPath(): string
95
    {
96
        return ViewPath::make($this, $this->viewParent, $this->baseViewPath ?? null)->get();
0 ignored issues
show
Bug introduced by
$this of type Thinktomorrow\Chief\Concerns\Viewable\Viewable is incompatible with the type Thinktomorrow\Chief\Conc...ewable\ViewableContract expected by parameter $viewable of Thinktomorrow\Chief\Conc...ewable\ViewPath::make(). ( Ignorable by Annotation )

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

96
        return ViewPath::make(/** @scrutinizer ignore-type */ $this, $this->viewParent, $this->baseViewPath ?? null)->get();
Loading history...
97
    }
98
99
    /**
100
     * @return array
101
     */
102
    private function viewData(): array
103
    {
104
        $viewData = [
105
            'model'  => $this,
106
            'parent' => $this->viewParent,
107
        ];
108
109
        /** @deprecated since 0.3 in favor of generic 'model' variable */
110
        if ($this instanceof Page) {
111
            $viewData['page'] = $this;
112
        }
113
        if ($this instanceof Module) {
114
            $viewData['module'] = $this;
115
        }
116
        if ($this instanceof Set) {
117
            $viewData['collection'] = $this;
118
            $viewData['pages'] = $this;
119
        }
120
121
        return $viewData;
122
    }
123
}
124