1 | <?php |
||
32 | final class ReconfigureInstitutionRequestValidator extends ConstraintValidator |
||
33 | { |
||
34 | /** |
||
35 | * @var ConfiguredInstitutionService |
||
36 | */ |
||
37 | private $configuredInstitutionsService; |
||
38 | |||
39 | /** |
||
40 | * @var string[] internal cache, access through getConfiguredInstitutions() |
||
41 | */ |
||
42 | private $configuredInstitutions; |
||
43 | |||
44 | public function __construct(ConfiguredInstitutionService $configuredInstitutionsService) |
||
48 | |||
49 | public function validate($value, Constraint $constraint) |
||
50 | { |
||
51 | /** @var \Symfony\Component\Validator\Violation\ConstraintViolationBuilder|false $violation */ |
||
52 | $violation = false; |
||
53 | |||
54 | try { |
||
55 | $this->validateRoot($value); |
||
56 | } catch (AssertionException $exception) { |
||
57 | // method is not in the interface yet, but the old method is deprecated. |
||
58 | $violation = $this->context->buildViolation($exception->getMessage()); |
||
|
|||
59 | $violation->atPath($exception->getPropertyPath()); |
||
60 | } catch (CoreInvalidArgumentException $exception) { |
||
61 | $violation = $this->context->buildViolation($exception->getMessage()); |
||
62 | } |
||
63 | |||
64 | if ($violation) { |
||
65 | $violation->addViolation(); |
||
66 | } |
||
67 | } |
||
68 | |||
69 | public function validateRoot(array $configuration) |
||
78 | |||
79 | /** |
||
80 | * @param array $institutions |
||
81 | */ |
||
82 | public function validateInstitutionsExist(array $institutions) |
||
83 | { |
||
84 | $configuredInstitutions = $this->getConfiguredInstitutions(); |
||
85 | |||
86 | $nonExistentInstitutions = $this->determineNonExistentInstitutions($institutions, $configuredInstitutions); |
||
87 | |||
88 | if (!empty($nonExistentInstitutions)) { |
||
89 | throw new InvalidArgumentException( |
||
90 | sprintf('Cannot reconfigure non-existent institution(s): %s', implode(', ', $nonExistentInstitutions)) |
||
91 | ); |
||
92 | } |
||
93 | } |
||
94 | |||
95 | /** |
||
96 | * @param array $options |
||
97 | * @param string $institution |
||
98 | */ |
||
99 | public function validateInstitutionConfigurationOptions($options, $institution) |
||
100 | { |
||
101 | $propertyPath = sprintf('Institution(%s)', $institution); |
||
102 | |||
103 | Assertion::isArray($options, 'Invalid institution configuration, must be an object', $propertyPath); |
||
104 | |||
105 | $acceptedOptions = ['use_ra_locations', 'show_raa_contact_information', 'allowed_second_factors']; |
||
106 | StepupAssert::keysMatch( |
||
107 | $options, |
||
108 | $acceptedOptions, |
||
109 | sprintf('Expected only options "%s" for "%s"', join(', ', $acceptedOptions), $institution), |
||
110 | $propertyPath |
||
111 | ); |
||
112 | |||
113 | Assertion::boolean( |
||
114 | $options['use_ra_locations'], |
||
115 | sprintf('Option "use_ra_locations" for "%s" must be a boolean value', $institution), |
||
116 | $propertyPath |
||
117 | ); |
||
118 | |||
119 | Assertion::boolean( |
||
120 | $options['show_raa_contact_information'], |
||
121 | sprintf('Option "show_raa_contact_information" for "%s" must be a boolean value', $institution), |
||
122 | $propertyPath |
||
123 | ); |
||
124 | |||
125 | Assertion::isArray( |
||
126 | $options['allowed_second_factors'], |
||
127 | sprintf('Option "allowed_second_factors" for "%s" must be an array of strings', $institution), |
||
128 | $propertyPath |
||
129 | ); |
||
130 | Assertion::allString( |
||
131 | $options['allowed_second_factors'], |
||
132 | sprintf('Option "allowed_second_factors" for "%s" must be an array of strings', $institution), |
||
133 | $propertyPath |
||
134 | ); |
||
135 | Assertion::allInArray( |
||
136 | $options['allowed_second_factors'], |
||
137 | SecondFactorType::getAvailableSecondFactorTypes(), |
||
138 | 'Option "allowed_second_factors" for "%s" must contain valid second factor types', |
||
139 | $propertyPath |
||
140 | ); |
||
141 | } |
||
142 | |||
143 | /** |
||
144 | * Accessor for configured institutions to be able to use an internal cache |
||
145 | * |
||
146 | * @return string[] |
||
147 | */ |
||
148 | private function getConfiguredInstitutions() |
||
163 | |||
164 | /** |
||
165 | * @param string[] $institutions |
||
166 | * @param $configuredInstitutions |
||
167 | * @return string[] |
||
168 | */ |
||
169 | public function determineNonExistentInstitutions(array $institutions, $configuredInstitutions) |
||
187 | } |
||
188 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: