Completed
Push — feature/EVO-4972-incremental-d... ( 4d7d33 )
by Narcotic
05:58
created

ConstraintUtils::onSchemaValidation()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
crap 1
1
<?php
2
/**
3
 * Common functions for constraints, mostly here for performance reasons
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
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
class ConstraintUtils
18
{
19
20
    /**
21
     * @var array
22
     */
23
    private $entities = [];
24
25
    /**
26
     * @var \stdClass
27
     */
28
    private $currentSchema;
29
30
    /**
31
     * @var \stdClass
32
     */
33
    private $currentData;
34
35
    /**
36
     * Constructor.
37
     *
38
     * @param DocumentManager $dm        DocumentManager
39
     * @param RestUtils       $restUtils RestUtils
40
     */
41 4
    public function __construct(DocumentManager $dm, RestUtils $restUtils)
42
    {
43 4
        $this->dm = $dm;
44 4
        $this->restUtils = $restUtils;
45 4
    }
46
47
    /**
48
     * Gets a entity from the database as a generic object. All constraints that need the saved data to compare
49
     * values or anything should call this function to get what they need. As this is cached in the instance,
50
     * it will fetched only once even if multiple constraints need that object.
51
     *
52
     * @param string $documentClass document class
53
     * @param string $recordId      record id
54
     *
55
     * @throws \Doctrine\ODM\MongoDB\LockException
56
     * @throws \Exception
57
     *
58
     * @return object|null entity
59
     */
60 4
    public function getSerializedEntity($documentClass, $recordId)
61
    {
62 4
        if (!isset($this->entities[$documentClass][$recordId])) {
63 4
            $current = $this->dm->getRepository($documentClass)->find($recordId);
64
65 4
            if (is_null($current)) {
66
                $this->entities[$documentClass][$recordId] = null;
67
            } else {
68 4
                $this->entities[$documentClass][$recordId] = json_decode($this->restUtils->serializeContent($current));
69
            }
70 2
        }
71
72 4
        return $this->entities[$documentClass][$recordId];
73
    }
74
75
    /**
76
     * Returns the current request entity (as \stdClass) if possible
77
     *
78
     * @return null|object
79
     */
80 4
    public function getCurrentEntity()
81
    {
82 4
        if (isset($this->currentSchema->{'x-documentClass'}) && isset($this->currentData->id)) {
83 4
            return $this->getSerializedEntity($this->currentSchema->{'x-documentClass'}, $this->currentData->id);
84
        }
85
86
        return null;
87
    }
88
89
    /**
90
     * gets the current schema. helpful for field schema validators that need access to the whole schema in some way.
91
     *
92
     * @return \stdClass
93
     */
94
    public function getCurrentSchema()
95
    {
96
        return $this->currentSchema;
97
    }
98
99
    /**
100
     * gets the current data from the client (the whole object).
101
     * helpful for field schema validators that need access to the whole data in some way.
102
     *
103
     * @return \stdClass
104
     */
105
    public function getCurrentData()
106
    {
107
        return $this->currentData;
108
    }
109
110
    /**
111
     * called on the first schema validation, before anything else.
112
     *
113
     * @param ConstraintEventSchema $event event
114
     *
115
     * @return void
116
     */
117 4
    public function onSchemaValidation(ConstraintEventSchema $event)
118
    {
119 4
        $this->currentSchema = $event->getSchema();
120 4
        $this->currentData = $event->getElement();
121 4
    }
122
}
123