Passed
Push — feature/initial-implementation ( 3b7c72...40c5a6 )
by Fike
01:49
created

Registry::find()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 3
eloc 5
nc 3
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AmaTeam\ElasticSearch\Mapping\Type\Infrastructure;
6
7
use AmaTeam\ElasticSearch\API\Mapping\Type\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
37
class Registry
38
{
39
    /**
40
     * @var TypeInterface[]
41
     */
42
    private $idIndex = [];
43
    /**
44
     * @var TypeInterface[]
45
     */
46
    private $friendlyIdIndex = [];
47
48
    /**
49
     * @param string $id
50
     * @return TypeInterface|null
51
     */
52
    public function find(string $id): ?TypeInterface
53
    {
54
        if (isset($this->friendlyIdIndex[$id])) {
55
            return $this->friendlyIdIndex[$id];
56
        }
57
        if (isset($this->idIndex[$id])) {
58
            return $this->idIndex[$id];
59
        }
60
        return null;
61
    }
62
63
    public function register(TypeInterface $type): Registry
64
    {
65
        $this->idIndex[$type->getId()] = $type;
66
        $this->friendlyIdIndex[$type->getFriendlyId()] = $type;
67
        return $this;
68
    }
69
70
    /**
71
     * @return TypeInterface[]
72
     */
73
    public function getAll(): array
74
    {
75
        return array_values($this->idIndex);
76
    }
77
78
    public function withDefaultTypes(): Registry
79
    {
80
        // TODO: use code generation
81
        $types = [
82
            BinaryType::getInstance(),
83
            BooleanType::getInstance(),
84
            ByteType::getInstance(),
85
            DateRangeType::getInstance(),
86
            DateType::getInstance(),
87
            DoubleRangeType::getInstance(),
88
            DoubleType::getInstance(),
89
            FloatRangeType::getInstance(),
90
            FloatType::getInstance(),
91
            GeoPointType::getInstance(),
92
            GeoShapeType::getInstance(),
93
            HalfFloatType::getInstance(),
94
            IntegerRangeType::getInstance(),
95
            IntegerType::getInstance(),
96
            IpRangeType::getInstance(),
97
            IpType::getInstance(),
98
            JoinType::getInstance(),
99
            KeywordType::getInstance(),
100
            LongRangeType::getInstance(),
101
            LongType::getInstance(),
102
            NestedType::getInstance(),
103
            ObjectType::getInstance(),
104
            PercolatorType::getInstance(),
105
            RootType::getInstance(),
106
            ScaledFloatType::getInstance(),
107
            ShortType::getInstance(),
108
            TextType::getInstance(),
109
            TokenCountType::getInstance(),
110
        ];
111
        foreach ($types as $type) {
112
            $this->register($type);
113
        }
114
        return $this;
115
    }
116
117
    private static $instance;
118
119
    public static function getInstance(): Registry
120
    {
121
        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...
122
            static::$instance = (new static())
123
                ->withDefaultTypes();
124
        }
125
        return static::$instance;
126
    }
127
}
128