Completed
Push — master ( 8413a9...d75c00 )
by Narcotic
10:07
created

ExtRefFieldsCompilerPass::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
/**
3
 * build a list of all services that have extref mappings
4
 *
5
 * This list later gets used during rendering URLs in the output where we
6
 * need to know when and wht really needs rendering after our doctrine
7
 * custom type is only able to spit out the raw data during hydration.
8
 */
9
10
namespace Graviton\DocumentBundle\DependencyInjection\Compiler;
11
12
use Graviton\DocumentBundle\DependencyInjection\Compiler\Utils\ArrayField;
13
use Graviton\DocumentBundle\DependencyInjection\Compiler\Utils\Document;
14
use Graviton\DocumentBundle\DependencyInjection\Compiler\Utils\DocumentMap;
15
use Graviton\DocumentBundle\DependencyInjection\Compiler\Utils\EmbedMany;
16
use Graviton\DocumentBundle\DependencyInjection\Compiler\Utils\EmbedOne;
17
use Graviton\DocumentBundle\DependencyInjection\Compiler\Utils\Field;
18
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
19
use Symfony\Component\DependencyInjection\ContainerBuilder;
20
use Symfony\Component\DependencyInjection\Definition;
21
22
/**
23
 * @author   List of contributors <https://github.com/libgraviton/graviton/graphs/contributors>
24
 * @license  http://opensource.org/licenses/gpl-license.php GNU Public License
25
 * @link     http://swisscom.ch
26
 */
27
class ExtRefFieldsCompilerPass implements CompilerPassInterface
28
{
29
30
    /**
31
     * @var DocumentMap
32
     */
33
    private $documentMap;
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
        $this->documentMap = $container->get('graviton.document.map');
44
45 4
        $map = [];
46
47 4
        $services = array_keys($container->findTaggedServiceIds('graviton.rest'));
48 4
        foreach ($services as $id) {
49 4
            list($ns, $bundle, , $doc) = explode('.', $id);
50 4
            if (empty($bundle) || empty($doc)) {
51
                continue;
52
            }
53 4
            if ($bundle === 'core' && $doc === 'main') {
54
                continue;
55
            }
56
57 4
            $className = $this->getServiceDocument(
58 4
                $container->getDefinition($id),
59
                $ns,
60
                $bundle,
61
                $doc
62
            );
63 4
            $extRefFields = $this->processDocument($this->documentMap->getDocument($className));
64 4
            $routePrefix = strtolower($ns.'.'.$bundle.'.'.'rest'.'.'.$doc);
65
66 4
            $map[$routePrefix.'.get'] = $extRefFields;
67 4
            $map[$routePrefix.'.all'] = $extRefFields;
68
        }
69
70 4
        $container->setParameter('graviton.document.extref.fields', $map);
71 4
    }
72
73
74
    /**
75
     * Get document class name from service
76
     *
77
     * @param Definition $service Service definition
78
     * @param string     $ns      Bundle namespace
79
     * @param string     $bundle  Bundle name
80
     * @param string     $doc     Document name
81
     * @return string
82
     */
83 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...
84
    {
85 4
        $tags = $service->getTag('graviton.rest');
86 4
        if (!empty($tags[0]['collection'])) {
87 2
            $doc = $tags[0]['collection'];
88 2
            $bundle = $tags[0]['collection'];
89
        }
90
91 4
        if (strtolower($ns) === 'gravitondyn') {
92 2
            $ns = 'GravitonDyn';
93
        }
94
95 4
        return sprintf(
96 4
            '%s\\%s\\Document\\%s',
97
            ucfirst($ns),
98 4
            ucfirst($bundle).'Bundle',
99
            ucfirst($doc)
100
        );
101
    }
102
103
    /**
104
     * Recursive doctrine document processing
105
     *
106
     * @param Document $document      Document
107
     * @param string   $exposedPrefix Exposed field prefix
108
     * @return array
109
     */
110 4
    private function processDocument(Document $document, $exposedPrefix = '')
111
    {
112 4
        $result = [];
113 4
        foreach ($document->getFields() as $field) {
114 4
            if ($field instanceof Field) {
115 4
                if ($field->getType() === 'extref') {
116 4
                    $result[] = $exposedPrefix.$field->getExposedName();
117
                }
118 1
            } elseif ($field instanceof ArrayField) {
119 2
                if ($field->getItemType() === 'extref') {
120 1
                    $result[] = $exposedPrefix.$field->getExposedName().'.0';
121
                }
122
            } elseif ($field instanceof EmbedOne) {
123 4
                $result = array_merge(
124
                    $result,
125 4
                    $this->processDocument(
126 4
                        $field->getDocument(),
127 4
                        $exposedPrefix.$field->getExposedName().'.'
128
                    )
129
                );
130 View Code Duplication
            } elseif ($field instanceof EmbedMany) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
131 4
                $result = array_merge(
132
                    $result,
133 4
                    $this->processDocument(
134 4
                        $field->getDocument(),
135 4
                        $exposedPrefix.$field->getExposedName().'.0.'
136
                    )
137
                );
138
            }
139
        }
140
141 4
        return $result;
142
    }
143
}
144