DTO   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
wmc 7
lcom 0
cbo 2
dl 0
loc 48
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 26 6
A getClass() 0 4 1
1
<?php
2
/*
3
 * This file is part of the StfalconApiBundle.
4
 *
5
 * (c) Stfalcon LLC <stfalcon.com>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
declare(strict_types=1);
12
13
namespace StfalconStudio\ApiBundle\Annotation;
14
15
use Doctrine\Common\Annotations\Annotation;
16
use StfalconStudio\ApiBundle\DTO\DtoInterface;
17
use StfalconStudio\ApiBundle\Exception\InvalidArgumentException;
18
use StfalconStudio\ApiBundle\Exception\LogicException;
19
20
/**
21
 * Data Transfer Object Annotation.
22
 *
23
 * @Annotation
24
 *
25
 * @Target({"CLASS"})
26
 */
27
class DTO implements DtoAnnotationInterface
28
{
29
    private const DTO_SUFFIX = 'Dto';
30
31
    /** @var string */
32
    private $class;
33
34
    /**
35
     * @param mixed[] $options
36
     *
37
     * @throws LogicException
38
     * @throws InvalidArgumentException
39
     */
40
    public function __construct(array $options)
41
    {
42
        if (!\array_key_exists('value', $options)) {
43
            throw new LogicException('DTO class must be set.');
44
        }
45
46
        $class = $options['value'];
47
48
        if (!\is_string($class)) {
49
            throw new InvalidArgumentException('Value should be string');
50
        }
51
52
        if (!\class_exists($class)) {
53
            throw new InvalidArgumentException(\sprintf('Class %s does not exist.', $class));
54
        }
55
56
        if (!\is_subclass_of($class, DtoInterface::class)) {
0 ignored issues
show
Bug introduced by
Due to PHP Bug #53727, is_subclass_of might return inconsistent results on some PHP versions if \StfalconStudio\ApiBundle\DTO\DtoInterface::class can be an interface. If so, you could instead use ReflectionClass::implementsInterface.
Loading history...
57
            throw new InvalidArgumentException(\sprintf('Class %s does not implement %s interface.', $class, DtoInterface::class));
58
        }
59
60
        if (self::DTO_SUFFIX !== \mb_substr($class, -3)) {
61
            throw new InvalidArgumentException(\sprintf('Class name %s must be suffixed with "%s".', $class, self::DTO_SUFFIX));
62
        }
63
64
        $this->class = $class;
65
    }
66
67
    /**
68
     * {@inheritdoc}
69
     */
70
    public function getClass(): string
71
    {
72
        return $this->class;
73
    }
74
}
75