XabbuhXApiClientExtension::getNamespace()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2
Metric Value
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
2
3
/*
4
 * This file is part of the xAPI package.
5
 *
6
 * (c) Christian Flothmann <[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 Xabbuh\XApi\Bundle\ClientBundle\DependencyInjection;
13
14
use Symfony\Component\Config\FileLocator;
15
use Symfony\Component\DependencyInjection\ContainerBuilder;
16
use Symfony\Component\DependencyInjection\Definition;
17
use Symfony\Component\DependencyInjection\Extension\Extension;
18
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
19
use Symfony\Component\DependencyInjection\Reference;
20
21
/**
22
 * XabbuhXApiClientBundle extension.
23
 *
24
 * @author Christian Flothmann <[email protected]>
25
 */
26
class XabbuhXApiClientExtension extends Extension
27
{
28
    /**
29
     * {@inheritDoc}
30
     */
31 4
    public function load(array $configs, ContainerBuilder $container)
32
    {
33 4
        $configuration = new Configuration();
34 4
        $config = $this->processConfiguration($configuration, $configs);
35
36 4
        $loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
37 4
        $loader->load('client_manager.xml');
38
39 4
        $this->processClients($config['clients'], $container);
40 4
    }
41
42
    /**
43
     * {@inheritDoc}
44
     */
45
    public function getXsdValidationBasePath()
46
    {
47
        return __DIR__.'/../Resources/config/schema';
0 ignored issues
show
Bug Best Practice introduced by
The return type of return __DIR__ . '/../Resources/config/schema'; (string) is incompatible with the return type of the parent method Symfony\Component\Depend...etXsdValidationBasePath of type boolean.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

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

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
48
    }
49
50
    /**
51
     * {@inheritDoc}
52
     */
53
    public function getNamespace()
54
    {
55
        return 'http://xabbuh.de/schema/dic/xabbuh/xapi-client';
56
    }
57
58
    /**
59
     * {@inheritDoc}
60
     */
61 1
    public function getAlias()
62
    {
63 1
        return 'xabbuh_xapi_client';
64
    }
65
66 4
    protected function processClients($clients, ContainerBuilder $container)
67
    {
68 4
        $clientsParameter = array();
69
70 4
        foreach ($clients as $name => $client) {
71 2
            $httpClientId = 'xabbuh_xapi_client.http_client.'.$name;
72 2
            $httpClientDefinition = new Definition('Guzzle\Http\Client');
73 2
            $httpClientDefinition->setPublic(false);
74 2
            $container->setDefinition($httpClientId, $httpClientDefinition);
75
76 2
            $id = 'xabbuh_xapi_client.client.'.$name;
77 2
            $definition = new Definition(
78 2
                'Xabbuh\XApi\Client\XApiClient',
79
                array(
80 2
                    new Reference($httpClientId),
81 2
                    new Reference('jms_serializer.serializer'),
82 2
                    $client['version']
83 2
                )
84 2
            );
85 2
            $container->setDefinition($id, $definition);
86
87 2
            $clientsParameter[$name] = $id;
88 4
        }
89
90 4
        $container->setParameter('xabbuh_xapi_client.clients', $clientsParameter);
91 4
    }
92
}
93