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

ReadOnlyFieldsCompilerPass   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

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

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 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