Completed
Push — feature/EVO-5751-mongodb-3.x ( f7410f...d456eb )
by Lucas
67:06 queued 60:09
created

ConstraintUtils::getCurrentEntity()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 4.0466

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 11
ccs 6
cts 7
cp 0.8571
rs 9.2
cc 4
eloc 6
nc 2
nop 0
crap 4.0466
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'}) &&
83 4
            !empty($this->currentSchema->{'x-documentClass'}) &&
84 2
            isset($this->currentData->id)
85 2
        ) {
86 4
            return $this->getSerializedEntity($this->currentSchema->{'x-documentClass'}, $this->currentData->id);
87
        }
88
89
        return null;
90
    }
91
92
    /**
93
     * gets the current schema. helpful for field schema validators that need access to the whole schema in some way.
94
     *
95
     * @return \stdClass
96
     */
97
    public function getCurrentSchema()
98
    {
99
        return $this->currentSchema;
100
    }
101
102
    /**
103
     * gets the current data from the client (the whole object).
104
     * helpful for field schema validators that need access to the whole data in some way.
105
     *
106
     * @return \stdClass
107
     */
108
    public function getCurrentData()
109
    {
110
        return $this->currentData;
111
    }
112
113
    /**
114
     * called on the first schema validation, before anything else.
115
     *
116
     * @param ConstraintEventSchema $event event
117
     *
118
     * @return void
119
     */
120 4
    public function onSchemaValidation(ConstraintEventSchema $event)
121
    {
122 4
        $this->currentSchema = $event->getSchema();
123 4
        $this->currentData = $event->getElement();
124 4
    }
125
}
126