Completed
Push — master ( 6abda5...b08bdd )
by Narcotic
56:44 queued 45:19
created

ConstraintUtils::getCurrentEntity()   C

Complexity

Conditions 7
Paths 8

Size

Total Lines 25
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 56

Importance

Changes 4
Bugs 0 Features 2
Metric Value
c 4
b 0
f 2
dl 0
loc 25
ccs 0
cts 19
cp 0
rs 6.7272
cc 7
eloc 12
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 ($this->requestStack->getCurrentRequest() instanceof Request &&
101
            $this->requestStack->getCurrentRequest()->attributes->has('id')
102
        ) {
103
            $currentRecordId = $this->requestStack->getCurrentRequest()->attributes->get('id');
104
        }
105
106
        if (isset($this->currentSchema->{'x-documentClass'}) &&
107
            !empty($this->currentSchema->{'x-documentClass'}) &&
108
            !is_null($currentRecordId)
109
        ) {
110
            return $this->getSerializedEntity($this->currentSchema->{'x-documentClass'}, $currentRecordId);
111
        }
112
113
        return null;
114
    }
115
116
    /**
117
     * gets the current schema. helpful for field schema validators that need access to the whole schema in some way.
118
     *
119
     * @return \stdClass
120
     */
121
    public function getCurrentSchema()
122
    {
123
        return $this->currentSchema;
124
    }
125
126
    /**
127
     * gets the current data from the client (the whole object).
128
     * helpful for field schema validators that need access to the whole data in some way.
129
     *
130
     * @return \stdClass
131
     */
132
    public function getCurrentData()
133
    {
134
        return $this->currentData;
135
    }
136
137
    /**
138
     * called on the first schema validation, before anything else.
139
     *
140
     * @param ConstraintEventSchema $event event
141
     *
142
     * @return void
143
     */
144
    public function onSchemaValidation(ConstraintEventSchema $event)
145
    {
146
        $this->currentSchema = $event->getSchema();
147
        $this->currentData = $event->getElement();
148
    }
149
}
150