GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 75bdf9...8faa57 )
by joseph
83:56 queued 81:04
created

EntitySaverFactory::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 8
rs 10
c 0
b 0
f 0
ccs 4
cts 4
cp 1
cc 1
nc 1
nop 3
crap 1
1
<?php declare(strict_types=1);
2
3
namespace EdmondsCommerce\DoctrineStaticMeta\Entity\Savers;
4
5
use Doctrine\ORM\EntityManagerInterface;
6
use Doctrine\ORM\Proxy\Proxy;
7
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\NamespaceHelper;
8
use EdmondsCommerce\DoctrineStaticMeta\Entity\Interfaces\EntityInterface;
9
10
class EntitySaverFactory
11
{
12
    /**
13
     * @var EntityManagerInterface
14
     */
15
    protected $entityManager;
16
17
    /**
18
     * @var EntitySaver
19
     */
20
    protected $genericEntitySaver;
21
    /**
22
     * @var EntitySaver
23
     */
24
    protected $genericSaver;
25
    /**
26
     * @var NamespaceHelper
27
     */
28
    protected $namespaceHelper;
29
30 4
    public function __construct(
31
        EntityManagerInterface $entityManager,
32
        EntitySaver $genericSaver,
33
        NamespaceHelper $namespaceHelper
34
    ) {
35 4
        $this->entityManager   = $entityManager;
36 4
        $this->genericSaver    = $genericSaver;
37 4
        $this->namespaceHelper = $namespaceHelper;
38 4
    }
39
40
    /**
41
     * Gets the Entity Specific Saver if one is defined, otherwise the standard Entity Saver
42
     *
43
     * @param EntityInterface $entity
44
     *
45
     * @return EntitySaverInterface
46
     */
47 3
    public function getSaverForEntity(
48
        EntityInterface $entity
49
    ): EntitySaverInterface {
50 3
        $fqn = $this->getEntityNamespace($entity);
51
52 3
        return $this->getSaverForEntityFqn($fqn);
53
    }
54
55
    /**
56
     * It is possible to pass a proxy to the class which will trigger a fatal error due to autoloading problems.
57
     *
58
     * This will resolve the namespace to that of the entity, rather than the proxy. May need to update this to handle
59
     * other cases
60
     *
61
     * @param EntityInterface $entity
62
     *
63
     * @return string
64
     */
65 3
    private function getEntityNamespace(EntityInterface $entity): string
66
    {
67 3
        if ($entity instanceof Proxy) {
68
            $proxyFqn  = \get_class($entity);
69
            $namespace = $this->entityManager->getConfiguration()->getProxyNamespace();
70
            $marker    = \Doctrine\Common\Persistence\Proxy::MARKER;
71
72
            return str_replace($namespace . '\\' . $marker . '\\', '', $proxyFqn);
73
        }
74
75 3
        return $this->namespaceHelper->getObjectFqn($entity);
76
    }
77
78
    /**
79
     * @param string $entityFqn
80
     *
81
     * @return EntitySaverInterface
82
     */
83 4
    public function getSaverForEntityFqn(string $entityFqn): EntitySaverInterface
84
    {
85 4
        $saverFqn = $this->getSaverFqn($entityFqn);
86 4
        if (class_exists($saverFqn)) {
87 2
            return new $saverFqn($this->entityManager, $this->namespaceHelper);
88
        }
89
90 2
        return $this->genericSaver;
91
    }
92
93
    /**
94
     * Get the fully qualified name of the saver for the entity we are testing.
95
     *
96
     * @param string $entityFqn
97
     *
98
     * @return string
99
     */
100 4
    protected function getSaverFqn(
101
        string $entityFqn
102
    ): string {
103
104 4
        return \str_replace(
105 4
            'Entities',
106 4
            'Entity\\Savers',
107 4
            $entityFqn
108 4
        ) . 'Saver';
109
    }
110
}
111