Completed
Pull Request — develop (#430)
by Narcotic
15:11 queued 09:56
created

ReadOnlyFieldsCompilerPass::__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 1
Bugs 0 Features 1
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 1
b 0
f 1
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
/**
3
 * a CompilerPass to assist the new JSON schema based validation for readOnly fields
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 ReadOnlyFieldsCompilerPass implements CompilerPassInterface
19
{
20
    /**
21
     * @var DocumentMap
22
     */
23
    private $documentMap;
24
25
    /**
26
     * Constructor
27
     *
28
     * @param DocumentMap $documentMap Document map
29
     */
30 2
    public function __construct(DocumentMap $documentMap)
31
    {
32 2
        $this->documentMap = $documentMap;
33 2
    }
34
35
    /**
36
     * Make readOnly fields map and set it to parameter
37
     *
38
     * @param ContainerBuilder $container container builder
39
     *
40
     * @return void
41
     */
42 2
    public function process(ContainerBuilder $container)
43
    {
44 2
        $map = [];
45 2
        foreach ($this->documentMap->getDocuments() as $document) {
46 2
            $readOnlyFields = $this->documentMap->getFieldNamesFlat(
47 1
                $document,
48 2
                '',
49 2
                '',
50 2
                function ($field) {
51 2
                    return $field->isReadOnly();
52 1
                }
53 1
            );
54
55 2
            if (!empty($readOnlyFields)) {
56 2
                foreach ($readOnlyFields as $key => $readOnlyField) {
57 2
                    if (substr($readOnlyField, -2) == '.0') {
58 2
                        unset($readOnlyFields[$key]);
59 1
                    }
60 1
                }
61
62 2
                $map[$document->getClass()] = array_values($readOnlyFields);
63 1
            }
64 1
        }
65
66 2
        $container->setParameter('graviton.document.readonly.fields', $map);
67 2
    }
68
}
69