Completed
Pull Request — master (#165)
by Paul
03:21
created

AbstractConfig   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3
Metric Value
wmc 6
lcom 0
cbo 3
dl 0
loc 71
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getConfigurationReference() 0 4 1
processConfiguration() 0 1 ?
A mergeConfiguration() 0 4 1
A getAlias() 0 10 2
A getParameters() 0 6 2
1
<?php
2
/**
3
 * This file is part of the PPI Framework.
4
 *
5
 * @copyright   Copyright (c) 2011-2016 Paul Dragoonis <[email protected]>
6
 * @license     http://opensource.org/licenses/mit-license.php MIT
7
 *
8
 * @link        http://www.ppi.io
9
 */
10
11
namespace PPI\Framework\ServiceManager\Config;
12
13
use PPI\Framework\Config\ConfigurationProviderInterface;
14
use Zend\ServiceManager\Config;
15
use Zend\ServiceManager\ServiceManager;
16
use Zend\Stdlib\ArrayUtils;
17
18
/**
19
 * AbstractConfig class.
20
 *
21
 * @author     Vítor Brandão <[email protected]>
22
 */
23
abstract class AbstractConfig extends Config implements ConfigurationProviderInterface
24
{
25
    /**
26
     * Returns the mandatory prefix to use when using YAML.
27
     *
28
     * This convention is to remove the "Config" postfix from the class
29
     * name and then lowercase and underscore the result. So:
30
     *
31
     *     AcmeHelloConfig
32
     *
33
     * becomes
34
     *
35
     *     acme_hello
36
     *
37
     * This can be overridden in a sub-class to specify the alias manually.
38
     *
39
     * @throws \BadMethodCallException When the extension name does not follow conventions
40
     *
41
     * @return string The alias
42
     */
43
    public function getAlias()
44
    {
45
        $className = get_class($this);
46
        if (substr($className, -6) != 'Config') {
47
            throw new \BadMethodCallException('This Config class does not follow the naming convention; you must overwrite the getAlias() method.');
48
        }
49
        $classBaseName = substr(strrchr($className, '\\'), 1, -6);
50
51
        return strtolower($classBaseName);
52
    }
53
54
    /**
55
     * Returns the configuration reference for this component.
56
     *
57
     * @returns array
58
     */
59
    public function getConfigurationReference()
60
    {
61
        return array($this->getAlias() => array());
62
    }
63
64
    /**
65
     * @param ServiceManager $serviceManager
66
     *
67
     * @return array
68
     */
69
    public function getParameters(ServiceManager $serviceManager)
70
    {
71
        $config = $serviceManager->get('Config');
72
73
        return isset($config['parameters']) ? $config['parameters'] : array();
74
    }
75
76
    /**
77
     * Process an array with the application configuration.
78
     *
79
     * @param array                               $config
80
     * @param \Zend\ServiceManager\ServiceManager $serviceManager
81
     *
82
     * @return array
83
     */
84
    abstract protected function processConfiguration(array $config, ServiceManager $serviceManager = null);
85
86
    /**
87
     * Merges configuration.
88
     */
89
    protected function mergeConfiguration(array $defaults, array $config)
90
    {
91
        return ArrayUtils::merge($defaults, $config);
92
    }
93
}
94