Passed
Pull Request — master (#2)
by Alex
02:28
created

EntityManagerProvider::setEntityManagers()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 4
c 0
b 0
f 0
rs 10
cc 2
nc 2
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Arp\LaminasDoctrine\Service\EntityManager;
6
7
use Arp\LaminasDoctrine\Config\DoctrineConfig;
8
use Arp\LaminasDoctrine\Factory\Service\EntityManagerFactory;
9
use Doctrine\ORM\EntityManagerInterface;
10
11
/**
12
 * @author  Alex Patterson <[email protected]>
13
 * @package Arp\LaminasDoctrine\Service\EntityManager
14
 */
15
final class EntityManagerProvider implements EntityManagerProviderInterface
16
{
17
    /**
18
     * @var DoctrineConfig
19
     */
20
    private DoctrineConfig $config;
21
22
    /**
23
     * @var EntityManagerManager
24
     */
25
    private EntityManagerManager $manager;
26
27
    /**
28
     * @param DoctrineConfig       $config
29
     * @param EntityManagerManager $manager
30
     * @param array                $entityManagers
31
     */
32
    public function __construct(DoctrineConfig $config, EntityManagerManager $manager, array $entityManagers = [])
33
    {
34
        $this->config = $config;
35
        $this->manager = $manager;
36
37
        $this->setEntityManagers($entityManagers);
38
    }
39
40
    /**
41
     * Set the configuration options for a single entity manager with the provided $name
42
     *
43
     * @param string $name
44
     * @param array  $config
45
     */
46
    public function setEntityManagerConfig(string $name, array $config): void
47
    {
48
        $this->config->setEntityManagerConfig($name, $config);
49
    }
50
51
    /**
52
     * Check if the entity manager is registered with the provider
53
     *
54
     * @param string $name The name of the entity manager to check
55
     *
56
     * @return bool
57
     */
58
    public function hasEntityManager(string $name): bool
59
    {
60
        return $this->manager->has($name) || $this->config->hasEntityManagerConfig($name);
61
    }
62
63
    /**
64
     * @param string $name
65
     *
66
     * @return EntityManagerInterface
67
     *
68
     * @throws EntityManagerProviderException
69
     */
70
    public function getEntityManager(string $name): EntityManagerInterface
71
    {
72
        try {
73
            if (!$this->manager->has($name) && $this->config->hasEntityManagerConfig($name)) {
74
                $this->manager->setService($name, $this->create($name, $this->config->getEntityManagerConfig($name)));
75
            }
76
77
            if ($this->manager->has($name)) {
78
                return $this->manager->get($name);
79
            }
80
        } catch (EntityManagerProviderException $e) {
0 ignored issues
show
Bug introduced by
The type Arp\LaminasDoctrine\Serv...anagerProviderException was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
81
            throw $e;
82
        } catch (\Throwable $e) {
83
            throw new EntityManagerProviderException(
84
                sprintf('Failed return entity manager \'%s\': %s', $name, $e->getMessage()),
85
                $e->getCode(),
86
                $e
87
            );
88
        }
89
90
        throw new EntityManagerProviderException(
91
            sprintf('Unable to find entity manager \'%s\'', $name)
92
        );
93
    }
94
95
    /**
96
     * @param string $name
97
     *
98
     * @return EntityManagerInterface
99
     *
100
     * @throws EntityManagerProviderException
101
     */
102
    public function refresh(string $name): EntityManagerInterface
103
    {
104
        if ($this->manager->has($name)) {
105
            $entityManager = $this->getEntityManager($name);
106
107
            if ($entityManager->isOpen()) {
108
                $entityManager->close();
109
            }
110
111
            $entityManager = $this->create($name, $this->config->getEntityManagerConfig($name));
112
            $this->manager->setService($name, $entityManager);
113
        }
114
115
        return $this->getEntityManager($name);
116
    }
117
118
    /**
119
     * @param string                 $name
120
     * @param EntityManagerInterface $entityManager
121
     */
122
    public function setEntityManager(string $name, EntityManagerInterface $entityManager): void
123
    {
124
        $this->manager->setService($name, $entityManager);
125
    }
126
127
    /**
128
     * @param array $entityManagers
129
     */
130
    public function setEntityManagers(array $entityManagers): void
131
    {
132
        foreach ($entityManagers as $name => $entityManager) {
133
            $this->setEntityManager($name, $entityManager);
134
        }
135
    }
136
137
    /**
138
     * @param string      $name
139
     * @param array       $config
140
     * @param string|null $factoryClassName
141
     *
142
     * @return EntityManagerInterface
143
     *
144
     * @throws EntityManagerProviderException
145
     */
146
    private function create(string $name, array $config, string $factoryClassName = null): EntityManagerInterface
147
    {
148
        if (!$this->manager->has($name)) {
149
            // There is no manual entry for this entity manager. We can manually add it so we do not need
150
            // to explicitly define it each time with the 'entity_manager_manager'
151
            $this->manager->setFactory($name, $factoryClassName ?? EntityManagerFactory::class);
152
        }
153
154
        try {
155
            return $this->manager->build($name, $config);
156
        } catch (\Throwable $e) {
157
            throw new EntityManagerProviderException(
158
                sprintf('Failed to create entity manager \'%s\' from configuration: %s', $name, $e->getMessage()),
159
                $e->getCode(),
160
                $e
161
            );
162
        }
163
    }
164
}
165