AnnotationsServiceProvider   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 5
dl 0
loc 60
ccs 30
cts 30
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B register() 0 37 5
A configureDoctrine() 0 12 4
1
<?php
2
3
namespace Skalpa\Silex\Doctrine;
4
5
use Doctrine\Common\Annotations\AnnotationReader;
6
use Doctrine\Common\Annotations\AnnotationRegistry;
7
use Doctrine\Common\Annotations\CachedReader;
8
use Doctrine\Common\Annotations\SimpleAnnotationReader;
9
use Doctrine\Common\Cache\ArrayCache;
10
use Pimple\Container;
11
use Pimple\ServiceProviderInterface;
12
13
/**
14
 * Doctrine annotations service provider for Silex/Pimple
15
 */
16
class AnnotationsServiceProvider implements ServiceProviderInterface
17
{
18
    private static $isConfigured = false;
19
20
    /**
21
     * Register the provided services.
22
     *
23
     * @param Container $container
24
     */
25 7
    public function register(Container $container)
26
    {
27 7
        $container['annotations.use_simple_reader']   = false;
28 7
        $container['annotations.register_autoloader'] = true;
29 7
        $container['annotations.debug']               = isset($container['debug']) ? $container['debug'] : false;
30 7
        $container['annotations.ignored_names']       = [];
31
32
        $container['annotations'] = function (Container $container) {
33 7
            $reader = $container['annotations.use_simple_reader'] ? $container['annotations.simple_reader'] : $container['annotations.reader'];
34
35 7
            if (isset($container['annotations.cache'])) {
36 5
                if (is_string($cache = $container['annotations.cache'])) {
37 1
                    $cache = $container[$cache];
38 1
                }
39
40 5
                return new CachedReader($reader, $cache, $container['annotations.debug']);
41
            }
42
43 2
            return $reader;
44
        };
45
46
        $container['annotations.cache'] = function () {
47 3
            return new ArrayCache();
48
        };
49
50
        $container['annotations.simple_reader'] = function (Container $container) {
51 1
            $this->configureDoctrine($container);
52
53 1
            return new SimpleAnnotationReader();
54
        };
55
56 6
        $container['annotations.reader'] = function (Container $container) {
57 6
            $this->configureDoctrine($container);
58
59 6
            return new AnnotationReader();
60
        };
61 7
    }
62
63 7
    private function configureDoctrine(Container $container)
64
    {
65 7
        if (!self::$isConfigured) {
66 3
            self::$isConfigured = true;
67 3
            if ($container['annotations.register_autoloader']) {
68 2
                AnnotationRegistry::registerLoader('class_exists');
69 2
            }
70 3
            foreach ($container['annotations.ignored_names'] as $name) {
71 1
                AnnotationReader::addGlobalIgnoredName($name);
72 3
            }
73 3
        }
74 7
    }
75
}
76