ServiceBuilderLoader::build()   D
last analyzed

Complexity

Conditions 13
Paths 236

Size

Total Lines 44
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 44
rs 4.022
c 0
b 0
f 0
cc 13
eloc 18
nc 236
nop 2

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Guzzle\Service\Builder;
4
5
use Guzzle\Service\AbstractConfigLoader;
6
use Guzzle\Service\Exception\ServiceNotFoundException;
7
8
/**
9
 * Service builder config loader
10
 */
11
class ServiceBuilderLoader extends AbstractConfigLoader
12
{
13
    protected function build($config, array $options)
14
    {
15
        // A service builder class can be specified in the class field
16
        $class = !empty($config['class']) ? $config['class'] : __NAMESPACE__ . '\\ServiceBuilder';
17
18
        // Account for old style configs that do not have a services array
19
        $services = isset($config['services']) ? $config['services'] : $config;
20
21
        // Validate the configuration and handle extensions
22
        foreach ($services as $name => &$service) {
23
24
            $service['params'] = isset($service['params']) ? $service['params'] : array();
25
26
            // Check if this client builder extends another client
27
            if (!empty($service['extends'])) {
28
29
                // Make sure that the service it's extending has been defined
30
                if (!isset($services[$service['extends']])) {
31
                    throw new ServiceNotFoundException(
32
                        "{$name} is trying to extend a non-existent service: {$service['extends']}"
33
                    );
34
                }
35
36
                $extended = &$services[$service['extends']];
37
38
                // Use the correct class attribute
39
                if (empty($service['class'])) {
40
                    $service['class'] = isset($extended['class']) ? $extended['class'] : '';
41
                }
42
                if ($extendsParams = isset($extended['params']) ? $extended['params'] : false) {
43
                    $service['params'] = $service['params'] + $extendsParams;
44
                }
45
            }
46
47
            // Overwrite default values with global parameter values
48
            if (!empty($options)) {
49
                $service['params'] = $options + $service['params'];
50
            }
51
52
            $service['class'] = isset($service['class']) ? $service['class'] : '';
53
        }
54
55
        return new $class($services);
56
    }
57
58
    protected function mergeData(array $a, array $b)
59
    {
60
        $result = $b + $a;
61
62
        // Merge services using a recursive union of arrays
63
        if (isset($a['services']) && $b['services']) {
64
65
            // Get a union of the services of the two arrays
66
            $result['services'] = $b['services'] + $a['services'];
67
68
            // Merge each service in using a union of the two arrays
69
            foreach ($result['services'] as $name => &$service) {
70
71
                // By default, services completely override a previously defined service unless it extends itself
72
                if (isset($a['services'][$name]['extends'])
73
                    && isset($b['services'][$name]['extends'])
74
                    && $b['services'][$name]['extends'] == $name
75
                ) {
76
                    $service += $a['services'][$name];
77
                    // Use the `extends` attribute of the parent
78
                    $service['extends'] = $a['services'][$name]['extends'];
79
                    // Merge parameters using a union if both have parameters
80
                    if (isset($a['services'][$name]['params'])) {
81
                        $service['params'] += $a['services'][$name]['params'];
82
                    }
83
                }
84
            }
85
        }
86
87
        return $result;
88
    }
89
}
90