MailablesPreviewController::mailable()   B
last analyzed

Complexity

Conditions 7
Paths 14

Size

Total Lines 35
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 19
c 1
b 0
f 0
nc 14
nop 1
dl 0
loc 35
rs 8.8333
1
<?php
2
3
namespace Qoraiche\MailEclipse\Http\Controllers;
4
5
use Illuminate\Http\Request;
6
use Illuminate\Routing\Controller;
7
use Illuminate\Support\Facades\App;
8
use Qoraiche\MailEclipse\Facades\MailEclipse;
9
use Qoraiche\MailEclipse\Http\Exceptions\PreviewErrorException;
10
11
class MailablesPreviewController extends Controller
12
{
13
    public function __construct()
14
    {
15
        abort_unless(
16
            App::environment(config('maileclipse.allowed_environments', ['local'])),
0 ignored issues
show
Bug introduced by
It seems like Illuminate\Support\Facad...ents', array('local'))) can also be of type string; however, parameter $boolean of abort_unless() does only seem to accept boolean, maybe add an additional type check? ( Ignorable by Annotation )

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

16
            /** @scrutinizer ignore-type */ App::environment(config('maileclipse.allowed_environments', ['local'])),
Loading history...
17
            403
18
        );
19
    }
20
21
    public function previewError()
22
    {
23
        return view(MailEclipse::VIEW_NAMESPACE.'::previewerror');
24
    }
25
26
    public function markdownView(Request $request)
27
    {
28
        return MailEclipse::previewMarkdownViewContent(false, $request->markdown, $request->name, false, $request->namespace);
29
    }
30
31
    public function mailable($name)
32
    {
33
        $mailable = MailEclipse::getMailable('name', $name);
34
35
        if ($mailable->isEmpty()) {
36
            return redirect()->route('mailableList');
37
        }
38
39
        $resource = $mailable->first();
40
41
        if (collect($resource['data'])->isEmpty()) {
42
            return 'View not found';
43
        }
44
45
        $instance = MailEclipse::handleMailableViewDataArgs($resource['namespace']);
46
47
        if (is_null($instance)) {
0 ignored issues
show
introduced by
The condition is_null($instance) is always false.
Loading history...
48
            $instance = new $resource['namespace'];
49
        }
50
51
        $view = ! is_null($resource['markdown'])
52
            ? $resource['markdown']
53
            : $resource['data']->view;
54
55
        if (view()->exists($view)) {
56
            try {
57
                $html = $instance;
58
59
                return $html->render();
60
            } catch (\ErrorException $e) {
61
                throw new PreviewErrorException($e);
62
            }
63
        }
64
65
        throw new PreviewErrorException(new \Exception('No template associated with this mailable.'));
66
    }
67
}
68