Passed
Push — dev ( 40f0b5...3a2e98 )
by Fike
02:50
created

Registry::withDefaultTypes()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 37
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 37
rs 8.8571
c 0
b 0
f 0
cc 2
eloc 32
nc 2
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AmaTeam\ElasticSearch\Mapping\Type\Infrastructure;
6
7
use AmaTeam\ElasticSearch\API\Mapping\TypeInterface;
8
use AmaTeam\ElasticSearch\Mapping\Type\BinaryType;
9
use AmaTeam\ElasticSearch\Mapping\Type\BooleanType;
10
use AmaTeam\ElasticSearch\Mapping\Type\ByteType;
11
use AmaTeam\ElasticSearch\Mapping\Type\DateRangeType;
12
use AmaTeam\ElasticSearch\Mapping\Type\DateType;
13
use AmaTeam\ElasticSearch\Mapping\Type\DoubleRangeType;
14
use AmaTeam\ElasticSearch\Mapping\Type\DoubleType;
15
use AmaTeam\ElasticSearch\Mapping\Type\FloatRangeType;
16
use AmaTeam\ElasticSearch\Mapping\Type\FloatType;
17
use AmaTeam\ElasticSearch\Mapping\Type\GeoPointType;
18
use AmaTeam\ElasticSearch\Mapping\Type\GeoShapeType;
19
use AmaTeam\ElasticSearch\Mapping\Type\HalfFloatType;
20
use AmaTeam\ElasticSearch\Mapping\Type\IntegerRangeType;
21
use AmaTeam\ElasticSearch\Mapping\Type\IntegerType;
22
use AmaTeam\ElasticSearch\Mapping\Type\IpRangeType;
23
use AmaTeam\ElasticSearch\Mapping\Type\IpType;
24
use AmaTeam\ElasticSearch\Mapping\Type\JoinType;
25
use AmaTeam\ElasticSearch\Mapping\Type\KeywordType;
26
use AmaTeam\ElasticSearch\Mapping\Type\LongRangeType;
27
use AmaTeam\ElasticSearch\Mapping\Type\LongType;
28
use AmaTeam\ElasticSearch\Mapping\Type\NestedType;
29
use AmaTeam\ElasticSearch\Mapping\Type\ObjectType;
30
use AmaTeam\ElasticSearch\Mapping\Type\PercolatorType;
31
use AmaTeam\ElasticSearch\Mapping\Type\RootType;
32
use AmaTeam\ElasticSearch\Mapping\Type\ScaledFloatType;
33
use AmaTeam\ElasticSearch\Mapping\Type\ShortType;
34
use AmaTeam\ElasticSearch\Mapping\Type\TextType;
35
use AmaTeam\ElasticSearch\Mapping\Type\TokenCountType;
36
use BadMethodCallException;
37
38
/**
39
 * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
40
 */
41
class Registry
42
{
43
    /**
44
     * @var TypeInterface[]
45
     */
46
    private $idIndex = [];
47
    /**
48
     * @var TypeInterface[]
49
     */
50
    private $friendlyIdIndex = [];
51
52
    /**
53
     * @param string $id
54
     * @return TypeInterface|null
55
     */
56
    public function find(string $id): ?TypeInterface
57
    {
58
        if (isset($this->friendlyIdIndex[$id])) {
59
            return $this->friendlyIdIndex[$id];
60
        }
61
        if (isset($this->idIndex[$id])) {
62
            return $this->idIndex[$id];
63
        }
64
        return null;
65
    }
66
67
    public function get(string $id): TypeInterface
68
    {
69
        $type = $this->find($id);
70
        if ($type) {
71
            return $type;
72
        }
73
        throw new BadMethodCallException("Unknown mapping type `$id`");
74
    }
75
76
    public function register(TypeInterface $type): Registry
77
    {
78
        $this->idIndex[$type->getId()] = $type;
79
        $this->friendlyIdIndex[$type->getFriendlyId()] = $type;
80
        return $this;
81
    }
82
83
    /**
84
     * @return TypeInterface[]
85
     */
86
    public function getAll(): array
87
    {
88
        return array_values($this->idIndex);
89
    }
90
91
    public function withDefaultTypes(): Registry
92
    {
93
        // TODO: use code generation
94
        $types = [
95
            BinaryType::getInstance(),
96
            BooleanType::getInstance(),
97
            ByteType::getInstance(),
98
            DateRangeType::getInstance(),
99
            DateType::getInstance(),
100
            DoubleRangeType::getInstance(),
101
            DoubleType::getInstance(),
102
            FloatRangeType::getInstance(),
103
            FloatType::getInstance(),
104
            GeoPointType::getInstance(),
105
            GeoShapeType::getInstance(),
106
            HalfFloatType::getInstance(),
107
            IntegerRangeType::getInstance(),
108
            IntegerType::getInstance(),
109
            IpRangeType::getInstance(),
110
            IpType::getInstance(),
111
            JoinType::getInstance(),
112
            KeywordType::getInstance(),
113
            LongRangeType::getInstance(),
114
            LongType::getInstance(),
115
            NestedType::getInstance(),
116
            ObjectType::getInstance(),
117
            PercolatorType::getInstance(),
118
            RootType::getInstance(),
119
            ScaledFloatType::getInstance(),
120
            ShortType::getInstance(),
121
            TextType::getInstance(),
122
            TokenCountType::getInstance(),
123
        ];
124
        foreach ($types as $type) {
125
            $this->register($type);
126
        }
127
        return $this;
128
    }
129
130
    private static $instance;
131
132
    public static function getInstance(): Registry
133
    {
134
        if (!static::$instance) {
0 ignored issues
show
Bug introduced by
Since $instance is declared private, accessing it with static will lead to errors in possible sub-classes; you can either use self, or increase the visibility of $instance to at least protected.
Loading history...
135
            static::$instance = (new static())
136
                ->withDefaultTypes();
137
        }
138
        return static::$instance;
139
    }
140
}
141