AbstractSpecializationFactory   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 3
eloc 17
c 1
b 0
f 0
dl 0
loc 44
ccs 0
cts 15
cp 0
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A specializedFactoryAll() 0 23 3
1
<?php declare(strict_types=1);
2
3
namespace Formularium\Factory;
4
5
use Formularium\CodeGenerator\CodeGenerator;
6
use HaydenPierce\ClassFinder\ClassFinder;
7
8
/**
9
 * Extends the Abstract Factory for specialization classes, such as Element or Renderable,
10
 * that are children of a BaseSpecialization (e.g. "Framework")
11
 */
12
abstract class AbstractSpecializationFactory extends AbstractFactory
13
{
14
    /**
15
     * The base classe name, which is prepended, like 'DatatypeGenerator'
16
     *
17
     * @return string
18
     */
19
    abstract public static function baseclassName(): string;
20
21
    /**
22
     * @param mixed $datatypeName
23
     * @param object $baseSpecialization
24
     * @param object $composer
25
     * @return mixed
26
     */
27
    abstract public static function specializedFactory($datatypeName, object $baseSpecialization, object $composer = null);
28
29
    /**
30
     * @param CodeGenerator $codeGenerator
31
     * @return array
32
     */
33
    public static function specializedFactoryAll(CodeGenerator $codeGenerator): array
34
    {
35
        $subns = static::getSubNamespace();
36
        $codeGeneratorClassname = $codeGenerator->getName();
37
        $objects = [];
38
        foreach (static::getBaseNamespaces() as $ns) {
39
            $dgNS = "$ns\\$subns\\$codeGeneratorClassname\\" . static::baseclassName();
40
            $classes = ClassFinder::getClassesInNamespace($dgNS);
41
            $objects = array_merge(
42
                $objects,
43
                array_map(
44
                    function ($c) {
45
                        try {
46
                            return new $c();
47
                        } catch (\Throwable $e) {
48
                            return null;
49
                        }
50
                    },
51
                    $classes
52
                )
53
            );
54
        }
55
        return array_filter($objects);
56
    }
57
}
58