ServiceManager   A
last analyzed

Complexity

Total Complexity 33

Size/Duplication

Total Lines 305
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 33
lcom 1
cbo 4
dl 0
loc 305
ccs 111
cts 111
cp 1
rs 9.3999
c 0
b 0
f 0

18 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 15 1
A getConfig() 0 4 1
A setFilesystem() 0 5 1
A getFilesystem() 0 7 2
A getConfigFilename() 0 10 2
A hasConfigFile() 0 5 2
A load() 0 5 2
A offsetGet() 0 16 3
A offsetSet() 0 7 2
A offsetUnset() 0 5 1
A getBuilder() 0 4 1
A setBuilder() 0 6 1
B setClient() 0 26 2
A getClient() 0 4 2
A removeClient() 0 6 1
A save() 0 6 2
B saveServiceGroup() 0 37 5
A deleteServiceGroup() 0 13 2
1
<?php
2
3
namespace Acquia\Rest;
4
5
use Acquia\Json\Json;
6
use Guzzle\Service\Builder\ServiceBuilder;
7
use Guzzle\Service\Client;
8
use Symfony\Component\Filesystem\Filesystem;
9
10
class ServiceManager extends \ArrayObject
11
{
12
    /**
13
     * @var \Guzzle\Common\Collection
14
     */
15
    protected $config;
16
17
    /**
18
     * @var array
19
     */
20
    protected $removed = array();
21
22
    /**
23
     * @var \Symfony\Component\Filesystem\Filesystem
24
     */
25
    protected $filesystem;
26
27
    /**
28
     * @param array $config
29
     */
30 48
    public function __construct(array $config = array())
31
    {
32
        $defaults = array(
33 48
            'conf_dir' => 'conf',
34 48
            'conf_files' => array(),
35 48
        );
36
37
        $required = array(
38 48
            'conf_dir',
39 48
            'conf_files',
40 48
        );
41
42 48
        $this->config = \Guzzle\Common\Collection::fromConfig($config, $defaults, $required);
43 48
        $this->config['conf_dir'] = rtrim($this->config['conf_dir'], '/\\');
44 48
    }
45
46
    /**
47
     * @return \Guzzle\Common\Collection
48
     */
49 3
    public function getConfig()
50
    {
51 3
        return $this->config;
52
    }
53
54
    /**
55
     * @param \Symfony\Component\Filesystem\Filesystem $filesystem
56
     *
57
     * @return \Acquia\Rest\ServiceManager
58
     */
59 3
    public function setFilesystem(Filesystem $filesystem)
60
    {
61 3
        $this->filesystem = $filesystem;
62 3
        return $this;
63
    }
64
65
    /**
66
     * @return \Symfony\Component\Filesystem\Filesystem
67
     */
68 15
    public function getFilesystem()
69
    {
70 15
        if (!isset($this->filesystem)) {
71 12
            $this->filesystem = new Filesystem();
72 12
        }
73 15
        return $this->filesystem;
74
    }
75
76
    /**
77
     * @param string $group
78
     *
79
     * @return string
80
     */
81 30
    public function getConfigFilename($group)
82
    {
83 30
        $conf_files = $this->config->get('conf_files');
84 30
        if (!isset($conf_files[$group])) {
85 30
            $filename = $this->config['conf_dir'] . '/' . $group . '.json';
86 30
            $conf_files[$group] = $filename;
87 30
        }
88 30
        $this->config->set('conf_files', $conf_files);
89 30
        return $conf_files[$group];
90
    }
91
92
    /**
93
     * @param string $name
94
     *
95
     * @return boolean
96
     */
97 24
    public function hasConfigFile($name)
98
    {
99 24
        $filename = $this->getConfigFilename($name);
100 24
        return is_file($filename) && is_readable($filename);
101
    }
102
103
    /**
104
     * @param string $group
105
     *
106
     * @return \Guzzle\Service\Builder\ServiceBuilder
107
     */
108 21
    public function load($group)
109
    {
110 21
        $confg = $this->hasConfigFile($group) ? $this->getConfigFilename($group) : array();
111 21
        return ServiceBuilder::factory($confg);
112
    }
113
114
    /**
115
     * @param string $group
116
     *
117
     * @return \Guzzle\Service\Builder\ServiceBuilder
118
     */
119 24
    public function offsetGet($group)
120
    {
121 24
        if (!isset($this[$group])) {
122
123
            // Load builder from file or instantiate an empty one.
124 21
            if ($this->hasConfigFile($group)) {
125 21
                $this[$group] = $this->load($group);
126 21
            } else {
127 3
                $this[$group] = ServiceBuilder::factory(array());
128
            }
129
130
            // Initialize the "removed" flag.
131 21
            $this->removed[$group] = array();
132 21
        }
133 24
        return parent::offsetGet($group);
134
    }
135
136
    /**
137
     * @param string $group
138
     * @param \Guzzle\Service\Builder\ServiceBuilder $builder
139
     */
140 27
    public function offsetSet($group, $builder)
141
    {
142 27
        if (!$builder instanceof ServiceBuilder) {
143 3
            throw new \UnexpectedValueException('Expecting value to be an instance of Guzzle\Service\Builder\ServiceBuilder');
144
        }
145 24
        parent::offsetSet($group, $builder);
146 24
    }
147
148
    /**
149
     * @param string $group
150
     */
151 9
    public function offsetUnset($group)
152
    {
153 9
        unset($this->removed[$group]);
154 9
        parent::offsetUnset($group);
155 9
    }
156
157
    /**
158
     * @param string $group
159
     *
160
     * @return \Guzzle\Service\Builder\ServiceBuilder
161
     */
162 12
    public function getBuilder($group)
163
    {
164 12
        return $this[$group];
165
    }
166
167
    /**
168
     * @param string $group
169
     * @param \Guzzle\Service\Builder\ServiceBuilder $builder
170
     *
171
     * @return \Acquia\Rest\ServiceManager
172
     */
173 6
    public function setBuilder($group, ServiceBuilder $builder)
174
    {
175 6
        $this[$group] = $builder;
176 6
        $this->removed[$group] = array();
177 6
        return $this;
178
    }
179
180
    /**
181
     * @param string $group
182
     * @param string $name
183
     * @param \Guzzle\Service\Client $client
184
     *
185
     * @return \Acquia\Rest\ServiceManager
186
     */
187 6
    public function setClient($group, $name, Client $client)
188
    {
189
        // Must also be service manager aware.
190 6
        if (!$client instanceof ServiceManagerAware) {
191 3
            throw new \UnexpectedValueException('Client must implement Acquia\Rest\ServiceManagerAware');
192
        }
193
194 3
        $builder = $this[$group];
195
196
        // Set the client in the service builder.
197 3
        $builder[$name] = $client;
198
199
        // This looks funky, but it is actually not overwriting the value we
200
        // just set. This snippet adds the builder config so that saving the
201
        // service will add this client as a service in the JSON file.
202
        // @see \Guzzle\Service\Builder\ServiceBuilder::set()
0 ignored issues
show
Unused Code Comprehensibility introduced by
38% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
203 3
        $builder[$name] = array(
204 3
            'class' => get_class($client),
205 3
            'params' => $client->getBuilderParams(),
206
        );
207
208
        // If the client was previously removed, unset the remove flag.
209 3
        unset($this->removed[$group][$name]);
210
211 3
        return $this;
212
    }
213
214
    /**
215
     * @param string $group
216
     * @param string $name
217
     *
218
     * @return \Guzzle\Service\Client
219
     */
220 6
    public function getClient($group, $name)
221
    {
222 6
        return isset($this[$group][$name]) ? $this[$group][$name] : null;
223
    }
224
225
    /**
226
     * @param string $group
227
     * @param string $name
228
     *
229
     * @return \Acquia\Rest\ServiceManager
230
     */
231 3
    public function removeClient($group, $name)
232
    {
233 3
        unset($this[$group][$name]);
234 3
        $this->removed[$group][$name] = $name;
235 3
        return $this;
236
    }
237
238
    /**
239
     * Writes all service group configurations to the backend.
240
     *
241
     * @param boolean $overwrite
242
     */
243 6
    public function save($overwrite = false)
244
    {
245 6
        foreach ($this as $group => $builder) {
246 6
            $this->saveServiceGroup($group, $overwrite);
247 6
        }
248 6
    }
249
250
    /**
251
     * @param string $group
252
     * @param boolean $overwrite
253
     *
254
     * @throws \Symfony\Component\Filesystem\Exception\IOException
255
     *
256
     * @see http://guzzlephp.org/webservice-client/using-the-service-builder.html#sourcing-from-a-json-document
257
     */
258 6
    public function saveServiceGroup($group, $overwrite = false)
259
    {
260 6
        $filename = $this->getConfigFilename($group);
261 6
        $hasConfigFile = $this->hasConfigFile($group);
262
263
        // This sucks, but it is the only way to get the builder config.
264
        // @todo Create a Guzzle issue to add a getBuilderConfig() method.
265 6
        $builder = $this[$group];
266 6
        $builderConfig = Json::decode($builder->serialize());
267
268
        // @todo Add validation.
269 6
        if (!$overwrite && $hasConfigFile) {
270 6
            $groupJson = file_get_contents($filename);
271 6
            $groupData = Json::decode($groupJson);
272 6
            $builderConfig = array_merge($groupData['services'], $builderConfig);
273 6
        }
274
275
        // Unset the services that are flagged to be removed then clear the
276
        // remove flag since action was taken.
277 6
        foreach ($this->removed[$group] as $name) {
278 3
            unset($builderConfig[$name]);
279 6
        }
280 6
        $this->removed[$group] = array();
281
282 6
        $json = Json::encode(array(
283 6
            'class' => get_class($builder),
284 6
            'services' => $builderConfig,
285 6
        ));
286
287 6
        $filesystem = $this->getFilesystem();
288
289 6
        if (!$hasConfigFile) {
290 3
            $filesystem->mkdir(dirname($filename), 0755);
291 3
        }
292
293 6
        $filesystem->dumpFile($filename, $json, 0600);
294 6
    }
295
296
    /**
297
     * @param string $group
298
     *
299
     * @throws \Symfony\Component\Filesystem\Exception\IOException
300
     */
301 6
    public function deleteServiceGroup($group)
302
    {
303 6
        $filename = $this->getConfigFilename($group);
304 6
        $this->getFilesystem()->remove($filename);
305
306
        // Unlike normal arrays, unset() will throw errors here if the key
307
        // doesn't exist.
308 6
        if (isset($this[$group])) {
309 6
            unset($this[$group]);
310 6
        }
311
312 6
        unset($this->removed[$group]);
313 6
    }
314
}
315