GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( f617e2...cdd1bd )
by Pascal
01:23
created

BladeOnDemandRenderer::theme()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace ProtoneMedia\BladeOnDemand;
4
5
use Illuminate\Mail\Markdown;
6
use Illuminate\Support\Str;
7
use Illuminate\View\Factory as ViewFactory;
8
use TijsVerkoyen\CssToInlineStyles\CssToInlineStyles;
9
10
class BladeOnDemandRenderer
11
{
12
    /**
13
     * @var \Illuminate\View\Factory
14
     */
15
    private $viewFactory;
16
17
    /**
18
     * @var \Illuminate\Mail\Markdown
19
     */
20
    private $markdown;
21
22
    /**
23
     * @var \TijsVerkoyen\CssToInlineStyles\CssToInlineStyles
24
     */
25
    private $cssInliner;
26
27
    /**
28
     * Wether to fill the missing variables from the template.
29
     *
30
     * @var boolean
31
     */
32
    private $fillMissingVariables = false;
33
34
    /**
35
     * The current theme being used when generating emails.
36
     *
37
     * @var string
38
     */
39
    private $theme = 'default';
40
41
    public function __construct(ViewFactory $viewFactory, Markdown $markdown, CssToInlineStyles $cssInliner)
42
    {
43
        $this->viewFactory = $viewFactory;
44
        $this->markdown    = $markdown;
45
        $this->cssInliner  = $cssInliner;
46
47
        $this->theme = config('mail.markdown.theme', 'default');
48
    }
49
50
    /**
51
     * Fills the missing variables in the template
52
     *
53
     * @param callable $callback
54
     * @return $this
55
     */
56
    public function fillMissingVariables(callable $callback = null)
57
    {
58
        $this->fillMissingVariables = $callback ?: true;
0 ignored issues
show
Documentation Bug introduced by
It seems like $callback ?: true can also be of type callable. However, the property $fillMissingVariables is declared as type boolean. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
59
60
        return $this;
61
    }
62
63
    /**
64
     * Set the default theme to be used.
65
     *
66
     * @param  string  $theme
67
     * @return $this
68
     */
69
    public function theme($theme)
70
    {
71
        $this->theme = $theme;
72
73
        return $this;
74
    }
75
76
    /**
77
     * Renders the content with the given data.
78
     *
79
     * @param string $contents
80
     * @param array $data
81
     * @return string
82
     */
83
    public function render(string $contents, array $data = []): string
84
    {
85
        if ($this->fillMissingVariables) {
86
            $data = $this->addMissingVariables($contents, $data);
87
        }
88
89
        file_put_contents(
90
            $path = tempnam(sys_get_temp_dir(), 'blade-on-demand') . '.blade.php',
91
            $contents
92
        );
93
94
        $this->viewFactory->flushFinderCache();
95
96
        return tap($this->viewFactory->file($path, $data)->render(), function () use ($path) {
97
            unlink($path);
98
99
            $this->fillMissingVariables = false;
100
            $this->theme = config('mail.markdown.theme', 'default');
101
        });
102
    }
103
104
    /**
105
     * Finds all missing variables.
106
     * Source: https://stackoverflow.com/a/19563063
107
     *
108
     * @param string $contents
109
     * @param array $data
110
     * @return array
111
     */
112
    public function getMissingVariables(string $contents, array $data = []): array
0 ignored issues
show
Unused Code introduced by
The parameter $data is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
113
    {
114
        $pattern = '/\$([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)/';
115
116
        preg_match_all($pattern, $contents, $matches);
117
118
        return $matches[1] ?? [];
119
    }
120
121
    /**
122
     * Makes sure each variable is present in the data array.
123
     *
124
     * @param string $contents
125
     * @param array $data
126
     * @return array
127
     */
128
    private function addMissingVariables(string $contents, array $data = []): array
129
    {
130
        foreach (static::getMissingVariables($contents, $data) as $variable) {
131
            if (array_key_exists($variable, $data)) {
132
                continue;
133
            }
134
135
            if (!is_callable($this->fillMissingVariables)) {
136
                $data[$variable] = $variable;
137
                continue;
138
            }
139
140
            $data[$variable] = call_user_func_array($this->fillMissingVariables, [$variable]);
141
        }
142
143
        return $data;
144
    }
145
146
    /**
147
     * Renders the markdown content to a HTML mail.
148
     *
149
     * @param string $contents
150
     * @param array $data
151
     * @return string
152
     */
153
    public function renderMarkdownMailToHtml(string $contents, array $data = []): string
154
    {
155
        $this->viewFactory->replaceNamespace('mail', $this->markdown->htmlComponentPaths());
156
157
        $theme = Str::contains($this->theme, '::')
158
            ? $this->theme
159
            : 'mail::themes.' . $this->theme;
160
161
        $rendered = $this->render($contents, $data);
162
163
        return $this->cssInliner->convert(
164
            $rendered,
165
            $this->viewFactory->make($theme)->render()
166
        );
167
    }
168
169
    /**
170
     * Renders the markdown content to a Text mail.
171
     *
172
     * @param string $contents
173
     * @param array $data
174
     * @return string
175
     */
176
    public function renderMarkdownMailToText(string $contents, array $data = []): string
177
    {
178
        $this->viewFactory->replaceNamespace('mail', $this->markdown->textComponentPaths());
179
180
        $rendered = $this->render($contents, $data);
181
182
        return html_entity_decode(preg_replace("/[\r\n]{2,}/", "\n\n", $rendered), ENT_QUOTES, 'UTF-8');
183
    }
184
185
    /**
186
     * Parses the markdown content.
187
     *
188
     * @param string $contents
189
     * @param array $data
190
     * @return string
191
     */
192
    public function parseMarkdownMail(string $contents, array $data = []): string
193
    {
194
        return $this->markdown->parse($this->renderMarkdownMailToText($contents, $data));
195
    }
196
}
197