Test Failed
Pull Request — main (#20)
by Michael
04:15 queued 52s
created

Email::validationRules()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
ccs 1
cts 1
cp 1
rs 10
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
/**
4
 * This file is part of michael-rubel/laravel-value-objects. (https://github.com/michael-rubel/laravel-value-objects)
5
 *
6
 * @link https://github.com/michael-rubel/laravel-value-objects for the canonical source repository
7
 * @copyright Copyright (c) 2022 Michael Rubél. (https://github.com/michael-rubel/)
8
 * @license https://raw.githubusercontent.com/michael-rubel/laravel-value-objects/main/LICENSE.md MIT
9
 */
10
11
namespace MichaelRubel\ValueObjects\Collection\Complex;
12
13
use Illuminate\Support\Collection;
14
use Illuminate\Support\Facades\Validator;
15
use Illuminate\Support\Stringable;
16
use Illuminate\Validation\ValidationException;
17
use MichaelRubel\ValueObjects\Collection\Primitive\Text;
18
19
/**
20
 * "Email" object that represents the email address.
21
 *
22
 * @author Michael Rubél <[email protected]>
23
 *
24
 * @template TKey of array-key
25
 * @template TValue
26
 *
27
 * @method static static make(string|Stringable $value)
28
 * @method static static from(string|Stringable $value)
29
 * @method static static makeOrNull(string|Stringable|null $value)
30
 *
31
 * @extends Text<TKey, TValue>
32
 */
33
class Email extends Text
34
{
35
    /**
36
     * @var Collection<int, string>
37
     */
38
    protected Collection $split;
39
40
    /**
41
     * Create a new instance of the value object.
42
     *
43
     * @param  string|Stringable  $value
44
     */
45
    public function __construct(string|Stringable $value)
46
    {
47 15
        parent::__construct($value);
48
49 15
        $this->validate();
50
        $this->split();
51 11
    }
52 11
53
    /**
54
     * @return string
55
     */
56
    public function username(): string
57
    {
58 2
        return (string) $this->split->first();
59
    }
60 2
61
    /**
62
     * @return string
63
     */
64
    public function domain(): string
65
    {
66 2
        return (string) $this->split->last();
67
    }
68 2
69
    /**
70
     * Get an array representation of the value object.
71
     *
72
     * @return array
73
     */
74
    public function toArray(): array
75
    {
76 1
        return [
77
            'email'    => $this->value(),
78
            'username' => $this->username(),
79 1
            'domain'   => $this->domain(),
80 1
        ];
81 1
    }
82
83
    /**
84
     * Validate the email.
85
     *
86
     * @return void
87
     */
88
    protected function validate(): void
89
    {
90 15
        $validator = Validator::make(
91
            ['email' => $this->value()],
92 15
            ['email' => $this->validationRules()],
93
        );
94 15
95 4
        if ($validator->fails()) {
96
            throw ValidationException::withMessages([__('Your email is invalid.')]);
97
        }
98
    }
99
100
    /**
101
     * Define the rules for email validation.
102
     *
103
     * @return array
104 15
     */
105
    protected function validationRules(): array
106 15
    {
107
        return ['required', 'email:', implode(',', $this->validationParameters())];
0 ignored issues
show
Deprecated Code introduced by
The function MichaelRubel\ValueObject...:validationParameters() has been deprecated. ( Ignorable by Annotation )

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

107
        return ['required', 'email:', implode(',', /** @scrutinizer ignore-deprecated */ $this->validationParameters())];
Loading history...
108
    }
109
110
    /**
111
     * Define how you want to validate the email.
112
     *
113
     * @deprecated
114 11
     *
115
     * @return array
116 11
     */
117
    protected function validationParameters(): array
118
    {
119
        return ['filter', 'spoof'];
120
    }
121
122
    /**
123
     * Split the value by at symbol.
124
     *
125
     * @return void
126
     */
127
    protected function split(): void
128
    {
129
        $this->split = str($this->value())->split('/@/');
130
    }
131
}
132