Completed
Pull Request — develop (#487)
by Lucas
100:12 queued 61:52
created

RecordOriginExceptionFieldsCompilerPass::process()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 26
Code Lines 15

Duplication

Lines 26
Ratio 100 %

Code Coverage

Tests 19
CRAP Score 5.0214

Importance

Changes 2
Bugs 1 Features 1
Metric Value
c 2
b 1
f 1
dl 26
loc 26
ccs 19
cts 21
cp 0.9048
rs 8.439
cc 5
eloc 15
nc 5
nop 1
crap 5.0214
1
<?php
2
/**
3
 * a CompilerPass to assist the new JSON schema based validation for recordOriginException 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
12
/**
13
 * @author   List of contributors <https://github.com/libgraviton/graviton/graphs/contributors>
14
 * @license  http://opensource.org/licenses/gpl-license.php GNU Public License
15
 * @link     http://swisscom.ch
16
 */
17 View Code Duplication
class RecordOriginExceptionFieldsCompilerPass implements CompilerPassInterface
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
18
{
19
    /**
20
     * @var DocumentMap
21
     */
22
    private $documentMap;
23
24
    /**
25
     * Constructor
26
     *
27
     * @param DocumentMap $documentMap Document map
28
     */
29 2
    public function __construct(DocumentMap $documentMap)
30
    {
31 2
        $this->documentMap = $documentMap;
32 2
    }
33
34
    /**
35
     * Make recordOriginException fields map and set it to parameter
36
     *
37
     * @param ContainerBuilder $container container builder
38
     *
39
     * @return void
40
     */
41 2
    public function process(ContainerBuilder $container)
42
    {
43 2
        $map = [];
44 2
        foreach ($this->documentMap->getDocuments() as $document) {
45 2
            $recordOriginExceptionFields = $this->documentMap->getFieldNamesFlat(
46 2
                $document,
47 2
                '',
48 2
                '',
49 2
                function ($field) {
50 2
                    return $field->isRecordOriginException();
51
                }
52 2
            );
53
54 2
            if (!empty($recordOriginExceptionFields)) {
55 2
                foreach ($recordOriginExceptionFields as $key => $recordOriginExceptionField) {
56 2
                    if (substr($recordOriginExceptionField, -2) == '.0') {
57
                        unset($recordOriginExceptionFields[$key]);
58
                    }
59 2
                }
60
61 2
                $map[$document->getClass()] = array_values($recordOriginExceptionFields);
62 2
            }
63 2
        }
64
65 2
        $container->setParameter('graviton.document.recordoriginexception.fields', $map);
66 2
    }
67
}
68