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

ReadOnlyFieldConstraint::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
dl 0
loc 6
ccs 5
cts 5
cp 1
rs 9.4285
c 1
b 0
f 1
cc 1
eloc 4
nc 1
nop 3
crap 1
1
<?php
2
/**
3
 * Schema constraint that validates if readOnly: true fields are manipulated and rejects changes on those.
4
 */
5
6
namespace Graviton\SchemaBundle\Constraint;
7
8
use Doctrine\ODM\MongoDB\DocumentManager;
9
use Graviton\JsonSchemaBundle\Validator\Constraint\Event\ConstraintEventSchema;
10
use Graviton\RestBundle\Service\RestUtils;
11
use Symfony\Component\PropertyAccess\PropertyAccess;
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 ReadOnlyFieldConstraint
19
{
20
21
    /**
22
     * @var DocumentManager
23
     */
24
    private $dm;
25
26
    /**
27
     * @var RestUtils
28
     */
29
    private $restUtils;
30
31
    /**
32
     * @var array
33
     */
34
    private $fieldMap;
35
36
    /**
37
     * ReadOnlyFieldConstraint constructor.
38
     *
39
     * @param DocumentManager $dm                DocumentManager
40
     * @param RestUtils       $restUtils         RestUtils
41
     * @param array           $readOnlyFieldsMap field map from compiler pass
42
     */
43 4
    public function __construct(DocumentManager $dm, RestUtils $restUtils, array $readOnlyFieldsMap)
44
    {
45 4
        $this->dm = $dm;
46 4
        $this->restUtils = $restUtils;
47 4
        $this->fieldMap = $readOnlyFieldsMap;
48 4
    }
49
50
    /**
51
     * Checks the readOnly fields and sets error in event if needed
52
     *
53
     * @param ConstraintEventSchema $event event class
54
     *
55
     * @return void
56
     */
57 4
    public function checkReadOnlyFields(ConstraintEventSchema $event)
58
    {
59 4
        $schema = $event->getSchema();
60 4
        $data = $event->getElement();
61
62 4
        if (!isset($schema->{'x-documentClass'}) || !isset($data->id)) {
63
            return;
64
        }
65
66 4
        $documentClass = $schema->{'x-documentClass'};
67
68 4
        if (!isset($this->fieldMap[$documentClass])) {
69
            return;
70
        }
71
72 4
        $readOnlyFields = $this->fieldMap[$documentClass];
73 4
        $recordId = $data->id;
74
75
        // get the current record
76 4
        $currentRecord = $this->getCurrentRecordSerializedAndBack($documentClass, $recordId);
77
78 4
        if (is_null($currentRecord)) {
79
            return;
80
        }
81
82
        // compare fields in both objects
83 4
        $accessor = PropertyAccess::createPropertyAccessor();
84 4
        foreach ($readOnlyFields as $fieldName) {
85 4
            $storedValue = null;
86 4
            if ($accessor->isReadable($currentRecord, $fieldName)) {
87 4
                $storedValue = $accessor->getValue($currentRecord, $fieldName);
88 2
            }
89
90 4
            if (is_object($storedValue)) {
91
                // skip objects as a whole, we will test their readOnly properties instead
92
                continue;
93
            }
94
95 4
            $setValue = null;
96 4
            if ($accessor->isReadable($data, $fieldName)) {
97 4
                $setValue = $accessor->getValue($data, $fieldName);
98 2
            }
99
100 4
            if ($storedValue != $setValue) {
101 2
                $event->addError(
102 3
                    sprintf('The value %s is read only.', json_encode($accessor->getValue($currentRecord, $fieldName))),
103
                    $fieldName
104 1
                );
105 1
            }
106 2
        }
107 4
    }
108
109
    /**
110
     * to make sure we don't compare apple and oranges, we let the serializer
111
     * do what he does and bring it back as object. only then all friends like extref
112
     * (exposeAs) and so on are resolved and we can truly compare structures..
113
     *
114
     * @param string $documentClass document class
115
     * @param string $recordId      record id
116
     *
117
     * @return object stored object in presentation form
118
     */
119 4
    private function getCurrentRecordSerializedAndBack($documentClass, $recordId)
120
    {
121 4
        $current = $this->dm->getRepository($documentClass)->find($recordId);
122
123 4
        if (is_null($current)) {
124
            return null;
125
        }
126
127 4
        return json_decode($this->restUtils->serializeContent($current));
128
    }
129
}
130