Passed
Push — dependabot/npm_and_yarn/@vue/t... ( 1a0b23...9776fc )
by
unknown
84:45 queued 64:29
created

Viewable   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 98
Duplicated Lines 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
wmc 18
eloc 33
c 2
b 1
f 0
dl 0
loc 98
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A viewData() 0 20 4
A viewKey() 0 15 5
A renderView() 0 10 3
A setViewParent() 0 5 1
A viewPath() 0 3 1
A renderRawView() 0 13 4
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
use Thinktomorrow\Chief\Management\ManagedModel;
11
12
trait Viewable
13
{
14
    protected $viewParent;
15
16
    public function renderView(): string
17
    {
18
        $renderedView = $this->renderRawView();
19
20
        // In case of snippets, we inject these after the initial view rendering
21
        if (method_exists($this, 'shouldParseWithSnippets') && $this->shouldParseWithSnippets($renderedView)) {
22
            $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

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

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