CreateMailable::commandParameters()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 9
rs 10
1
<?php
2
3
namespace Qoraiche\MailEclipse\Actions;
4
5
use Illuminate\Support\Facades\Artisan;
6
use Illuminate\Support\Str;
7
use Qoraiche\MailEclipse\Facades\MailEclipse;
8
9
class CreateMailable
10
{
11
    /**
12
     * Handle Creating a new mailable.
13
     *
14
     * @param  array  $parameters
15
     * @return string[]
16
     */
17
    public function handle(array $parameters)
18
    {
19
        $parameters = $this->commandParameters($parameters);
20
21
        if (strtolower($parameters['name']) === 'mailable') {
22
            return [
23
                'status' => 'error',
24
                'message' => 'You cannot use this name',
25
            ];
26
        }
27
28
        if (! MailEclipse::getMailable('name', $parameters['name'])->isEmpty() && ! isset($parameters['force'])) {
29
            return [
30
                'status' => 'error',
31
                'message' => 'This mailable name already exists. names should be unique! to override it, enable "force" option.',
32
            ];
33
        }
34
35
        $exitCode = Artisan::call('make:mail', $parameters);
36
37
        if ($exitCode > -1) {
38
            return [
39
                'status' => 'ok',
40
                'message' => 'Mailable Created<br> <small>Reloading...<small>',
41
            ];
42
        }
43
44
        return [
45
            'status' => 'error',
46
            'message' => 'Unable to create the Mailable, please double check the log file',
47
        ];
48
    }
49
50
    /**
51
     * Get the parameters for the artisan command.
52
     *
53
     * @param  array  $parameters
54
     * @return array
55
     */
56
    protected function commandParameters(array $parameters)
57
    {
58
        $parameters['name'] = ucwords(Str::camel(preg_replace('/\s+/', '_', $parameters['name'])));
59
60
        if (isset($parameters['force'])) {
61
            $parameters['force'] = true;
62
        }
63
64
        return $parameters;
65
    }
66
}
67