Completed
Pull Request — master (#20)
by Kevin
03:55
created

LinkValueJarvisExtension::load()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 10
ccs 0
cts 7
cp 0
rs 9.4285
c 1
b 0
f 0
cc 1
eloc 6
nc 1
nop 2
crap 2
1
<?php
2
3
namespace LinkValue\JarvisBundle\DependencyInjection;
4
5
use Symfony\Component\DependencyInjection\ContainerBuilder;
6
use Symfony\Component\Config\FileLocator;
7
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
8
use Symfony\Component\DependencyInjection\Loader;
9
use Symfony\Component\DependencyInjection\Reference;
10
11
/**
12
 * This is the class that loads and manages LinkValueJarvisBundle configuration.
13
 */
14
class LinkValueJarvisExtension extends Extension
15
{
16
    /**
17
     * {@inheritdoc}
18
     */
19
    public function load(array $configs, ContainerBuilder $container)
20
    {
21
        $configuration = new Configuration();
22
        $config = $this->processConfiguration($configuration, $configs);
23
24
        $this->registerClients($config, $container);
25
26
        $loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
27
        $loader->load('services.yml');
28
    }
29
30
    /**
31
     * Register each client as service, such as "link_value_mobile_notif.clients.the_client_type.my_custom_client_name"
32
     *
33
     * @param array $config
34
     * @param ContainerBuilder $container
35
     */
36
    private function registerClients(array $config, ContainerBuilder $container)
37
    {
38
        foreach ($config['clients'] as $clientType => $clients) {
39
            $clientFQCN = ($clientType == 'apns') ? 'LinkValue\JarvisBundle\Client\ApnsClient' : 'LinkValue\JarvisBundle\Client\GcmClient';
40
41
            foreach ($clients as $clientName => $clientConfig) {
42
                $params = isset($clientConfig['params']) ? $clientConfig['params'] : array();
43
                $services = isset($clientConfig['services']) ? $clientConfig['services'] : array();
44
45
                // The final client name is a concatenation of the client type (apns/gcm) and the client name (defined by user) separated by a point '.'
46
                $clientName = sprintf('%s.%s', $clientType, $clientName);
47
48
                // Register client with required stuff
49
                $client = $container->register(sprintf('link_value_mobile_notif.clients.%s', $clientName), $clientFQCN)
50
                    ->addMethodCall('setUp', array($params))
51
                    ->addTag('link_value_mobile_notif.client', array('name' => $clientName))
52
                ;
53
54
                // Set optional logger
55
                if(!empty($services['logger'])) {
56
                    $client->addMethodCall('setLogger', array(new Reference($services['logger'])));
57
                }
58
59
                // Set optional profiler
60
                if(!empty($services['profiler'])) {
61
                    $client->addMethodCall('setClientProfiler', array(new Reference($services['profiler'])));
62
                }
63
            }
64
        }
65
    }
66
}
67