Passed
Pull Request — master (#98)
by Julien
03:09
created

ObjectMetadataResolver::loadRegistry()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 4
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 9
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Mapado\RestClientSdk\PHPStan\Type;
6
7
use Mapado\RestClientSdk\Mapping\ClassMetadata;
8
use Mapado\RestClientSdk\SdkClientRegistry;
9
10
final class ObjectMetadataResolver
11
{
12
    /** @var string */
13
    private $repositoryClass;
14
15
    /** @var ?SdkClientRegistry */
16
    private $registry;
17
18
    public function __construct(?string $registryFile)
19
    {
20
        if (null !== $registryFile) {
21
            $this->registry = $this->loadRegistry($registryFile);
22
        }
23
24
        $this->repositoryClass = 'Mapado\RestClientSdk\EntityRepository';
25
    }
26
27
    public function resolveClassnameForKey(string $key): string
28
    {
29
        if (null === $this->registry) {
30
            return $key;
31
        }
32
33
        $metadata = $this->getMetadataForKeyOrClassname($key);
34
35
        return $metadata->getModelName();
36
    }
37
38
    public function getRepositoryClass(string $className): string
39
    {
40
        if (null === $this->registry) {
41
            return $this->repositoryClass;
42
        }
43
44
        return $this
45
            ->registry
46
            ->getSdkClientForClass($className)
47
            ->getMapping()
48
            ->getClassMetadata($className)
49
            ->getRepositoryName()
50
        ;
51
    }
52
53
    private function getMetadataForKeyOrClassname(string $value): ClassMetadata
54
    {
55
        foreach ($this->registry->getSdkClientList() as $sdkClient) {
0 ignored issues
show
Bug introduced by
The method getSdkClientList() does not exist on null. ( Ignorable by Annotation )

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

55
        foreach ($this->registry->/** @scrutinizer ignore-call */ getSdkClientList() as $sdkClient) {

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...
56
            $mapping = $sdkClient->getMapping();
57
58
            // get repository by key
59
            $metadata = $mapping->getClassMetadataByKey($value);
60
61
            if ($metadata) {
62
                return $metadata;
63
            }
64
65
            if ($mapping->hasClassMetadata($value)) {
66
                return $mapping->getClassMetadata($value);
67
            }
68
        }
69
70
        throw new \RuntimeException('Unable to find sdk client for key or classname ' . $value);
71
    }
72
73
    private function loadRegistry(string $registryFile): ?SdkClientRegistry
74
    {
75
        if (!file_exists($registryFile)
76
            || !is_readable($registryFile)
77
        ) {
78
            throw new \PHPStan\ShouldNotHappenException('Object manager could not be loaded');
79
        }
80
81
        return require $registryFile;
82
    }
83
}
84