Completed
Push — develop ( e55011...fb44ee )
by
unknown
13s
created

ExtRefFieldsCompilerPass::processDocument()   C

Complexity

Conditions 8
Paths 8

Size

Total Lines 33
Code Lines 22

Duplication

Lines 9
Ratio 27.27 %

Code Coverage

Tests 29
CRAP Score 8.0023

Importance

Changes 0
Metric Value
dl 9
loc 33
ccs 29
cts 30
cp 0.9667
rs 5.3846
c 0
b 0
f 0
cc 8
eloc 22
nc 8
nop 2
crap 8.0023
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 4
                $ns,
60 4
                $bundle,
61 2
                $doc
62 2
            );
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 2
        }
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)
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 1
        }
90
91 4
        if (strtolower($ns) === 'gravitondyn') {
92 2
            $ns = 'GravitonDyn';
93 1
        }
94
95 4
        return sprintf(
96 4
            '%s\\%s\\Document\\%s',
97 4
            ucfirst($ns),
98 4
            ucfirst($bundle).'Bundle',
99 4
            ucfirst($doc)
100 2
        );
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 2
                }
118 4
            } elseif ($field instanceof ArrayField) {
119 2
                if ($field->getItemType() === 'extref') {
120 1
                    $result[] = $exposedPrefix.$field->getExposedName().'.0';
121
                }
122 4
            } elseif ($field instanceof EmbedOne) {
123 4
                $result = array_merge(
124 4
                    $result,
125 4
                    $this->processDocument(
126 4
                        $field->getDocument(),
127 4
                        $exposedPrefix.$field->getExposedName().'.'
128 2
                    )
129 2
                );
130 4 View Code Duplication
            } elseif ($field instanceof EmbedMany) {
131 4
                $result = array_merge(
132 4
                    $result,
133 4
                    $this->processDocument(
134 4
                        $field->getDocument(),
135 4
                        $exposedPrefix.$field->getExposedName().'.0.'
136 2
                    )
137 2
                );
138 2
            }
139 2
        }
140
141 4
        return $result;
142
    }
143
}
144