LinkValueMobileNotifExtension   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 7

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 9
lcom 0
cbo 7
dl 0
loc 53
ccs 0
cts 27
cp 0
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A load() 0 10 1
C registerClients() 0 30 8
1
<?php
2
3
namespace LinkValue\MobileNotifBundle\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 LinkValueMobileNotifBundle configuration.
13
 */
14
class LinkValueMobileNotifExtension 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\MobileNotifBundle\Client\ApnsClient' : 'LinkValue\MobileNotifBundle\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