MailablesController::delete()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 7
nc 2
nop 1
dl 0
loc 14
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
            'Environment Not Allowed'
18
      );
19
    }
20
21
    public function toMailablesList()
22
    {
23
        return redirect()->route('mailableList');
24
    }
25
26
    public function index()
27
    {
28
        $mailables = MailEclipse::getMailables();
29
30
        $mailables = (null !== $mailables) ? $mailables->sortBy('name') : collect([]);
31
32
        return view(MailEclipse::VIEW_NAMESPACE.'::sections.mailables', compact('mailables'));
33
    }
34
35
    public function generateMailable(Request $request)
36
    {
37
        return MailEclipse::generateMailable($request);
38
    }
39
40
    public function viewMailable($name)
41
    {
42
        $mailable = MailEclipse::getMailable('name', $name);
43
44
        if ($mailable->isEmpty()) {
45
            return redirect()->route('mailableList');
46
        }
47
48
        $resource = $mailable->first();
49
50
        return view(MailEclipse::VIEW_NAMESPACE.'::sections.view-mailable')->with(compact('resource'));
51
    }
52
53
    public function editMailable($name)
54
    {
55
        $templateData = MailEclipse::getMailableTemplateData($name);
56
57
        if (! $templateData) {
58
            return redirect()->route('viewMailable', ['name' => $name]);
59
        }
60
61
        return view(MailEclipse::VIEW_NAMESPACE.'::sections.edit-mailable-template', compact('templateData', 'name'));
62
    }
63
64
    public function parseTemplate(Request $request)
65
    {
66
        $template = $request->has('template') ? $request->template : false;
67
68
        $viewPath = $request->has('template') ? $request->viewpath : base64_decode($request->viewpath);
69
70
        // ref https://regexr.com/4dflu
71
        $bladeRenderable = preg_replace('/((?!{{.*?-)(&gt;)(?=.*?}}))/', '>', $request->markdown);
72
73
        if (MailEclipse::markdownedTemplateToView(true, $bladeRenderable, $viewPath, $template)) {
74
            return response()->json([
75
                'status' => 'ok',
76
            ]);
77
        }
78
79
        return response()->json([
80
            'status' => 'error',
81
        ]);
82
    }
83
84
    public function previewMarkdownView(Request $request)
85
    {
86
        return MailEclipse::previewMarkdownViewContent(false, $request->markdown, $request->name, false, $request->namespace);
87
    }
88
89
    public function previewMailable($name)
90
    {
91
        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

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