Completed
Push — develop ( c1e8ff...b43a0e )
by
unknown
57:29 queued 47:29
created

RqlFieldsCompilerPass::process()   B

Complexity

Conditions 4
Paths 3

Size

Total Lines 28
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 4.002

Importance

Changes 0
Metric Value
dl 0
loc 28
ccs 19
cts 20
cp 0.95
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 18
nc 3
nop 1
crap 4.002
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
     * 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 2
                $ns,
47 2
                $bundle,
48
                $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)
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...
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 2
            ucfirst($ns),
84 4
            ucfirst($bundle).'Bundle',
85 2
            ucfirst($doc)
86 2
        );
87
    }
88
}
89