MetadataFactory   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 19
dl 0
loc 69
rs 10
c 1
b 0
f 0
wmc 11

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getTypeName() 0 3 1
A stripEntityPrefix() 0 11 3
A appendGroupSuffix() 0 11 3
A getDefaultStrategy() 0 16 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace ApiSkeletons\Doctrine\ORM\GraphQL\Metadata\Common;
6
7
use ApiSkeletons\Doctrine\ORM\GraphQL\Config;
8
use ApiSkeletons\Doctrine\ORM\GraphQL\Hydrator\Strategy;
9
10
use function in_array;
11
use function str_replace;
12
use function strlen;
13
use function strpos;
14
use function substr;
15
16
/**
17
 * This ancestor class contains functions common to the MetadataFactory
18
 * and GlobalEnable
19
 */
20
abstract class MetadataFactory
21
{
22
    protected function getDefaultStrategy(string|null $fieldType): string
23
    {
24
        // Set default strategy based on field type
25
        if (in_array($fieldType, ['tinyint', 'smallint', 'integer', 'int'])) {
26
            return Strategy\ToInteger::class;
27
        }
28
29
        if (in_array($fieldType, ['decimal', 'float'])) {
30
            return Strategy\ToFloat::class;
31
        }
32
33
        if ($fieldType === 'boolean') {
34
            return Strategy\ToBoolean::class;
35
        }
36
37
        return Strategy\FieldDefault::class;
38
    }
39
40
    /**
41
     * Compute the GraphQL type name
42
     *
43
     * @param class-string $entityClass
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string.
Loading history...
44
     */
45
    protected function getTypeName(string $entityClass): string
46
    {
47
        return $this->appendGroupSuffix($this->stripEntityPrefix($entityClass));
48
    }
49
50
    /**
51
     * Strip the configured entityPrefix from the type name
52
     *
53
     * @param class-string $entityClass
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string.
Loading history...
54
     */
55
    protected function stripEntityPrefix(string $entityClass): string
56
    {
57
        $entityClassWithPrefix = $entityClass;
58
59
        if ($this->getConfig()->getEntityPrefix() !== null) {
60
            if (strpos($entityClass, (string) $this->getConfig()->getEntityPrefix()) === 0) {
61
                $entityClassWithPrefix = substr($entityClass, strlen((string) $this->getConfig()->getEntityPrefix()));
62
            }
63
        }
64
65
        return str_replace('\\', '_', $entityClassWithPrefix);
66
    }
67
68
    /**
69
     * Append the configured groupSuffix to the type name
70
     */
71
    protected function appendGroupSuffix(string $entityClass): string
72
    {
73
        if ($this->getConfig()->getGroupSuffix() !== null) {
74
            if ($this->getConfig()->getGroupSuffix()) {
75
                $entityClass .= '_' . $this->getConfig()->getGroupSuffix();
76
            }
77
        } else {
78
            $entityClass .= '_' . $this->getConfig()->getGroup();
79
        }
80
81
        return $entityClass;
82
    }
83
84
    /**
85
     * Because the Config class is not available in this class,
86
     * this method must be implemented in the child class
87
     */
88
    abstract protected function getConfig(): Config;
89
}
90