Completed
Push — master ( e6319e...8dcc47 )
by Adrien
07:50
created

EmailType   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 11
dl 0
loc 39
ccs 14
cts 14
cp 1
rs 10
c 0
b 0
f 0
wmc 10

4 Methods

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