EmailType   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 12
c 1
b 0
f 0
dl 0
loc 37
ccs 15
cts 15
cp 1
rs 10
wmc 9

4 Methods

Rating   Name   Duplication   Size   Complexity  
A serialize() 0 7 2
A parseValue() 0 7 2
A parseLiteral() 0 7 3
A isValid() 0 5 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Ecodev\Felix\Api\Scalar;
6
7
use Ecodev\Felix\Validator\DeliverableEmail;
8
use GraphQL\Language\AST\Node;
9
use GraphQL\Language\AST\StringValueNode;
10
11
/**
12
 * Represent an email address.
13
 *
14
 * This exceptionally accept empty string as null because email address are often unique
15
 * in DB and thus can never be empty string to indicate absence of email. So we simplify
16
 * the client work by accepting empty string and transparently transforming into a null value.
17
 */
18
final class EmailType extends AbstractStringBasedType
19
{
20
    /**
21
     * Validate a email.
22
     */
23 12
    protected function isValid(?string $value): bool
24
    {
25 12
        $validator = new DeliverableEmail();
26
27 12
        return $value === null || $validator->isValid($value);
28
    }
29
30 7
    public function serialize(mixed $value): mixed
31
    {
32 7
        if ($value === '') {
33 1
            return null;
34
        }
35
36 6
        return parent::serialize($value);
37
    }
38
39 7
    public function parseValue(mixed $value): ?string
40
    {
41 7
        if ($value === '') {
42 1
            return null;
43
        }
44
45 6
        return parent::parseValue($value);
46
    }
47
48 8
    public function parseLiteral(Node $valueNode, ?array $variables = null): ?string
49
    {
50 8
        if ($valueNode instanceof StringValueNode && $valueNode->value === '') {
51 1
            return null;
52
        }
53
54 7
        return parent::parseLiteral($valueNode, $variables);
55
    }
56
}
57