Completed
Push — develop ( e149d8...a7cea2 )
by Lucas
08:25
created

GravitonDocumentExtension   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 56.25%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
lcom 0
cbo 2
dl 0
loc 75
ccs 18
cts 32
cp 0.5625
rs 10
c 1
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getConfigDir() 0 4 1
B prepend() 0 53 7
1
<?php
2
/**
3
 * manage and load bundle config.
4
 */
5
6
namespace Graviton\DocumentBundle\DependencyInjection;
7
8
use Graviton\BundleBundle\DependencyInjection\GravitonBundleExtension;
9
use Symfony\Component\DependencyInjection\ContainerBuilder;
10
11
/**
12
 * This is the class that loads and manages your bundle configuration
13
 *
14
 * To learn more see {@link http://scm.to/004w}
15
 *
16
 * @author   List of contributors <https://github.com/libgraviton/graviton/graphs/contributors>
17
 * @license  http://opensource.org/licenses/gpl-license.php GNU Public License
18
 * @link     http://swisscom.ch
19
 */
20
class GravitonDocumentExtension extends GravitonBundleExtension
21
{
22
    /**
23
     * {@inheritDoc}
24
     *
25
     * @return string
26
     */
27 1
    public function getConfigDir()
28
    {
29 1
        return __DIR__ . '/../Resources/config';
30
    }
31
32
    /**
33
     * {@inheritDoc}
34
     *
35
     * Overwrite mongodb config from parent in cloud case.
36
     *
37
     * @param ContainerBuilder $container instance
38
     *
39
     * @return void
40
     */
41 1
    public function prepend(ContainerBuilder $container)
0 ignored issues
show
Coding Style introduced by
prepend uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
42
    {
43 1
        parent::prepend($container);
44
45
        /** [nue]
46
         * this is a workaround for a new symfony feature:
47
         * https://github.com/symfony/symfony/issues/7555
48
         *
49
         * we *need* to be able to override any param with our env variables..
50
         * so we do again, what the kernel did already here.. ;-)
51
         *
52
         * Since fabpot seems to have said bye to this feature we are
53
         * re-implementing it here. We are also adding some fancy json
54
         * parsing for hashes and arrays while at it.
55
         *
56
         * @todo move this out of file bundle as it is much more global
57
         * @todo add proper documentation on this "feature" to a README
58
         */
59 1
        foreach ($_SERVER as $key => $value) {
60 1
            if (0 === strpos($key, 'SYMFONY__')) {
61
62
                if (substr($value, 0, 1) == '[' || substr($value, 0, 1) == '{') {
63
                    $value = json_decode($value, true);
64
                    if (JSON_ERROR_NONE !== json_last_error()) {
65
                        throw new \RuntimeException(
66
                            sprintf('error "%s" in env variable "%s"', json_last_error_msg(), $key)
67
                        );
68
                    }
69
                }
70
71
                $container->setParameter(strtolower(str_replace('__', '.', substr($key, 9))), $value);
72
            }
73 1
        }
74
75
        // grab mongo config directly from vcap...
76 1
        $services = getenv('VCAP_SERVICES');
77 1
        if (!empty($services)) {
78
            $services = json_decode($services, true);
79
            $mongo = $services['mongodb'][0]['credentials'];
80
81
            $container->setParameter('mongodb.default.server.uri', $mongo['uri']);
82
            $container->setParameter('mongodb.default.server.db', $mongo['database']);
83
        } else {
84 1
            $container->setParameter(
85 1
                'mongodb.default.server.uri',
86 1
                $container->getParameter('graviton.mongodb.default.server.uri')
87 1
            );
88 1
            $container->setParameter(
89 1
                'mongodb.default.server.db',
90 1
                $container->getParameter('graviton.mongodb.default.server.db')
91 1
            );
92
        }
93 1
    }
94
}
95