-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathManagerRegistry.php
More file actions
66 lines (53 loc) · 1.76 KB
/
Copy pathManagerRegistry.php
File metadata and controls
66 lines (53 loc) · 1.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
<?php
namespace Cristal\ApiWrapper\Bridges\Symfony;
use Cristal\ApiWrapper\Bridges\Symfony\Exception\ConnectionNotFoundException;
use Cristal\ApiWrapper\Bridges\Symfony\Exception\RepositoryNotFoundException;
use Symfony\Component\DependencyInjection\ContainerInterface;
class ManagerRegistry
{
/**
* @var iterable|ConnectionInterface[]
*/
private $connections;
/**
* @var ContainerInterface
*/
private $container;
public function __construct(ContainerInterface $container)
{
$this->container = $container;
}
public function getRepository(string $entityName): Repository
{
$metadata = $this->getMetadataFromClass($entityName);
if(null === $metadata){
throw new RepositoryNotFoundException(sprintf('Unable to find API repository for %s.', $entityName));
}
return $this->container->get($metadata->getRepositoryClass())->setupRepository($entityName);
}
public function getMetadataFromClass(string $entityName): ?ClassMetadata
{
$metadata = new ClassMetadata($entityName);
if(null === $metadata->getEntity()){
return null;
}
return $metadata;
}
/**
* @throws ConnectionNotFoundException
*/
public function getConnection(string $connectionName)
{
if(!isset($this->connections[$connectionName])){
throw new ConnectionNotFoundException(
sprintf('Unable to find connection named %s.', $connectionName)
);
}
return $this->connections[$connectionName];
}
public function addConnection(ConnectionInterface $connection): ManagerRegistry
{
$this->connections[$connection->getName()] = $connection;
return $this;
}
}