Completed
Push — master ( 0598e6...0379c1 )
by
unknown
13s queued 11s
created

MailablesController::templatePreviewError()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
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
10
class MailablesController extends Controller
11
{
12
    public function __construct()
13
    {
14
        abort_unless(
15
            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

15
            /** @scrutinizer ignore-type */ App::environment(config('maileclipse.allowed_environments', ['local'])),
Loading history...
16
            403
17
      );
18
    }
19
20
    public function toMailablesList()
21
    {
22
        return redirect()->route('mailableList');
23
    }
24
25
    public function index()
26
    {
27
        $mailables = MailEclipse::getMailables();
28
29
        $mailables = (null !== $mailables) ? $mailables->sortBy('name') : collect([]);
30
31
        return view(MailEclipse::VIEW_NAMESPACE.'::sections.mailables', compact('mailables'));
32
    }
33
34
    public function generateMailable(Request $request)
35
    {
36
        return MailEclipse::generateMailable($request);
37
    }
38
39
    public function viewMailable($name)
40
    {
41
        $mailable = MailEclipse::getMailable('name', $name);
42
43
        if ($mailable->isEmpty()) {
44
            return redirect()->route('mailableList');
45
        }
46
47
        $resource = $mailable->first();
48
49
        return view(MailEclipse::VIEW_NAMESPACE.'::sections.view-mailable')->with(compact('resource'));
50
    }
51
52
    public function editMailable($name)
53
    {
54
        $templateData = MailEclipse::getMailableTemplateData($name);
55
56
        if (! $templateData) {
57
            return redirect()->route('viewMailable', ['name' => $name]);
58
        }
59
60
        return view(MailEclipse::VIEW_NAMESPACE.'::sections.edit-mailable-template', compact('templateData', 'name'));
61
    }
62
63
    public function parseTemplate(Request $request)
64
    {
65
        $template = $request->has('template') ? $request->template : false;
66
67
        $viewPath = $request->has('template') ? $request->viewpath : base64_decode($request->viewpath);
68
69
        // ref https://regexr.com/4dflu
70
        $bladeRenderable = preg_replace('/((?!{{.*?-)(&gt;)(?=.*?}}))/', '>', $request->markdown);
71
72
        if (MailEclipse::markdownedTemplateToView(true, $bladeRenderable, $viewPath, $template)) {
73
            return response()->json([
74
                'status' => 'ok',
75
            ]);
76
        }
77
78
        return response()->json([
79
            'status' => 'error',
80
        ]);
81
    }
82
83
    public function previewMarkdownView(Request $request)
84
    {
85
        return MailEclipse::previewMarkdownViewContent(false, $request->markdown, $request->name, false, $request->namespace);
86
    }
87
88
    public function previewMailable($name)
89
    {
90
        return MailEclipse::renderMailable($name);
0 ignored issues
show
Bug introduced by
The method renderMailable() does not exist on Qoraiche\MailEclipse\Facades\MailEclipse. Since you implemented __callStatic, consider adding a @method annotation. ( Ignorable by Annotation )

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

90
        return MailEclipse::/** @scrutinizer ignore-call */ renderMailable($name);
Loading history...
91
    }
92
93
    public function delete(Request $request)
94
    {
95
        $mailableFile = config('maileclipse.mailables_dir').'/'.$request->mailablename.'.php';
96
97
        if (file_exists($mailableFile)) {
98
            unlink($mailableFile);
99
100
            return response()->json([
101
                'status' => 'ok',
102
            ]);
103
        }
104
105
        return response()->json([
106
            'status' => 'error',
107
        ]);
108
    }
109
110
    public function sendTest(Request $request)
111
    {
112
        $validatedData = $request->validate([
0 ignored issues
show
Unused Code introduced by
The assignment to $validatedData is dead and can be removed.
Loading history...
113
            'email' => 'email|nullable',
114
            'name' => 'string|required',
115
        ]);
116
117
        $email = $request->get('email') ?? config('maileclipse.test_mail');
118
119
        MailEclipse::sendTest($request->get('name'), $email);
120
    }
121
}
122