Completed
Pull Request — develop (#552)
by
unknown
13:11 queued 08:53
created

VersionFieldConstraint::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
ccs 0
cts 4
cp 0
cc 1
eloc 2
nc 1
nop 1
crap 2
1
<?php
2
/**
3
 * Field constraint that validates if the posted version is the same as the one in DB.
4
 */
5
6
namespace Graviton\SchemaBundle\Constraint;
7
8
use Graviton\JsonSchemaBundle\Validator\Constraint\Event\ConstraintEventFormat;
9
use Symfony\Component\PropertyAccess\PropertyAccess;
10
11
/**
12
 * @author   List of contributors <https://github.com/libgraviton/graviton/graphs/contributors>
13
 * @license  http://opensource.org/licenses/gpl-license.php GNU Public License
14
 * @link     http://swisscom.ch
15
 */
16
class VersionFieldConstraint
17
{
18
    /** @var ConstraintUtils  */
19
    private $utils;
20
21
    /**
22
     * Constructor
23
     *
24
     * @param ConstraintUtils $utils Utils
25
     */
26
    public function __construct(ConstraintUtils $utils)
27
    {
28
        $this->utils = $utils;
29
    }
30
31
    /**
32
     * Check if the version is correct:
33
     * -> Post, even if posted it should be version 1
34
     * -> Put, should be the same value. Reject if not.
35
     * -> Patch, no check, only increment
36
     *
37
     * Default, if object have version as integer , increment 1.
38
     *
39
     * @param ConstraintEventFormat $event event class
40
     *
41
     * @return void
42
     */
43
    public function validate(ConstraintEventFormat $event)
44
    {
45
        $schema = $event->getSchema();
46
47 View Code Duplication
        if (!isset($schema->{'x-constraints'}) ||
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
48
            (is_array($schema->{'x-constraints'}) && !in_array('versioning', $schema->{'x-constraints'}))
49
        ) {
50
            return;
51
        }
52
53
        // get the current recor
54
        if ($currentRecord = $this->utils->getCurrentEntity()) {
55
            $formVersion = $event->getElement();
56
            /** @var \JsonSchema\Entity\JsonPointer $pointer */
57
            $pointer = $event->getPath();
58
            $accessor = PropertyAccess::createPropertyAccessor();
59
            $path = $this->utils->getNormalizedPathFromPointer($pointer);
60
            $storedVersion = $accessor->getValue($currentRecord, $path);
61
            if ($storedVersion !== $formVersion) {
62
                $event->addError(
63
                    sprintf(
64
                        'The version does not match %s, please update your data.',
65
                        'version: '.$storedVersion
66
                    )
67
                );
68
                return;
69
            }
70
        }
71
        return;
72
    }
73
}
74