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

Viewable::renderView()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 10
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

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

21
            /** @scrutinizer ignore-call */ 
22
            $renderedView = $this->parseWithSnippets($renderedView);
Loading history...
22
        }
23
24 12
        return $renderedView;
25
    }
26
27 12
    private function renderRawView(): string
28
    {
29
        try {
30 12
            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...
31 12
        } catch (NotFoundView $e) {
32 12
            if (config('thinktomorrow.chief.strict')) {
33
                throw $e;
34
            }
35
        }
36
37
        // If no view has been created for this model, we try once again to fetch the content value if any. This will silently fail
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 131 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...
38
        // if no content value is present. We consider the 'content' attribute to be a default for our copy.
39 12
        return isset($this->content) ? (string) $this->content : '';
40
    }
41
42 12
    public function setViewParent(ActsAsParent $parent): ViewableContract
43
    {
44 12
        $this->viewParent = $parent;
45
46 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...
47
    }
48
49
    /**
50
     * This is the model's view identifier. This key is used to determine the full view
51
     * path of the model. By default this is based on the morphKey value of the model.
52
     *
53
     * @return string
54
     * @throws NotFoundViewKey
55
     */
56 12
    public function viewKey(): string
57
    {
58 12
        if (property_exists($this, 'viewKey') && isset($this->viewKey)) {
59 6
            return $this->viewKey;
60
        }
61
62 11
        if ($this instanceof MorphableContract) {
63 10
            return $this->morphKey();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->morphKey() could return the type null which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
64
        }
65
66 1
        if (config('thinktomorrow.chief.strict')) {
67
            throw new NotFoundViewKey('Missing view key. Please add a [viewKey] property to ' . get_class($this));
68
        };
69
70 1
        return '';
71
    }
72
73
    /**
74
     * This is the full path reference for this model's view file. This is relative to your main view folder
75
     * e.g. key 'articles.show'. A sensible default is set and determined based on the viewKey value.
76
     * But you are free to override this and change it to another value to fit your own logic.
77
     *
78
     * @return string
79
     * @throws NotFoundView
80
     */
81 12
    protected function viewPath(): string
82
    {
83 12
        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

83
        return ViewPath::make(/** @scrutinizer ignore-type */ $this, $this->viewParent, $this->baseViewPath ?? null)->get();
Loading history...
84
    }
85
86
    /**
87
     * @return array
88
     */
89
    private function viewData(): array
90
    {
91
        $viewData = [
92
            'model'  => $this,
93
            'parent' => $this->viewParent,
94
        ];
95
96
        // TODO: add className as variable next to generic model: e.g. Event class should pass variable $event.
97
98
        /** @deprecated since 0.3 in favor of generic 'model' variable */
99
        if ($this instanceof Page) {
100
            $viewData['page'] = $this;
101
        }
102
        if ($this instanceof Module) {
103
            $viewData['module'] = $this;
104
        }
105
        if ($this instanceof Set) {
106
            $viewData['collection'] = $this;
107
            $viewData['pages'] = $this;
108
        }
109
110
        return $viewData;
111
    }
112
}
113