|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the MilioooMessageBundle package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Michiel boeckaert <[email protected]> |
|
7
|
|
|
* This source file is subject to the MIT license that is bundled |
|
8
|
|
|
* with this source code in the file LICENSE. |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace Miliooo\MessagingBundle\Validator\Constraint; |
|
12
|
|
|
|
|
13
|
|
|
use Miliooo\Messaging\User\ParticipantInterface; |
|
14
|
|
|
use Symfony\Component\Validator\Constraint; |
|
15
|
|
|
use Symfony\Component\Validator\ConstraintValidator; |
|
16
|
|
|
use Miliooo\Messaging\Manager\CanMessageRecipientManagerInterface; |
|
17
|
|
|
use Miliooo\Messaging\User\ParticipantProviderInterface; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Can message recipient validator. |
|
21
|
|
|
* |
|
22
|
|
|
* @author Michiel Boeckaert <[email protected]> |
|
23
|
|
|
*/ |
|
24
|
|
|
class CanMessageRecipientValidator extends ConstraintValidator |
|
25
|
|
|
{ |
|
26
|
|
|
/** |
|
27
|
|
|
* A can message recipient manager instance. |
|
28
|
|
|
* |
|
29
|
|
|
* @var CanMessageRecipientManagerInterface |
|
30
|
|
|
*/ |
|
31
|
|
|
private $manager; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* A participant provider instance. |
|
35
|
|
|
* |
|
36
|
|
|
* @var ParticipantProviderInterface |
|
37
|
|
|
*/ |
|
38
|
|
|
private $participantProvider; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Constructor. |
|
42
|
|
|
* |
|
43
|
|
|
* @param CanMessageRecipientManagerInterface $manager |
|
44
|
|
|
* @param ParticipantProviderInterface $participantProvider |
|
45
|
|
|
*/ |
|
46
|
|
|
public function __construct( |
|
47
|
|
|
CanMessageRecipientManagerInterface $manager, |
|
48
|
|
|
ParticipantProviderInterface $participantProvider |
|
49
|
|
|
) { |
|
50
|
|
|
$this->manager = $manager; |
|
51
|
|
|
$this->participantProvider = $participantProvider; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Validate. |
|
56
|
|
|
* |
|
57
|
|
|
* @param ParticipantInterface $recipient The recipient we check |
|
58
|
|
|
* @param Constraint $constraint The CanMessageRecipient constraint |
|
59
|
|
|
* |
|
60
|
|
|
*/ |
|
61
|
|
|
public function validate($recipient, Constraint $constraint) |
|
62
|
|
|
{ |
|
63
|
|
|
$constraint = $this->getConstraint($constraint); |
|
|
|
|
|
|
64
|
|
|
|
|
65
|
|
|
$loggedInUser = $this->participantProvider->getAuthenticatedParticipant(); |
|
66
|
|
|
|
|
67
|
|
|
if ($this->manager->canMessageRecipient($loggedInUser, $recipient) === false) { |
|
68
|
|
|
//get the error code from the manager |
|
69
|
|
|
$errorCode = $this->manager->getErrorMessage(); |
|
70
|
|
|
//use the error code from the manager or the default error code if the manager returns no error code |
|
71
|
|
|
$errorMessage = $errorCode ? $errorCode : $constraint->message; |
|
72
|
|
|
$this->context->addViolation($errorMessage); |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @param CanMessageRecipient $constraint |
|
78
|
|
|
* |
|
79
|
|
|
* @return CanMessageRecipient |
|
80
|
|
|
*/ |
|
81
|
|
|
protected function getConstraint(CanMessageRecipient $constraint) |
|
82
|
|
|
{ |
|
83
|
|
|
return $constraint; |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.