Completed
Push — master ( d2fb93...a24186 )
by Narcotic
26:10 queued 11:13
created

RqlFieldsCompilerPass::process()   B

Complexity

Conditions 4
Paths 3

Size

Total Lines 26
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 4.0047

Importance

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