Completed
Pull Request — master (#76)
by Thibaud
04:14
created

EntityManager   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 63.64%

Importance

Changes 10
Bugs 0 Features 0
Metric Value
wmc 10
c 10
b 0
f 0
lcom 1
cbo 4
dl 0
loc 85
ccs 14
cts 22
cp 0.6364
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getLogger() 0 4 1
A getAdapter() 0 4 1
A __construct() 0 4 2
A getClient() 0 4 1
B getRepository() 0 25 5
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\ApiClient;
15
use PhraseanetSDK\Http\APIGuzzleAdapter;
16
use PhraseanetSDK\AbstractRepository;
17
use PhraseanetSDK\Http\Client;
18
use PhraseanetSDK\Orders\OrderRepository;
19
use PhraseanetSDK\Search\SearchRepository;
20
use Psr\Log\LoggerInterface;
21
use Psr\Log\NullLogger;
22
23
class EntityManager
24
{
25
    /**
26
     * @var APIGuzzleAdapter
27
     */
28
    private $client;
29
30
    /**
31
     * @var LoggerInterface
32
     */
33
    private $logger;
34
35
    /**
36
     * @var AbstractRepository[]
37
     */
38
    private $repositories = array();
39
40
    /**
41
     * @param ApiClient $client
42 88
     * @param LoggerInterface $logger
43
     */
44
    public function __construct(ApiClient $client, LoggerInterface $logger = null) {
45
        $this->client = $client;
0 ignored issues
show
Documentation Bug introduced by
It seems like $client of type object<PhraseanetSDK\Http\ApiClient> is incompatible with the declared type object<PhraseanetSDK\Http\APIGuzzleAdapter> of property $client.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
46 88
        $this->logger = $logger ?: new NullLogger();
47 88
    }
48 88
49
    /**
50
     * @return LoggerInterface
51
     */
52
    public function getLogger()
53
    {
54
        return $this->logger;
55
    }
56
57
    /**
58
     * Return the client attached to this entity manager
59
     *
60
     * @return APIGuzzleAdapter
61
     * @deprecated This method will be removed in the next major release of the SDK. Use EntityManager::getClient().
62
     */
63 86
    public function getAdapter()
64
    {
65 86
        return $this->getClient();
66
    }
67
68
    /**
69
     * @return ApiClient
70
     */
71
    public function getClient()
72
    {
73
        return $this->client;
74 17
    }
75
76 17
    /**
77
     * Get a repository by its name
78
     *
79
     * @param  string $name
80 17
     * @return AbstractRepository
81 17
     */
82
    public function getRepository($name)
83 17
    {
84
        if (isset($this->repositories[$name])) {
85
            return $this->repositories[$name];
86
        }
87 17
88
        $className = ucfirst($name);
89
        $objectName = sprintf('\\PhraseanetSDK\\Repository\\%s', $className);
90
91 17
        if ($name == 'search') {
92
            return $this->repositories['search'] = new SearchRepository($this, $this->client);
0 ignored issues
show
Unused Code introduced by
The call to SearchRepository::__construct() has too many arguments starting with $this->client.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
93
        }
94
95
        if ($name == 'orders') {
96
            return $this->repositories['orders'] = new OrderRepository($this, $this->client);
0 ignored issues
show
Unused Code introduced by
The call to OrderRepository::__construct() has too many arguments starting with $this->client.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
97 17
        }
98
99
        if (!class_exists($objectName)) {
100
            throw new Exception\InvalidArgumentException(
101
                sprintf('Class %s does not exists', $objectName)
102
            );
103
        }
104
105
        return $this->repositories[$name] = new $objectName($this);
106
    }
107
}
108