Completed
Branch feature/moar-test-optimizing (8cffd1)
by Lucas
19:31 queued 13:44
created

RqlFieldsCompilerPass   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 120
Duplicated Lines 15.83 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 89.23%
Metric Value
wmc 13
lcom 1
cbo 8
dl 19
loc 120
ccs 58
cts 65
cp 0.8923
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B process() 0 26 4
A getServiceDocument() 19 19 3
B processDocument() 0 32 5

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/**
3
 * RqlFieldsCompilerPass class file
4
 */
5
6
namespace Graviton\DocumentBundle\DependencyInjection\Compiler;
7
8
use Graviton\DocumentBundle\DependencyInjection\Compiler\Utils\Document;
9
use Graviton\DocumentBundle\DependencyInjection\Compiler\Utils\DocumentMap;
10
use Graviton\DocumentBundle\DependencyInjection\Compiler\Utils\ArrayField;
11
use Graviton\DocumentBundle\DependencyInjection\Compiler\Utils\EmbedMany;
12
use Graviton\DocumentBundle\DependencyInjection\Compiler\Utils\EmbedOne;
13
use Graviton\DocumentBundle\DependencyInjection\Compiler\Utils\Field;
14
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
15
use Symfony\Component\DependencyInjection\ContainerBuilder;
16
use Symfony\Component\DependencyInjection\Definition;
17
18
/**
19
 * @author   List of contributors <https://github.com/libgraviton/graviton/graphs/contributors>
20
 * @license  http://opensource.org/licenses/gpl-license.php GNU Public License
21
 * @link     http://swisscom.ch
22
 */
23
class RqlFieldsCompilerPass implements CompilerPassInterface
24
{
25
    /**
26
     * @var DocumentMap
27
     */
28
    private $documentMap;
29
30
    /**
31
     * Constructor
32
     *
33
     * @param DocumentMap $documentMap Document map
34
     */
35 2
    public function __construct(DocumentMap $documentMap)
36
    {
37 2
        $this->documentMap = $documentMap;
38 2
    }
39
40
    /**
41
     * Make extref fields map and set it to parameter
42
     *
43
     * @param ContainerBuilder $container container builder
44
     * @return void
45
     */
46 2
    public function process(ContainerBuilder $container)
47
    {
48 2
        $map = [];
49
50 2
        $services = array_keys($container->findTaggedServiceIds('graviton.rest'));
51 2
        foreach ($services as $id) {
52 2
            list($ns, $bundle, , $doc) = explode('.', $id);
53 2
            if (empty($bundle) || empty($doc)) {
54
                continue;
55
            }
56
57 2
            $className = $this->getServiceDocument(
58 2
                $container->getDefinition($id),
59 1
                $ns,
60 1
                $bundle,
61
                $doc
62 1
            );
63 2
            $rqlFields = $this->processDocument($this->documentMap->getDocument($className));
64 2
            $routePrefix = strtolower($ns.'.'.$bundle.'.'.'rest'.'.'.$doc);
65
66 2
            $map[$routePrefix.'.get'] = $rqlFields;
67 2
            $map[$routePrefix.'.all'] = $rqlFields;
68 1
        }
69
70 2
        $container->setParameter('graviton.document.rql.fields', $map);
71 2
    }
72
73
    /**
74
     * Get document class name from service
75
     *
76
     * @param Definition $service Service definition
77
     * @param string     $ns      Bundle namespace
78
     * @param string     $bundle  Bundle name
79
     * @param string     $doc     Document name
80
     * @return string
81
     */
82 2 View Code Duplication
    private function getServiceDocument(Definition $service, $ns, $bundle, $doc)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
83
    {
84 2
        $tags = $service->getTag('graviton.rest');
85 2
        if (!empty($tags[0]['collection'])) {
86
            $doc = $tags[0]['collection'];
87
            $bundle = $tags[0]['collection'];
88
        }
89
90 2
        if (strtolower($ns) === 'gravitondyn') {
91
            $ns = 'GravitonDyn';
92
        }
93
94 2
        return sprintf(
95 2
            '%s\\%s\\Document\\%s',
96 1
            ucfirst($ns),
97 2
            ucfirst($bundle).'Bundle',
98 1
            ucfirst($doc)
99 1
        );
100
    }
101
102
    /**
103
     * Recursive doctrine document processing
104
     *
105
     * @param Document $document       Document
106
     * @param string   $documentPrefix Document field prefix
107
     * @param string   $exposedPrefix  Exposed field prefix
108
     * @return array
109
     */
110 2
    private function processDocument(Document $document, $documentPrefix = '', $exposedPrefix = '')
111
    {
112 2
        $result = [];
113 2
        foreach ($document->getFields() as $field) {
114 2
            $result[$documentPrefix.$field->getFieldName()] = $exposedPrefix.$field->getExposedName();
115
116 2
            if ($field instanceof ArrayField) {
117
                $result[$documentPrefix.$field->getFieldName().'.0'] = $exposedPrefix.$field->getExposedName().'.0';
118 1
            } elseif ($field instanceof EmbedOne) {
119 2
                $result = array_merge(
120 1
                    $result,
121 2
                    $this->processDocument(
122 2
                        $field->getDocument(),
123 2
                        $documentPrefix.$field->getFieldName().'.',
124 2
                        $exposedPrefix.$field->getExposedName().'.'
125 1
                    )
126 1
                );
127 1
            } elseif ($field instanceof EmbedMany) {
128 2
                $result[$documentPrefix.$field->getFieldName().'.0'] = $exposedPrefix.$field->getExposedName().'.0';
129 2
                $result = array_merge(
130 1
                    $result,
131 2
                    $this->processDocument(
132 2
                        $field->getDocument(),
133 2
                        $documentPrefix.$field->getFieldName().'.0.',
134 2
                        $exposedPrefix.$field->getExposedName().'.0.'
135 1
                    )
136 1
                );
137 1
            }
138 1
        }
139
140 2
        return $result;
141
    }
142
}
143