Completed
Push — master ( 9a32a7...c0ece2 )
by
unknown
14s queued 10s
created

MailablesController::createMailable()   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 1
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\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([]);
0 ignored issues
show
introduced by
The condition null !== $mailables is always true.
Loading history...
30
31
        return view(MailEclipse::$view_namespace.'::sections.mailables', compact('mailables'));
32
    }
33
34
    public function createMailable(Request $request)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed. ( Ignorable by Annotation )

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

34
    public function createMailable(/** @scrutinizer ignore-unused */ Request $request)

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

Loading history...
35
    {
36
        return view(MailEclipse::$view_namespace.'::createmailable');
37
    }
38
39
    public function generateMailable(Request $request)
40
    {
41
        return MailEclipse::generateMailable($request);
42
    }
43
44
    public function viewMailable($name)
45
    {
46
        $mailable = MailEclipse::getMailable('name', $name);
47
48
        if ($mailable->isEmpty()) {
49
            return redirect()->route('mailableList');
50
        }
51
52
        $resource = $mailable->first();
53
54
        return view(MailEclipse::$view_namespace.'::sections.view-mailable')->with(compact('resource'));
55
    }
56
57
    public function editMailable($name)
58
    {
59
        $templateData = MailEclipse::getMailableTemplateData($name);
60
61
        if (! $templateData) {
62
            return redirect()->route('viewMailable', ['name' => $name]);
63
        }
64
65
        return view(MailEclipse::$view_namespace.'::sections.edit-mailable-template', compact('templateData', 'name'));
66
    }
67
68
    public function templatePreviewError()
69
    {
70
        return view(MailEclipse::$view_namespace.'::previewerror');
71
    }
72
73
    public function parseTemplate(Request $request)
74
    {
75
        $template = $request->has('template') ? $request->template : false;
76
77
        $viewPath = $request->has('template') ? $request->viewpath : base64_decode($request->viewpath);
78
79
        // ref https://regexr.com/4dflu
80
        $bladeRenderable = preg_replace('/((?!{{.*?-)(&gt;)(?=.*?}}))/', '>', $request->markdown);
81
82
        if (MailEclipse::markdownedTemplateToView(true, $bladeRenderable, $viewPath, $template)) {
83
            return response()->json([
84
                'status' => 'ok',
85
            ]);
86
        }
87
88
        return response()->json([
89
            'status' => 'error',
90
        ]);
91
    }
92
93
    public function previewMarkdownView(Request $request)
94
    {
95
        return MailEclipse::previewMarkdownViewContent(false, $request->markdown, $request->name, false, $request->namespace);
96
    }
97
98
    public function previewMailable($name)
99
    {
100
        $mailable = MailEclipse::getMailable('name', $name);
101
102
        if ($mailable->isEmpty()) {
103
            return redirect()->route('mailableList');
104
        }
105
106
        $resource = $mailable->first();
107
108
        if (! is_null(MailEclipse::handleMailableViewDataArgs($resource['namespace']))) {
109
            // $instance = new $resource['namespace'];
110
            //
111
            $instance = MailEclipse::handleMailableViewDataArgs($resource['namespace']);
112
        } else {
113
            $instance = new $resource['namespace'];
114
        }
115
116
        if (collect($resource['data'])->isEmpty()) {
117
            return 'View not found';
118
        }
119
120
        $view = ! is_null($resource['markdown']) ? $resource['markdown'] : $resource['data']->view;
121
122
        if (view()->exists($view)) {
123
            try {
124
                $html = $instance;
125
126
                return $html->render();
0 ignored issues
show
Bug introduced by
The method render() does not exist on null. ( Ignorable by Annotation )

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

126
                return $html->/** @scrutinizer ignore-call */ render();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
127
            } catch (\ErrorException $e) {
128
                return view(MailEclipse::$view_namespace.'::previewerror', ['errorMessage' => $e->getMessage()]);
129
            }
130
        }
131
132
        return view(MailEclipse::$view_namespace.'::previewerror', ['errorMessage' => 'No template associated with this mailable.']);
133
    }
134
135
    public function delete(Request $request)
136
    {
137
        $mailableFile = config('maileclipse.mailables_dir').'/'.$request->mailablename.'.php';
138
139
        if (file_exists($mailableFile)) {
140
            unlink($mailableFile);
141
142
            return response()->json([
143
                'status' => 'ok',
144
            ]);
145
        }
146
147
        return response()->json([
148
            'status' => 'error',
149
        ]);
150
    }
151
}
152