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
Pull Request — master (#116)
by joseph
35:50 queued 12:46
created

EntitySaverFactory::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 8
rs 10
c 0
b 0
f 0
ccs 0
cts 4
cp 0
cc 1
nc 1
nop 3
crap 2
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
    public function __construct(
31
        EntityManagerInterface $entityManager,
32
        EntitySaver $genericSaver,
33
        NamespaceHelper $namespaceHelper
34
    ) {
35
        $this->entityManager   = $entityManager;
36
        $this->genericSaver    = $genericSaver;
37
        $this->namespaceHelper = $namespaceHelper;
38
    }
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
     * @param string $entityFqn
57
     *
58
     * @return EntitySaverInterface
59
     */
60
    public function getSaverForEntityFqn(string $entityFqn): EntitySaverInterface
61
    {
62
        $saverFqn = $this->getSaverFqn($entityFqn);
63
        if (class_exists($saverFqn)) {
64
            return new $saverFqn($this->entityManager, $this->namespaceHelper);
65
        }
66
67
        return $this->genericSaver;
68
    }
69
70
    /**
71
     * It is possible to pass a proxy to the class which will trigger a fatal error due to autoloading problems.
72
     *
73
     * This will resolve the namespace to that of the entity, rather than the proxy. May need to update this to handle
74
     * other cases
75
     *
76
     * @param EntityInterface $entity
77
     *
78
     * @return string
79
     */
80
    private function getEntityNamespace(EntityInterface $entity): string
81
    {
82
        if ($entity instanceof Proxy) {
83
            $proxyFqn  = get_class($entity);
84
            $namespace = $this->entityManager->getConfiguration()->getProxyNamespace();
85
            $marker    = \Doctrine\Common\Persistence\Proxy::MARKER;
86
87
            return str_replace($namespace . '\\' . $marker . '\\', '', $proxyFqn);
88
        }
89
90
        return $this->namespaceHelper->getObjectFqn($entity);
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
    protected function getSaverFqn(
101
        string $entityFqn
102
    ): string {
103
104
        return \str_replace(
105
            'Entities',
106
            'Entity\\Savers',
107
            $entityFqn
108
        ) . 'Saver';
109
    }
110
}
111