DomainNormalizer   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 10
c 1
b 0
f 0
dl 0
loc 33
rs 10
wmc 6

5 Methods

Rating   Name   Duplication   Size   Complexity  
A supportsDenormalization() 0 3 1
A __construct() 0 3 1
A normalize() 0 4 1
A denormalize() 0 8 2
A supportsNormalization() 0 3 1
1
<?php
2
3
namespace W2w\Lib\ApieDomainPlugin\Normalizers;
4
5
use Pdp\Domain;
6
use Pdp\Rules;
7
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
8
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
9
use W2w\Lib\Apie\Exceptions\InvalidValueForValueObjectException;
10
11
class DomainNormalizer implements NormalizerInterface, DenormalizerInterface
12
{
13
    private $rules;
14
15
    public function __construct(Rules $rules)
16
    {
17
        $this->rules = $rules;
18
    }
19
20
    public function denormalize($data, $class, $format = null, array $context = [])
21
    {
22
        $domain = $this->rules->resolve((string) $data, Rules::ICANN_DOMAINS);
23
        if (!$domain->isResolvable()) {
24
            throw new InvalidValueForValueObjectException($data, Domain::class);
25
        }
26
27
        return $domain;
28
    }
29
30
    public function supportsDenormalization($data, $type, $format = null)
31
    {
32
        return $type === Domain::class;
33
    }
34
35
    public function normalize($object, $format = null, array $context = [])
36
    {
37
        /** @var Domain $object */
38
        return (string) $object;
39
    }
40
41
    public function supportsNormalization($data, $format = null)
42
    {
43
        return $data instanceof Domain;
44
    }
45
}
46