Completed
Push — feature/validate-extref-id ( 7527cf )
by
unknown
06:31
created

ExtReferenceValidator::validate()   C

Complexity

Conditions 11
Paths 8

Size

Total Lines 40
Code Lines 23

Duplication

Lines 9
Ratio 22.5 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 9
loc 40
rs 5.2653
cc 11
eloc 23
nc 8
nop 2

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * ExtReferenceValidator class file
4
 */
5
6
namespace Graviton\RestBundle\Validator\Constraints\ExtReference;
7
8
use Graviton\DocumentBundle\Entity\ExtReference as ExtRef;
9
use Symfony\Component\Validator\Constraint;
10
use Symfony\Component\Validator\ConstraintValidator;
11
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
12
use Doctrine\ODM\MongoDB\DocumentManager;
13
14
/**
15
 * Validator for the extref type
16
 *
17
 * @author   List of contributors <https://github.com/libgraviton/graviton/graphs/contributors>
18
 * @license  http://opensource.org/licenses/gpl-license.php GNU Public License
19
 * @link     http://swisscom.ch
20
 */
21
class ExtReferenceValidator extends ConstraintValidator
22
{
23
24
    /** @var DocumentManager $documentManager */
25
    private $documentManager;
26
27
    /** @var Boolean */
28
    private $validateId;
29
30
    /**
31
     * ExtReferenceValidator constructor.
32
     * @param DocumentManager $documentManager DB manager
33
     * @param boolean         $validateId      Validate ID against DB or not.
34
     */
35
    public function __construct(DocumentManager $documentManager, $validateId)
36
    {
37
        $this->documentManager = $documentManager;
38
        $this->validateId = $validateId;
39
    }
40
41
    /**
42
     * Checks if the passed value is valid.
43
     *
44
     * @param mixed      $value      The value that should be validated
45
     * @param Constraint $constraint The constraint for the validation
46
     * @return void
47
     * @throws UnexpectedTypeException
48
     */
49
    public function validate($value, Constraint $constraint)
50
    {
51
        if (!$constraint instanceof ExtReference) {
52
            throw new UnexpectedTypeException($constraint, ExtReference::class);
53
        }
54
55
        if ($value === null) {
56
            return;
57
        }
58
59
        if (!$value instanceof ExtRef) {
60
            throw new UnexpectedTypeException($value, ExtRef::class);
61
        }
62
63
        if (!empty($constraint->collections) &&
64
            !in_array('*', $constraint->collections, true) &&
65
            !in_array($value->getRef(), $constraint->collections, true)
66
        ) {
67
            $this->context->addViolation($constraint->message, ['%collection%' => $value->getRef()]);
68
            return;
69
        }
70
71 View Code Duplication
        if (!$value->getId() && $value->getId()!="0") {
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...
72
            $msgArg = ['%collection%' => $value->getRef().':empty:id'];
73
            $this->context->addViolation($constraint->message, $msgArg);
74
            return;
75
        }
76
77
        // Does ID exists in DB.
78
        if ($this->validateId) {
79
            $db = $this->documentManager->getConnection()->selectDatabase('db');
80
            $collection = $db->selectCollection($value->getRef());
81
            $document = $collection->findOne(['_id' => $value->getId()]);
82 View Code Duplication
            if (!$document) {
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...
83
                $msgArg = ['%collection%' => $value->getRef().':invalid:'.$value->getId()];
84
                $this->context->addViolation($constraint->message, $msgArg);
85
            }
86
        }
87
88
    }
89
}
90