RepositoryFactory   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 98
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
eloc 30
dl 0
loc 98
ccs 29
cts 29
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A getRepository() 0 26 3
A create() 0 13 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace CCT\Component\ODMElasticsearch\Repository;
6
7
use CCT\Component\ODMElasticsearch\Metadata\ClassMetadata;
8
use CCT\Component\ODMElasticsearch\Repository\Exception\InvalidArgumentException;
9
use CCT\Component\ODMElasticsearch\Repository\Exception\NoMetadataConfigException;
10
use CCT\Component\ODMElasticsearch\Repository\Model\DocumentSupportInterface;
11
use CCT\Component\ODMElasticsearch\Transformer\DataTransformerInterface;
12
use Metadata\MetadataFactory;
13
14
class RepositoryFactory
15
{
16
    /**
17
     * @var IndexMapping
18
     */
19
    protected $indexMapping;
20
21
    /**
22
     * @var DataTransformerInterface
23
     */
24
    protected $dataTransformer;
25
26
    /**
27
     * @var array
28
     */
29
    protected $repositories;
30
31
    /**
32
     * @var MetadataFactory
33
     */
34
    protected $metadataFactory;
35
36
    /**
37
     * RepositoryFactory constructor.
38
     *
39
     * @param IndexMapping $indexMapping
40
     * @param DataTransformerInterface $dataTransformer
41
     * @param MetadataFactory $metadataFactory
42
     */
43 4
    public function __construct(
44
        IndexMapping $indexMapping,
45
        DataTransformerInterface $dataTransformer,
46
        MetadataFactory $metadataFactory
47
    ) {
48 4
        $this->indexMapping = $indexMapping;
49 4
        $this->dataTransformer = $dataTransformer;
50 4
        $this->repositories = [];
51 4
        $this->metadataFactory = $metadataFactory;
52 4
    }
53
54
    /**
55
     * Gets repository for entity class name
56
     *
57
     * @param string $entityName the entity class name
58
     *
59
     * @return AbstractElasticsearchRepository|mixed
60
     * @throws \ReflectionException
61
     * @throws \Exception
62
     */
63 4
    public function getRepository(string $entityName)
64
    {
65 4
        $metadata = $this->metadataFactory->getMetadataForClass($entityName);
66
67 4
        if (null === $metadata) {
68 1
            throw new NoMetadataConfigException(
69 1
                sprintf('No metadata config was found for "%s"', $entityName)
70
            );
71
        }
72
73
        /** @var ClassMetadata $classMetadata */
74 3
        $classMetadata = $metadata->getRootClassMetadata();
0 ignored issues
show
Bug introduced by
The method getRootClassMetadata() does not exist on Metadata\MergeableClassMetadata. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

74
        /** @scrutinizer ignore-call */ 
75
        $classMetadata = $metadata->getRootClassMetadata();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
75 3
        $customRepositoryName = $classMetadata->getCustomRepositoryName();
76
77 3
        $class = new \ReflectionClass($entityName);
78 3
        if (!$class->implementsInterface(DocumentSupportInterface::class)) {
79 1
            throw new InvalidArgumentException(
80 1
                sprintf(
81 1
                    'Class "%s", does not implement interface "%s"',
82 1
                    $entityName,
83 1
                    DocumentSupportInterface::class
84
                )
85
            );
86
        }
87
88 2
        return $this->repositories[$entityName] ?? $this->create($entityName, $customRepositoryName);
89
    }
90
91
    /**
92
     * @param string $entityName
93
     * @param string|null $customRepositoryName
94
     *
95
     * @return AbstractElasticsearchRepository
96
     *
97
     * @throws \Exception
98
     */
99 2
    protected function create(string $entityName, string $customRepositoryName = null): AbstractElasticsearchRepository
100
    {
101 2
        if (null === $customRepositoryName) {
102 1
            $repository = new ElasticsearchRepository($entityName, $this->indexMapping, $this->dataTransformer);
103 1
            $this->repositories[$entityName] = $repository;
104
105 1
            return $repository;
106
        }
107
108 1
        $repository = new $customRepositoryName($entityName, $this->indexMapping, $this->dataTransformer);
109 1
        $this->repositories[$customRepositoryName] = $repository;
110
111 1
        return $repository;
112
    }
113
}
114