Completed
Pull Request — master (#64)
by Thibaud
03:51
created

Application   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 182
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Test Coverage

Coverage 92.31%

Importance

Changes 12
Bugs 0 Features 0
Metric Value
wmc 22
c 12
b 0
f 0
lcom 1
cbo 9
dl 0
loc 182
ccs 60
cts 65
cp 0.9231
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
C create() 0 22 7
A assertValidToken() 0 6 2
A __construct() 0 6 1
A setExtendedMode() 0 4 1
A getOauth2Connector() 0 8 2
A getUploader() 0 10 2
A getEntityManager() 0 13 2
A getMonitor() 0 10 2
A getAdapter() 0 4 1
A getAdapterByToken() 0 13 2
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\GuzzleAdapter;
15
use PhraseanetSDK\Exception\InvalidArgumentException;
16
use PhraseanetSDK\Http\ConnectedGuzzleAdapter;
17
use PhraseanetSDK\Http\APIGuzzleAdapter;
18
use Psr\Log\NullLogger;
19
20
/**
21
 * Phraseanet SDK Application
22
 */
23
class Application implements ApplicationInterface
24
{
25
    /**
26
     * Creates the application.
27
     *
28
     * @param array $config
29
     * @param GuzzleAdapter $adapter
30
     * @return Application
31
     */
32 5
    public static function create(array $config, GuzzleAdapter $adapter = null)
33
    {
34 5
        foreach (array('client-id', 'secret') as $key) {
35 5
            if (!isset($config[$key]) || !is_string($config[$key])) {
36 2
                throw new InvalidArgumentException(sprintf('Missing or invalid parameter "%s"', $key));
37
            }
38 4
        }
39
40 3
        if (null === $adapter) {
41 2
            if (!isset($config['url']) || !is_string($config['url'])) {
42 1
                throw new InvalidArgumentException(sprintf('Missing or invalid parameter "url"'));
43
            }
44
45 1
            $adapter = GuzzleAdapter::create($config['url']);
46 1
        }
47
48 2
        return new static(
49 2
            $adapter,
50 2
            $config['client-id'],
51 2
            $config['secret']
52 2
        );
53
    }
54
55
    /**
56
     * @param $token
57
     */
58 5
    private static function assertValidToken($token)
59
    {
60 5
        if ('' === trim($token)) {
61 2
            throw new InvalidArgumentException('Token can not be empty.');
62
        }
63 3
    }
64
65
    /**
66
     * @var GuzzleAdapter
67
     */
68
    private $adapter;
69
70
    /**
71
     * @var string Application client ID. Used by Oauth2Connector
72
     */
73
    private $clientId;
74
75
    /**
76
     * @var string Application secret. Used by Oauth2Connector
77
     */
78
    private $secret;
79
80
    /**
81
     * @var OAuth2Connector
82
     */
83
    private $connector;
84
85
    /**
86
     * @var EntityManager[]
87
     */
88
    private $entityManagers = array();
89
90
    /**
91
     * @var APIGuzzleAdapter[]
92
     */
93
    private $adapters = array();
94
95
    /**
96
     * @var Uploader[]
97
     */
98
    private $uploaders = array();
99
100
    /**
101
     * @var Monitor[]
102
     */
103
    private $monitors = array();
104
105 8
    public function __construct(GuzzleAdapter $adapter, $clientId, $secret)
106
    {
107 8
        $this->adapter = $adapter;
108 8
        $this->clientId = $clientId;
109 8
        $this->secret = $secret;
110 8
    }
111
112
    /**
113
     * Activate extended graph object by adding required accept headers.
114
     * This results in bigger response message but less requests to get
115
     * relation of queried object.
116
     *
117
     * @param $mode
118
     */
119
    public function setExtendedMode($mode)
120
    {
121
        $this->adapter->setExtended($mode);
122
    }
123
124
    /**
125
     * {@inheritdoc}
126
     */
127 1
    public function getOauth2Connector()
128
    {
129 1
        if ($this->connector === null) {
130 1
            $this->connector = new OAuth2Connector($this->adapter, $this->clientId, $this->secret);
131 1
        }
132
133 1
        return $this->connector;
134
    }
135
136
    /**
137
     * {@inheritdoc}
138
     */
139 1
    public function getUploader($token)
140
    {
141 1
        self::assertValidToken($token);
142
143 1
        if (!isset($this->uploaders[$token])) {
144 1
            $this->uploaders[$token] = new Uploader($this->getAdapterByToken($token), $this->getEntityManager($token));
145 1
        }
146
147 1
        return $this->uploaders[$token];
148
    }
149
150
    /**
151
     * {@inheritdoc}
152
     */
153 4
    public function getEntityManager($token)
154
    {
155 4
        self::assertValidToken($token);
156
157 2
        if (!isset($this->entityManagers[$token])) {
158 2
            $this->entityManagers[$token] = new EntityManager(
159 2
                $this->getAdapterByToken($token),
160 2
                new NullLogger()
161 2
            );
162 2
        }
163
164 2
        return $this->entityManagers[$token];
165
    }
166
167
    /**
168
     * {@inheritdoc}
169
     */
170 1
    public function getMonitor($token)
171
    {
172 1
        self::assertValidToken($token);
173
174 1
        if (!isset($this->monitors[$token])) {
175 1
            $this->monitors[$token] = new Monitor($this->getAdapterByToken($token));
176 1
        }
177
178 1
        return $this->monitors[$token];
179
    }
180
181
    /**
182
     * Returns the guzzle adapter
183
     *
184
     * @return GuzzleAdapter
185
     */
186
    public function getAdapter()
187
    {
188
        return $this->adapter;
189
    }
190
191 3
    private function getAdapterByToken($token)
192
    {
193 3
        if (!isset($this->adapters[$token])) {
194 3
            $this->adapters[$token] = new APIGuzzleAdapter(
195 3
                new ConnectedGuzzleAdapter(
196 3
                    $token,
197 3
                    $this->adapter
198 3
                )
199 3
            );
200 3
        }
201
202 3
        return $this->adapters[$token];
203
    }
204
}
205