DomainNormalizer::normalize()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 3
dl 0
loc 4
rs 10
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