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

RqlFieldsCompilerPass::processDocument()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 32
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 27
CRAP Score 5.0011
Metric Value
dl 0
loc 32
ccs 27
cts 28
cp 0.9643
rs 8.439
cc 5
eloc 22
nc 5
nop 3
crap 5.0011
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