Passed
Pull Request — main (#20)
by
unknown
12:34
created

Email   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 5
Bugs 0 Features 0
Metric Value
wmc 4
eloc 18
c 5
b 0
f 0
dl 0
loc 86
ccs 21
cts 21
cp 1
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A hp$0 ➔ split() 0 3 1
A __construct() 0 6 1
validate() 0 10 ?
A hp$0 ➔ validationParameters() 0 3 1
A domain() 0 3 1
A hp$0 ➔ validate() 0 10 2
split() 0 3 ?
A toArray() 0 6 1
A username() 0 3 1
validationParameters() 0 3 ?
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\Stringable;
15
use Illuminate\Validation\Concerns\ValidatesAttributes;
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 15
    public function __construct(string|Stringable $value)
46
    {
47 15
        parent::__construct($value);
48
49 11
        $this->validate();
50 11
        $this->split();
51
    }
52
53
    /**
54
     * @return string
55
     */
56 2
    public function username(): string
57
    {
58 2
        return (string) $this->split->first();
59
    }
60
61
    /**
62
     * @return string
63
     */
64 2
    public function domain(): string
65
    {
66 2
        return (string) $this->split->last();
67
    }
68
69
    /**
70
     * Get an array representation of the value object.
71
     *
72
     * @return array
73
     */
74 1
    public function toArray(): array
75
    {
76
        return [
77 1
            'email'    => $this->value(),
78 1
            'username' => $this->username(),
79 1
            'domain'   => $this->domain(),
80
        ];
81
    }
82
83
    /**
84
     * Validate the email.
85
     *
86
     * @return void
87
     */
88 15
    protected function validate(): void
89
    {
90 15
        $toValidate = ['email', $this->value(), $this->validationParameters()];
91 15
        $validator = new class
92
        {
93
            use ValidatesAttributes;
0 ignored issues
show
introduced by
The trait Illuminate\Validation\Concerns\ValidatesAttributes requires some properties which are not provided by anonymous//src/Collection/Complex/Email.php$0: $data, $container, $currentRule, $rules, $implicitAttributes
Loading history...
94
        };
95
96 15
        if (!$validator->validateEmail(...$toValidate)) {
0 ignored issues
show
Bug introduced by
It seems like $toValidate can also be of type array<integer,string>; however, parameter $attribute of anonymous//src/Collectio....php$0::validateEmail() does only seem to accept string, 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

96
        if (!$validator->validateEmail(/** @scrutinizer ignore-type */ ...$toValidate)) {
Loading history...
97 4
            throw ValidationException::withMessages([__('Your email is invalid.')]);
98
        }
99
    }
100
101
    /**
102
     * Define how you want to validate the email.
103
     *
104
     * @return array
105
     */
106 15
    protected function validationParameters(): array
107
    {
108 15
        return ['filter', 'spoof'];
109
    }
110
111
    /**
112
     * Split the value by at symbol.
113
     *
114
     * @return void
115
     */
116 11
    protected function split(): void
117
    {
118 11
        $this->split = str($this->value())->split('/@/');
119
    }
120
}
121