Passed
Pull Request — master (#130)
by
unknown
05:17
created

CreateMailable   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 22
c 1
b 0
f 0
dl 0
loc 58
rs 10
wmc 7

2 Methods

Rating   Name   Duplication   Size   Complexity  
A commandParameters() 0 9 2
A handle() 0 32 5
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
23
            return [
24
                'status' => 'error',
25
                'message' => 'You cannot use this name',
26
            ];
27
        }
28
29
        if (! MailEclipse::getMailable('name', $parameters['name'])->isEmpty() && !isset($parameters['force'])) {
30
31
            return [
32
                'status' => 'error',
33
                'message' => 'This mailable name already exists. names should be unique! to override it, enable "force" option.',
34
            ];
35
        }
36
37
        $exitCode = Artisan::call('make:mail', $parameters);
38
39
        if ($exitCode > -1) {
40
            return [
41
                'status' => 'ok',
42
                'message' => 'Mailable Created<br> <small>Reloading...<small>',
43
            ];
44
        }
45
46
        return [
47
            'status' => 'error',
48
            'message' => 'Unable to create the Mailable, please double check the log file',
49
        ];
50
    }
51
52
    /**
53
     * Get the parameters for the artisan command.
54
     *
55
     * @param  array $parameters
56
     * @return array
57
     */
58
    protected function commandParameters(array $parameters)
59
    {
60
        $parameters['name'] = ucwords(Str::camel(preg_replace('/\s+/', '_', $parameters['name'])));
61
62
        if (isset($parameters['force'])) {
63
            $parameters['force'] = true;
64
        }
65
66
        return $parameters;
67
    }
68
}
69