Passed
Push — main ( d0cb76...ea9e69 )
by Tom
01:11 queued 12s
created

AbstractMetadataFactory::getDefaultStrategy()   B

Complexity

Conditions 8
Paths 8

Size

Total Lines 19
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 8
eloc 13
c 1
b 0
f 0
nc 8
nop 1
dl 0
loc 19
rs 8.4444
1
<?php
2
3
declare(strict_types=1);
4
5
namespace ApiSkeletons\Doctrine\GraphQL\Metadata;
6
7
use ApiSkeletons\Doctrine\GraphQL\Config;
8
use ApiSkeletons\Doctrine\GraphQL\Hydrator\Strategy;
9
10
use function str_replace;
11
use function strlen;
12
use function strpos;
13
use function substr;
14
15
/**
16
 * This ancestor class contains functions common to the MetadataFactory
17
 * and GlobalEnable
18
 */
19
abstract class AbstractMetadataFactory
20
{
21
    protected Config $config;
22
23
    protected function getDefaultStrategy(string|null $fieldType): string
24
    {
25
        // Set default strategy based on field type
26
        switch ($fieldType) {
27
            case 'tinyint':
28
            case 'smallint':
29
            case 'integer':
30
            case 'int':
31
                return Strategy\ToInteger::class;
32
33
            case 'boolean':
34
                return Strategy\ToBoolean::class;
35
36
            case 'decimal':
37
            case 'float':
38
                return Strategy\ToFloat::class;
39
40
            default:
41
                return Strategy\FieldDefault::class;
42
        }
43
    }
44
45
    /**
46
     * Compute the GraphQL type name
47
     */
48
    protected function getTypeName(string $entityClass): string
49
    {
50
        return $this->appendGroupSuffix($this->stripEntityPrefix($entityClass));
51
    }
52
53
    /**
54
     * Strip the configured entityPrefix from the type name
55
     */
56
    protected function stripEntityPrefix(string $entityClass): string
57
    {
58
        if ($this->config->getEntityPrefix() !== null) {
59
            if (strpos($entityClass, $this->config->getEntityPrefix()) === 0) {
60
                $entityClass = substr($entityClass, strlen($this->config->getEntityPrefix()));
61
            }
62
        }
63
64
        return str_replace('\\', '_', $entityClass);
65
    }
66
67
    /**
68
     * Append the configured groupSuffix from the type name
69
     */
70
    protected function appendGroupSuffix(string $entityClass): string
71
    {
72
        if ($this->config->getGroupSuffix() !== null) {
73
            if ($this->config->getGroupSuffix()) {
74
                $entityClass .= '_' . $this->config->getGroupSuffix();
75
            }
76
        } else {
77
            $entityClass .= '_' . $this->config->getGroup();
78
        }
79
80
        return $entityClass;
81
    }
82
}
83