Passed
Push — feature/initial-implementation ( 0c2c6c...79751f )
by Fike
01:54
created

MappingProvider::getMapping()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AmaTeam\ElasticSearch\Mapping;
6
7
use AmaTeam\ElasticSearch\API\Mapping\DocumentMappingInterface;
8
use AmaTeam\ElasticSearch\API\Mapping\DocumentMappingProviderInterface;
9
use AmaTeam\ElasticSearch\Mapping\Annotation\Reader;
10
11
class MappingProvider implements DocumentMappingProviderInterface
12
{
13
    /**
14
     * @var Reader
15
     */
16
    private $reader;
17
    /**
18
     * @var DocumentMappingInterface[]
19
     */
20
    private $registry;
21
22
    /**
23
     * @param Reader $reader
24
     */
25
    public function __construct(Reader $reader = null)
26
    {
27
        $this->reader = $reader ?? new Reader();
28
    }
29
30
    public function getMapping(string $className): DocumentMappingInterface
31
    {
32
        if (isset($this->registry[$className])) {
33
            return $this->registry[$className];
34
        }
35
        $mapping = $this->reader->read($className);
36
        $this->registry[$className] = $mapping;
37
        return $mapping;
38
    }
39
}
40