Completed
Push — feature/other-validation ( 582c96...3c7e86 )
by Narcotic
109:38 queued 43:32
created

ReadOnlyFieldsCompilerPass   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 6
c 1
b 0
f 1
lcom 1
cbo 3
dl 0
loc 51
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B process() 0 26 5
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
    public function __construct(DocumentMap $documentMap)
31
    {
32
        $this->documentMap = $documentMap;
33
    }
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
    public function process(ContainerBuilder $container)
43
    {
44
        $map = [];
45
        foreach ($this->documentMap->getDocuments() as $document) {
46
            $readOnlyFields = $this->documentMap->getFieldNamesFlat(
47
                $document,
48
                '',
49
                '',
50
                function ($field) {
51
                    return $field->isReadOnly();
52
                }
53
            );
54
55
            if (!empty($readOnlyFields)) {
56
                foreach ($readOnlyFields as $key => $readOnlyField) {
57
                    if (substr($readOnlyField, -2) == '.0') {
58
                        unset($readOnlyFields[$key]);
59
                    }
60
                }
61
62
                $map[$document->getClass()] = array_values($readOnlyFields);
63
            }
64
        }
65
66
        $container->setParameter('graviton.document.readonly.fields', $map);
67
    }
68
}
69