|
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") { |
|
|
|
|
|
|
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) { |
|
|
|
|
|
|
83
|
|
|
$msgArg = ['%collection%' => $value->getRef().':invalid:'.$value->getId()]; |
|
84
|
|
|
$this->context->addViolation($constraint->message, $msgArg); |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
|
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.