AbstractEndpoint   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A validateEntity() 0 13 2
A __construct() 0 7 1
A getClient() 0 3 1
1
<?php
2
3
/*
4
 * This file is part of the CwdPowerDNS Client
5
 *
6
 * (c) 2018 cwd.at GmbH <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace Cwd\PowerDNSClient\Endpoints;
15
16
use Cwd\PowerDNSClient\Client;
17
use Cwd\PowerDNSClient\Validator\ValidationException;
18
use Symfony\Component\Validator\Validation;
19
20
abstract class AbstractEndpoint
21
{
22
    private $client;
23
    protected $defaultServerId;
24
    protected $validator;
25
26
    public function __construct(Client $client, $defaultServerId)
27
    {
28
        $this->client = $client;
29
        $this->defaultServerId = $defaultServerId;
30
        $this->validator = Validation::createValidatorBuilder()
31
                            ->enableAnnotationMapping()
32
                            ->getValidator();
33
    }
34
35
    public function validateEntity($entity, $groups = null): bool
36
    {
37
        $violations = $this->validator->validate($entity, null, $groups);
38
        if (\count($violations) > 0) {
39
            throw new ValidationException(
40
                sprintf('Entity %s does not validate to spezification', \get_class($entity)),
41
                0,
42
                null,
43
                $violations
44
            );
45
        }
46
47
        return true;
48
    }
49
50
    public function getClient(): Client
51
    {
52
        return $this->client;
53
    }
54
}
55