GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

ServiceMapPass   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 72%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
c 1
b 0
f 0
lcom 1
cbo 3
dl 0
loc 46
ccs 18
cts 25
cp 0.72
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A serialize() 0 4 1
A unserialize() 0 4 1
B process() 0 22 5
1
<?php
2
3
namespace JMS\SerializerBundle\DependencyInjection\Compiler;
4
5
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
6
use Symfony\Component\DependencyInjection\ContainerBuilder;
7
use Symfony\Component\DependencyInjection\Definition;
8
use Symfony\Component\DependencyInjection\Reference;
9
10
/**
11
 * This pass allows you to easily create service maps.
12
 *
13
 * ```php
14
 *    $container->addCompilerPass(new ServiceMapPass(
15
 *        'jms_serializer.visitor',
16
 *        'format',
17
 *        function(ContainerBuilder $container, Definition $def) {
18
 *            $container->getDefinition('jms_serializer')
19
 *                ->addArgument($def);
20
 *        }
21
 *    ));
22
 * ```
23
 *
24
 * In the example above, we convert the visitors into a map service.
25
 *
26
 * @author Johannes M. Schmitt <[email protected]>
27
 */
28
class ServiceMapPass implements CompilerPassInterface, \Serializable
29
{
30
    private $tagName;
31
    private $keyAttributeName;
32
    private $callable;
33
34 25
    public function __construct($tagName, $keyAttributeName, $callable)
35
    {
36 25
        $this->tagName = $tagName;
37 25
        $this->keyAttributeName = $keyAttributeName;
38 25
        $this->callable = $callable;
39 25
    }
40
41 25
    public function process(ContainerBuilder $container)
42
    {
43 25
        if (!is_callable($this->callable)) {
44
            throw new \RuntimeException('The callable is invalid. If you had serialized this pass, the original callable might not be available anymore.');
45
        }
46
47 25
        $serviceMap = array();
48 25
        foreach ($container->findTaggedServiceIds($this->tagName) as $id => $tags) {
49 25
            foreach ($tags as $tag) {
50 25
                if (!isset($tag[$this->keyAttributeName])) {
51
                    throw new \RuntimeException(sprintf('The attribute "%s" must be set for service "%s" and tag "%s".', $this->keyAttributeName, $id, $this->tagName));
52
                }
53
54 25
                $serviceMap[$tag[$this->keyAttributeName]] = new Reference($id);
55 25
            }
56 25
        }
57
58 25
        $def = new Definition('PhpCollection\Map');
59 25
        $def->addArgument($serviceMap);
60
61 25
        call_user_func($this->callable, $container, $def);
62 25
    }
63
64
    public function serialize()
65
    {
66
        return serialize(array($this->tagName, $this->keyAttributeName));
67
    }
68
69
    public function unserialize($str)
70
    {
71
        list($this->tagName, $this->keyAttributeName) = unserialize($str);
72
    }
73
}
74