View::__construct()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

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 2
nc 2
nop 3
crap 2
1
<?php
2
declare(strict_types=1);
3
4
namespace Shoot\Shoot;
5
6
use InvalidArgumentException;
7
use Throwable;
8
9
/**
10
 * A view represents a template with its presentation model. You'll never have to construct a view manually, as this is
11
 * taken care of by Shoot's Twig extension. You'll have access to them in any middleware you implement.
12
 */
13
final class View
14
{
15
    /** @var callable */
16
    private $callback;
17
18
    /** @var string */
19
    private $name;
20
21
    /** @var PresentationModel */
22
    private $presentationModel;
23
24
    /** @var Throwable|null */
25
    private $suppressedException;
26
27
    /**
28
     * @param string            $name
29
     * @param PresentationModel $presentationModel
30
     * @param callable          $callback
31
     *
32
     * @throws InvalidArgumentException
33
     *
34
     * @internal
35
     */
36 25
    public function __construct(string $name, PresentationModel $presentationModel, callable $callback)
37
    {
38 25
        $this->name = $name !== '' ? $name : 'unknown template';
39 25
        $this->presentationModel = $presentationModel;
40 25
        $this->callback = $callback;
41 25
    }
42
43
    /**
44
     * @return void
45
     *
46
     * @internal
47
     */
48 15
    public function render(): void
49
    {
50 15
        call_user_func($this->callback, $this->presentationModel->getVariables());
51 11
    }
52
53
    /**
54
     * Returns the name of the view.
55
     *
56
     * @return string
57
     */
58 4
    public function getName(): string
59
    {
60 4
        return $this->name;
61
    }
62
63
    /**
64
     * Returns the presentation model.
65
     *
66
     * @return PresentationModel
67
     */
68 16
    public function getPresentationModel(): PresentationModel
69
    {
70 16
        return $this->presentationModel;
71
    }
72
73
    /**
74
     * Returns a clone of this view with the new presentation model set.
75
     *
76
     * @param PresentationModel $presentationModel
77
     *
78
     * @return View
79
     */
80 9
    public function withPresentationModel(PresentationModel $presentationModel): self
81
    {
82 9
        $new = clone $this;
83 9
        $new->presentationModel = $presentationModel;
84
85 9
        return $new;
86
    }
87
88
    /**
89
     * Returns the exception that was suppressed while rendering this view.
90
     *
91
     * @return Throwable|null
92
     */
93 1
    public function getSuppressedException(): ?Throwable
94
    {
95 1
        return $this->suppressedException;
96
    }
97
98
    /**
99
     * Returns whether an exception was suppressed while rendering this view.
100
     *
101
     * @return bool
102
     */
103 3
    public function hasSuppressedException(): bool
104
    {
105 3
        return $this->suppressedException !== null;
106
    }
107
108
    /**
109
     * Returns a clone of this view with the suppressed exception set.
110
     *
111
     * @param Throwable|null $exception
112
     *
113
     * @return View
114
     */
115 3
    public function withSuppressedException(?Throwable $exception): self
116
    {
117 3
        $new = clone $this;
118 3
        $new->suppressedException = $exception;
119
120 3
        return $new;
121
    }
122
}
123