LocasticLoggasticExtension::getLoggablePaths()   B
last analyzed

Complexity

Conditions 11
Paths 33

Size

Total Lines 51
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 11
eloc 25
c 0
b 0
f 0
nc 33
nop 2
dl 0
loc 51
rs 7.3166

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Locastic\Loggastic\DependencyInjection;
4
5
use Locastic\Loggastic\Metadata\Extractor\XmlLoggableExtractor;
6
use Locastic\Loggastic\Metadata\Extractor\YamlLoggableExtractor;
7
use Symfony\Component\Config\FileLocator;
8
use Symfony\Component\Config\Resource\DirectoryResource;
9
use Symfony\Component\DependencyInjection\ContainerBuilder;
10
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
11
use Symfony\Component\DependencyInjection\Extension\Extension;
12
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
13
use Symfony\Component\Finder\Finder;
14
15
final class LocasticLoggasticExtension extends Extension
16
{
17
    /**
18
     * {@inheritdoc}
19
     */
20
    public function load(array $configs, ContainerBuilder $container): void
21
    {
22
        $loader = new YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
23
24
        $configuration = new Configuration();
25
        $config = $this->processConfiguration($configuration, $configs);
26
27
        $loader->load('commands.yaml');
28
        $loader->load('identifier.yaml');
29
        $loader->load('listeners.yaml');
30
        $loader->load('logger.yaml');
31
        $loader->load('message_dispatcher.yaml');
32
        $loader->load('message_handlers.yaml');
33
        $loader->load('serializer.yaml');
34
35
        $container->setParameter('locastic_activity_log.identifier_extractor', $config['identifier_extractor'] ?? true);
36
37
        $container->setParameter('locastic_activity_log.elasticsearch_host', $config['elastic_host']);
38
        $container->setParameter('locastic_activity_log.elastic_date_detection', $config['elastic_date_detection']);
39
        $container->setParameter('locastic_activity_log.elastic_dynamic_date_formats', $config['elastic_dynamic_date_formats']);
40
41
        $container->setParameter('locastic_activity_log.activity_log.elastic_properties', $config['activity_log']['elastic_properties']);
42
        $container->setParameter('locastic_activity_log.current_data_tracker.elastic_properties', $config['current_data_tracker']['elastic_properties']);
43
44
        $loader->load('elastic.yaml');
45
46
        // load loggable resources
47
        $loggableClasses = $this->getLoggablePaths($container, $config);
48
49
        $loader->load('loggable_context.yaml');
50
        $container->setParameter('locastic_activity_log.dir.loggable_classes', $loggableClasses['dir']);
51
52
        $loader->load('metadata.yaml');
53
        $container->getDefinition(XmlLoggableExtractor::class)->replaceArgument(0, $loggableClasses['xml']);
54
        $container->getDefinition(YamlLoggableExtractor::class)->replaceArgument(0, $loggableClasses['yml']);
55
56
        if($config['default_doctrine_subscriber']) {
57
            $loader->load('activity_log_doctrine_subscriber.yaml');
58
        }
59
    }
60
61
    private function getLoggablePaths(ContainerBuilder $container, array $config): array
62
    {
63
        $loggableClasses = ['yml' => [], 'xml' => [], 'dir' => []];
64
65
        if (!array_key_exists('loggable_paths', $config)) {
66
            return $loggableClasses;
67
        }
68
69
        $loggablePaths = $config['loggable_paths'];
70
71
        // add default paths
72
        $kernelRoot = $container->getParameter('kernel.project_dir');
73
74
        if(is_dir($dir = $kernelRoot.'/Resources/config/loggastic')) {
75
            $loggablePaths[] = $dir;
76
        }
77
78
        if(is_dir($dir = $kernelRoot.'/src/Entity')) {
79
            $loggablePaths[] = $dir;
80
        }
81
82
        $loggablePaths = array_unique($loggablePaths);
83
84
        foreach ($loggablePaths as $path) {
85
            if (is_dir($path)) {
86
                foreach (Finder::create()->followLinks()->files()->in($path)->name('/\.(xml|ya?ml)$/')->sortByName() as $file) {
87
                    $loggableClasses['yaml' === ($extension = $file->getExtension()) ? 'yml' : $extension][] = $file->getRealPath();
88
                }
89
90
                $loggableClasses['dir'][] = $path;
91
                $container->addResource(new DirectoryResource($path, '/\.(xml|ya?ml|php)$/'));
92
93
                continue;
94
            }
95
96
            if ($container->fileExists($path, false)) {
97
                if (!preg_match('/\.(xml|ya?ml)$/', (string) $path, $matches)) {
98
                    throw new RuntimeException(sprintf('Unsupported mapping type in "%s", supported types are XML & YAML.', $path));
99
                }
100
101
                $loggableClasses['yaml' === $matches[1] ? 'yml' : $matches[1]][] = $path;
102
103
                continue;
104
            }
105
106
            throw new RuntimeException(sprintf('Could not open file or directory "%s".', $path));
107
        }
108
109
        $container->setParameter('locastic_activity_logs.loggable_class_class_directories', $loggableClasses['dir']);
110
111
        return $loggableClasses;
112
    }
113
}
114