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

VersionFieldConstraint   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 58
Duplicated Lines 8.62 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 7
c 2
b 0
f 0
lcom 1
cbo 4
dl 5
loc 58
rs 10
ccs 0
cts 30
cp 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B validate() 5 30 6

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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