Completed
Push — master ( 81ce9d...1672d9 )
by Oleg
03:54
created

StandardProvider   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Test Coverage

Coverage 96.88%

Importance

Changes 0
Metric Value
dl 0
loc 89
ccs 31
cts 32
cp 0.9688
rs 10
c 0
b 0
f 0
wmc 11

8 Methods

Rating   Name   Duplication   Size   Complexity  
A getEntitiesSrc() 0 9 2
A getRouteFactoryName() 0 6 1
A getControllerName() 0 10 2
A getConfigNameSpace() 0 6 1
A __construct() 0 3 1
A getInputFilterSpec() 0 3 1
A getCurrentConfig() 0 9 2
A getInputFilterName() 0 3 1
1
<?php
2
declare(strict_types=1);
3
4
namespace SlayerBirden\DFCodeGeneration\Generator\Config;
5
6
use SlayerBirden\DFCodeGeneration\Generator\BaseNameTrait;
7
use SlayerBirden\DFCodeGeneration\Generator\Factory\SimpleProvider as FactoryProvider;
8
use SlayerBirden\DFCodeGeneration\Generator\Controllers\SimpleProvider as ControllerProvider;
9
use SlayerBirden\DFCodeGeneration\Util\Lexer;
10
11
class StandardProvider implements DataProviderInterface
12
{
13
    use BaseNameTrait;
14
    /**
15
     * @var string
16
     */
17
    private $entityClassName;
18
19 15
    public function __construct(string $entityClassName)
20
    {
21 15
        $this->entityClassName = $entityClassName;
22 15
    }
23
24
    /**
25
     * @return string
26
     * @throws \ReflectionException
27
     */
28 5
    public function getRouteFactoryName(): string
29
    {
30 5
        $baseName = $this->getBaseName();
31 5
        $factoryParams = (new FactoryProvider($this->entityClassName))->provide();
32
33 5
        return $factoryParams['ns'] . "\\{$baseName}RoutesDelegator";
34
    }
35
36
    /**
37
     * @return array
38
     * @throws \Doctrine\Common\Annotations\AnnotationException
39
     * @throws \ReflectionException
40
     */
41 5
    public function getInputFilterSpec(): array
42
    {
43 5
        return (new ReflectionInputFilter($this->entityClassName))->getSpec();
44
    }
45
46 5
    public function getEntitiesSrc(): string
47
    {
48
        // expect to have 3d part as Module
49 5
        $parts = explode('\\', $this->entityClassName);
50 5
        if (isset($parts[2])) {
51 1
            return sprintf('src/%s/Entities', $parts[2]);
52
        }
53
54 4
        return '';
55
    }
56
57 5
    public function getCurrentConfig(): array
58
    {
59 5
        $config = $this->getConfigNameSpace() . '\\' . 'ConfigProvider';
60
61 5
        if (class_exists($config)) {
62
            return (new $config())();
63
        }
64
65 5
        return [];
66
    }
67
68
    /**
69
     * @param string $type
70
     * @return string
71
     * @throws \ReflectionException
72
     */
73 9
    public function getControllerName(string $type): string
74
    {
75 9
        $baseName = $this->getBaseName();
76 9
        $controllerParams = (new ControllerProvider($this->entityClassName))->provide();
77 9
        if ($type === 'gets') {
78 5
            $baseName = Lexer::getPluralForm($baseName);
79 5
            $type = 'get';
80
        }
81
82 9
        return $controllerParams['ns'] . '\\' . ucwords($type) . $baseName . 'Action';
83
    }
84
85 6
    public function getConfigNameSpace(): string
86
    {
87 6
        $parts = explode('\\', $this->entityClassName);
88 6
        array_splice($parts, -2); // Entities\Model
89
90 6
        return ltrim(implode('\\', $parts), '\\');
91
    }
92
93
    /**
94
     * @return string
95
     * @throws \ReflectionException
96
     */
97 5
    public function getInputFilterName(): string
98
    {
99 5
        return $this->getBaseName() . 'InputFilter';
100
    }
101
}
102