Completed
Push — master ( 623d82...3c7591 )
by David
26s
created

NamingStrategy   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 5
eloc 10
dl 0
loc 29
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getInterfaceNameFromConcreteName() 0 3 1
A getOutputTypeName() 0 15 4
1
<?php
2
3
4
namespace TheCodingMachine\GraphQL\Controllers;
5
6
7
use TheCodingMachine\GraphQL\Controllers\Annotations\Type;
8
9
class NamingStrategy implements NamingStrategyInterface
10
{
11
    /**
12
     * Returns the name of the GraphQL interface from a name of a concrete class (when the interface is created
13
     * automatically to manage inheritance)
14
     */
15
    public function getInterfaceNameFromConcreteName(string $concreteType): string
16
    {
17
        return $concreteType.'Interface';
18
    }
19
20
    /**
21
     * Returns the GraphQL output object type name based on the type className and the Type annotation.
22
     */
23
    public function getOutputTypeName(string $typeClassName, Type $type): string
24
    {
25
        if ($prevPos = strrpos($typeClassName, '\\')) {
26
            $typeClassName = substr($typeClassName, $prevPos + 1);
27
        }
28
        // By default, if the class name ends with Type, let's take the name of the class for the type
29
        if (substr($typeClassName, -4) === 'Type') {
30
            return substr($typeClassName, 0, -4);
31
        }
32
        // Else, let's take the name of the targeted class
33
        $typeClassName = $type->getClass();
34
        if ($prevPos = strrpos($typeClassName, '\\')) {
35
            $typeClassName = substr($typeClassName, $prevPos + 1);
36
        }
37
        return $typeClassName;
38
    }
39
}
40