Completed
Push — master ( 00ae50...77d591 )
by Narcotic
27:26 queued 18:13
created

RqlFieldsCompilerPass::process()   B

Complexity

Conditions 4
Paths 3

Size

Total Lines 28
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 20
CRAP Score 4.0016

Importance

Changes 0
Metric Value
dl 0
loc 28
ccs 20
cts 21
cp 0.9524
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 18
nc 3
nop 1
crap 4.0016
1
<?php
2
/**
3
 * RqlFieldsCompilerPass class file
4
 */
5
6
namespace Graviton\DocumentBundle\DependencyInjection\Compiler;
7
8
use Graviton\DocumentBundle\DependencyInjection\Compiler\Utils\DocumentMap;
9
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
10
use Symfony\Component\DependencyInjection\ContainerBuilder;
11
use Symfony\Component\DependencyInjection\Definition;
12
13
/**
14
 * @author   List of contributors <https://github.com/libgraviton/graviton/graphs/contributors>
15
 * @license  https://opensource.org/licenses/MIT MIT License
16
 * @link     http://swisscom.ch
17
 */
18
class RqlFieldsCompilerPass implements CompilerPassInterface
19
{
20
    /**
21
     * @var DocumentMap
22
     */
23
    private $documentMap;
24
25
    /**
26
     * Make extref fields map and set it to parameter
27
     *
28
     * @param ContainerBuilder $container container builder
29
     * @return void
30
     */
31 4
    public function process(ContainerBuilder $container)
32
    {
33 4
        $this->documentMap = $container->get('graviton.document.map');
34
35 4
        $map = [];
36
37 4
        $services = array_keys($container->findTaggedServiceIds('graviton.rest'));
38 4
        foreach ($services as $id) {
39 4
            list($ns, $bundle, , $doc) = explode('.', $id);
40 4
            if (empty($bundle) || empty($doc)) {
41
                continue;
42
            }
43
44 4
            $className = $this->getServiceDocument(
45 4
                $container->getDefinition($id),
46 4
                $ns,
47 4
                $bundle,
48 2
                $doc
49 2
            );
50 4
            $rqlFields = $this->documentMap->getFieldNamesFlat($this->documentMap->getDocument($className));
51 4
            $routePrefix = strtolower($ns.'.'.$bundle.'.'.'rest'.'.'.$doc);
52
53 4
            $map[$routePrefix.'.get'] = $rqlFields;
54 4
            $map[$routePrefix.'.all'] = $rqlFields;
55 2
        }
56
57 4
        $container->setParameter('graviton.document.rql.fields', $map);
58 4
    }
59
60
    /**
61
     * Get document class name from service
62
     *
63
     * @param Definition $service Service definition
64
     * @param string     $ns      Bundle namespace
65
     * @param string     $bundle  Bundle name
66
     * @param string     $doc     Document name
67
     * @return string
68
     */
69 4 View Code Duplication
    private function getServiceDocument(Definition $service, $ns, $bundle, $doc)
70
    {
71 4
        $tags = $service->getTag('graviton.rest');
72 4
        if (!empty($tags[0]['collection'])) {
73 2
            $doc = $tags[0]['collection'];
74 2
            $bundle = $tags[0]['collection'];
75 1
        }
76
77 4
        if (strtolower($ns) === 'gravitondyn') {
78 2
            $ns = 'GravitonDyn';
79 1
        }
80
81 4
        return sprintf(
82 4
            '%s\\%s\\Document\\%s',
83 4
            ucfirst($ns),
84 4
            ucfirst($bundle).'Bundle',
85 4
            ucfirst($doc)
86 2
        );
87
    }
88
}
89