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

getCurrentRecordSerializedAndBack()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 10
rs 9.4285
cc 2
eloc 5
nc 2
nop 2
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
    public function __construct(DocumentManager $dm, RestUtils $restUtils, array $readOnlyFieldsMap)
44
    {
45
        $this->dm = $dm;
46
        $this->restUtils = $restUtils;
47
        $this->fieldMap = $readOnlyFieldsMap;
48
    }
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
    public function checkReadOnlyFields(ConstraintEventSchema $event)
58
    {
59
        $schema = $event->getSchema();
60
        $data = $event->getElement();
61
62
        if (!isset($schema->{'x-documentClass'}) || !isset($data->id)) {
63
            return;
64
        }
65
66
        $documentClass = $schema->{'x-documentClass'};
67
68
        if (!isset($this->fieldMap[$documentClass])) {
69
            return;
70
        }
71
72
        $readOnlyFields = $this->fieldMap[$documentClass];
73
        $recordId = $data->id;
74
75
        // get the current record
76
        $currentRecord = $this->getCurrentRecordSerializedAndBack($documentClass, $recordId);
77
78
        if (is_null($currentRecord)) {
79
            return;
80
        }
81
82
        // compare fields in both objects
83
        $accessor = PropertyAccess::createPropertyAccessor();
84
        foreach ($readOnlyFields as $fieldName) {
85
            $storedValue = $accessor->getValue($currentRecord, $fieldName);
86
87
            if (is_object($storedValue)) {
88
                // skip objects as a whole, we will test their readOnly properties instead
89
                continue;
90
            }
91
92
            if ($storedValue != $accessor->getValue($data, $fieldName)) {
93
                $event->addError(
94
                    sprintf('The value %s is read only.', json_encode($accessor->getValue($currentRecord, $fieldName))),
95
                    $fieldName
96
                );
97
            }
98
        }
99
    }
100
101
    /**
102
     * to make sure we don't compare apple and oranges, we let the serializer
103
     * do what he does and bring it back as object. only then all friends like extref
104
     * (exposeAs) and so on are resolved and we can truly compare structures..
105
     *
106
     * @param string $documentClass document class
107
     * @param string $recordId      record id
108
     *
109
     * @return object stored object in presentation form
110
     */
111
    private function getCurrentRecordSerializedAndBack($documentClass, $recordId)
112
    {
113
        $current = $this->dm->getRepository($documentClass)->find($recordId);
114
115
        if (is_null($current)) {
116
            return null;
117
        }
118
119
        return json_decode($this->restUtils->serializeContent($current));
120
    }
121
}
122