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

MappingProvider::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
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