Completed
Push — master ( cf321a...f177e6 )
by Thibaud
07:25
created

EntityManager::getRepository()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 25
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 6.9683

Importance

Changes 5
Bugs 0 Features 0
Metric Value
c 5
b 0
f 0
dl 0
loc 25
ccs 8
cts 14
cp 0.5714
rs 8.439
cc 5
eloc 13
nc 5
nop 1
crap 6.9683
1
<?php
2
3
/*
4
 * This file is part of Phraseanet SDK.
5
 *
6
 * (c) Alchemy <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace PhraseanetSDK;
13
14
use PhraseanetSDK\Http\APIGuzzleAdapter;
15
use PhraseanetSDK\AbstractRepository;
16
use PhraseanetSDK\Orders\OrderRepository;
17
use PhraseanetSDK\Search\SearchRepository;
18
use Psr\Log\LoggerInterface;
19
use Psr\Log\NullLogger;
20
21
class EntityManager
22
{
23
    /**
24
     * @var APIGuzzleAdapter
25
     */
26
    private $adapter;
27
28
    /**
29
     * @var LoggerInterface
30
     */
31
    private $logger;
32
33
    /**
34
     * @var AbstractRepository[]
35
     */
36
    private $repositories = array();
37
38
    /**
39
     * @param APIGuzzleAdapter $adapter
40
     * @param LoggerInterface $logger
41
     */
42 88
    public function __construct(
43
        APIGuzzleAdapter $adapter,
44
        LoggerInterface $logger = null
45
    ) {
46 88
        $this->adapter = $adapter;
47 88
        $this->logger = $logger ?: new NullLogger();
48 88
    }
49
50
    /**
51
     * @return LoggerInterface
52
     */
53
    public function getLogger()
54
    {
55
        return $this->logger;
56
    }
57
58
    /**
59
     * Return the client attached to this entity manager
60
     *
61
     * @return APIGuzzleAdapter
62
     */
63 86
    public function getAdapter()
64
    {
65 86
        return $this->adapter;
66
    }
67
68
    /**
69
     * Get a repository by its name
70
     *
71
     * @param  string $name
72
     * @return AbstractRepository
73
     */
74 17
    public function getRepository($name)
75
    {
76 17
        if (isset($this->repositories[$name])) {
77
            return $this->repositories[$name];
78
        }
79
80 17
        $className = ucfirst($name);
81 17
        $objectName = sprintf('\\PhraseanetSDK\\Repository\\%s', $className);
82
83 17
        if ($name == 'search') {
84
            return $this->repositories['search'] = new SearchRepository($this, $this->adapter);
85
        }
86
87 17
        if ($name == 'orders') {
88
            return $this->repositories['orders'] = new OrderRepository($this, $this->adapter);
89
        }
90
91 17
        if (!class_exists($objectName)) {
92
            throw new Exception\InvalidArgumentException(
93
                sprintf('Class %s does not exists', $objectName)
94
            );
95
        }
96
97 17
        return $this->repositories[$name] = new $objectName($this);
98
    }
99
}
100