Completed
Pull Request — master (#78)
by David
02:20 queued 10s
created

ExtendType::getClass()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
4
namespace TheCodingMachine\GraphQL\Controllers\Annotations;
5
6
use BadMethodCallException;
7
use function class_exists;
8
use TheCodingMachine\GraphQL\Controllers\Annotations\Exceptions\ClassNotFoundException;
9
use TheCodingMachine\GraphQL\Controllers\MissingAnnotationException;
10
11
/**
12
 * The ExtendType annotation must be put in a GraphQL type class docblock and is used to add additional fields to the underlying PHP class.
13
 *
14
 * @Annotation
15
 * @Target({"CLASS"})
16
 * @Attributes({
17
 *   @Attribute("class", type = "string"),
18
 * })
19
 */
20
class ExtendType
21
{
22
    /**
23
     * @var string
24
     */
25
    private $class;
26
27
    /**
28
     * @param mixed[] $attributes
29
     */
30
    public function __construct(array $attributes = [])
31
    {
32
        if (!isset($attributes['class'])) {
33
            throw new BadMethodCallException('In annotation @ExtendType, missing compulsory parameter "class".');
34
        }
35
        $this->class = $attributes['class'];
36
        if (!class_exists($this->class)) {
37
            throw ClassNotFoundException::couldNotFindClass($this->class);
38
        }
39
    }
40
41
    /**
42
     * Returns the name of the GraphQL query/mutation/field.
43
     * If not specified, the name of the method should be used instead.
44
     *
45
     * @return string
46
     */
47
    public function getClass(): string
48
    {
49
        return ltrim($this->class, '\\');
50
    }
51
}
52