Completed
Pull Request — develop (#563)
by Narcotic
66:28
created

DocumentFieldNamesCompilerPass::getFieldNames()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 1
cts 1
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 1
crap 2
1
<?php
2
/**
3
 * DocumentFieldNamesCompilerPass class file
4
 */
5
6
namespace Graviton\DocumentBundle\DependencyInjection\Compiler;
7
8
use Graviton\DocumentBundle\DependencyInjection\Compiler\Utils\Document;
9
use Graviton\DocumentBundle\DependencyInjection\Compiler\Utils\DocumentMap;
10
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
11
use Symfony\Component\DependencyInjection\ContainerBuilder;
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 DocumentFieldNamesCompilerPass implements CompilerPassInterface
19
{
20
    /**
21
     * @var DocumentMap
22
     */
23
    private $documentMap;
24
25
    /**
26
     * load services
27
     *
28
     * @param ContainerBuilder $container container builder
29
     *
30 4
     * @return void
31
     */
32 4
    public function process(ContainerBuilder $container)
33 4
    {
34
        $this->documentMap = $container->get('graviton.document.map');
35
36
        $map = [];
37
        foreach ($this->documentMap->getDocuments() as $document) {
38
            $map[$document->getClass()] = $this->getFieldNames($document);
39
        }
40
        $container->setParameter('graviton.document.field.names', $map);
41
    }
42 4
43
    /**
44 4
     * Get field names
45 4
     *
46 4
     * @param Document $document Document
47 2
     * @return array
48 4
     */
49 4
    private function getFieldNames(Document $document)
50
    {
51
        $result = [];
52
        foreach ($document->getFields() as $field) {
53
            $result[$field->getFieldName()] = $field->getExposedName();
54
        }
55
        return $result;
56
    }
57
}
58