Completed
Push — feature/fix-wrong-id-handling-... ( f8148f )
by Narcotic
34:53
created

ConstraintUtils::getCurrentEntity()   C

Complexity

Conditions 7
Paths 8

Size

Total Lines 26
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 56

Importance

Changes 3
Bugs 0 Features 1
Metric Value
c 3
b 0
f 1
dl 0
loc 26
ccs 0
cts 20
cp 0
rs 6.7272
cc 7
eloc 13
nc 8
nop 0
crap 56
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
use Symfony\Component\HttpFoundation\Request;
12
use Symfony\Component\HttpFoundation\RequestStack;
13
14
/**
15
 * @author   List of contributors <https://github.com/libgraviton/graviton/graphs/contributors>
16
 * @license  http://opensource.org/licenses/gpl-license.php GNU Public License
17
 * @link     http://swisscom.ch
18
 */
19
class ConstraintUtils
20
{
21
22
    /**
23
     * @var array
24
     */
25
    private $entities = [];
26
27
    /**
28
     * @var \stdClass
29
     */
30
    private $currentSchema;
31
32
    /**
33
     * @var \stdClass
34
     */
35
    private $currentData;
36
37
    /**
38
     * @var RequestStack
39
     */
40
    private $requestStack;
41
42
    /**
43
     * Constructor.
44
     *
45
     * @param DocumentManager $dm           DocumentManager
46
     * @param RestUtils       $restUtils    RestUtils
47
     * @param RequestStack    $requestStack RequestStack
48
     *
49
     */
50
    public function __construct(DocumentManager $dm, RestUtils $restUtils, RequestStack $requestStack)
51
    {
52
        $this->dm = $dm;
53
        $this->restUtils = $restUtils;
54
        $this->requestStack = $requestStack;
55
    }
56
57
    /**
58
     * Gets a entity from the database as a generic object. All constraints that need the saved data to compare
59
     * values or anything should call this function to get what they need. As this is cached in the instance,
60
     * it will fetched only once even if multiple constraints need that object.
61
     *
62
     * @param string $documentClass document class
63
     * @param string $recordId      record id
64
     *
65
     * @throws \Doctrine\ODM\MongoDB\LockException
66
     * @throws \Exception
67
     *
68
     * @return object|null entity
69
     */
70
    public function getSerializedEntity($documentClass, $recordId)
71
    {
72
        if (!isset($this->entities[$documentClass][$recordId])) {
73
            $current = $this->dm->getRepository($documentClass)->find($recordId);
74
75
            if (is_null($current)) {
76
                $this->entities[$documentClass][$recordId] = null;
77
            } else {
78
                $this->entities[$documentClass][$recordId] = json_decode($this->restUtils->serializeContent($current));
79
            }
80
        }
81
82
        return $this->entities[$documentClass][$recordId];
83
    }
84
85
    /**
86
     * Returns the current request entity (as \stdClass) if possible
87
     *
88
     * @return null|object
89
     */
90
    public function getCurrentEntity()
91
    {
92
        $currentRecordId = null;
93
94
        // first, let's the one from the payload..
95
        if (isset($this->currentData->id)) {
96
            $currentRecordId = $this->currentData->id;
97
        }
98
99
        // if we have a request, it must override it..
100
        if (
101
            $this->requestStack->getCurrentRequest() instanceof Request &&
102
            $this->requestStack->getCurrentRequest()->attributes->has('id')
103
        ) {
104
            $currentRecordId = $this->requestStack->getCurrentRequest()->attributes->get('id');
105
        }
106
107
        if (isset($this->currentSchema->{'x-documentClass'}) &&
108
            !empty($this->currentSchema->{'x-documentClass'}) &&
109
            !is_null($currentRecordId)
110
        ) {
111
            return $this->getSerializedEntity($this->currentSchema->{'x-documentClass'}, $currentRecordId);
112
        }
113
114
        return null;
115
    }
116
117
    /**
118
     * gets the current schema. helpful for field schema validators that need access to the whole schema in some way.
119
     *
120
     * @return \stdClass
121
     */
122
    public function getCurrentSchema()
123
    {
124
        return $this->currentSchema;
125
    }
126
127
    /**
128
     * gets the current data from the client (the whole object).
129
     * helpful for field schema validators that need access to the whole data in some way.
130
     *
131
     * @return \stdClass
132
     */
133
    public function getCurrentData()
134
    {
135
        return $this->currentData;
136
    }
137
138
    /**
139
     * called on the first schema validation, before anything else.
140
     *
141
     * @param ConstraintEventSchema $event event
142
     *
143
     * @return void
144
     */
145
    public function onSchemaValidation(ConstraintEventSchema $event)
146
    {
147
        $this->currentSchema = $event->getSchema();
148
        $this->currentData = $event->getElement();
149
    }
150
}
151