Completed
Pull Request — master (#13)
by
unknown
07:49
created

SendTestMail   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 71
Duplicated Lines 9.86 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 3
dl 7
loc 71
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A handle() 0 14 1
A getValues() 0 20 3
A guardAgainstInvalidArguments() 0 15 3
A vaildateMailable() 7 12 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Spatie\MailableTest;
4
5
use Mail;
6
use Validator;
7
use InvalidArgumentException;
8
use Illuminate\Console\Command;
9
10
class SendTestMail extends Command
11
{
12
    protected $signature = 'mail:send-test {mailableClass} {recipient} {--values=}';
13
14
    protected $description = 'Send a test email';
15
16
    public function handle()
17
    {
18
        $this->guardAgainstInvalidArguments();
19
20
        $mailable = app(MailableFactory::class)->getInstance(
21
            $this->argument('mailableClass'),
22
            $this->argument('recipient'),
23
            $this->getValues()
24
        );
25
26
        Mail::send($mailable);
27
28
        $this->comment("Mailable `{$this->argument('mailableClass')}` sent to {$this->argument('recipient')}!");
29
    }
30
31
    protected function guardAgainstInvalidArguments()
32
    {
33
        $validator = Validator::make(
34
            ['email' => $this->argument('recipient')],
35
            ['email' => 'email']
36
        );
37
38
        if (! $validator->passes()) {
39
            throw new InvalidArgumentException("`{$this->argument('recipient')}` is not a valid e-mail address");
40
        }
41
42
        if (! $this->vaildateMailable($this->argument('mailableClass'))) {
43
            throw new InvalidArgumentException("Mailable `{$this->argument('mailableClass')}` does not exist.");
44
        }
45
    }
46
47
    protected function getValues(): array
48
    {
49
        if (! $this->option('values')) {
50
            return [];
51
        }
52
53
        $values = explode(',', $this->option('values'));
54
55
        return collect($values)
56
            ->mapWithKeys(function (string $value) {
57
                $values = explode(':', $value);
58
59
                if (count($values) != 2) {
60
                    throw new InvalidArgumentException("The given value for the option 'values' `{$this->option('values')}` is not valid.");
61
                }
62
63
                return [$values[0] => $values[1]];
64
            })
65
            ->toArray();
66
    }
67
68
    protected function vaildateMailable($mailableClass)
69
    {
70 View Code Duplication
        if (! class_exists($mailableClass)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
71
            $mailableClass = sprintf('%s\\%s', config('mailable-test.base_namespace'), $mailableClass);
72
73
            if (! class_exists($mailableClass)) {
74
                return false;
75
            }
76
        }
77
78
        return true;
79
    }
80
}
81