|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * Copyright 2024 Google Inc. |
| 5 | + * |
| 6 | + * This program is free software; you can redistribute it and/or |
| 7 | + * modify it under the terms of the GNU General Public License |
| 8 | + * version 2 as published by the Free Software Foundation. |
| 9 | + * |
| 10 | + * This program is distributed in the hope that it will be useful, |
| 11 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | + * GNU General Public License for more details. |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU General Public License |
| 16 | + * along with this program; if not, write to the Free Software |
| 17 | + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, |
| 18 | + * USA. |
| 19 | + */ |
| 20 | + |
| 21 | +namespace Drupal\apigee_edge\Plugin\Validation\Constraint; |
| 22 | + |
| 23 | +use Drupal\apigee_edge\Entity\Controller\OrganizationController; |
| 24 | +use Drupal\apigee_edge\SDKConnectorInterface; |
| 25 | +use Drupal\Core\DependencyInjection\ContainerInjectionInterface; |
| 26 | +use Symfony\Component\DependencyInjection\ContainerInterface; |
| 27 | +use Symfony\Component\Validator\Constraint; |
| 28 | +use Symfony\Component\Validator\ConstraintValidator; |
| 29 | + |
| 30 | +/** |
| 31 | + * Validates the LowercaseEmail constraint. |
| 32 | + */ |
| 33 | +class LowercaseEmailConstraintValidator extends ConstraintValidator implements ContainerInjectionInterface { |
| 34 | + |
| 35 | + /** |
| 36 | + * The internal entity cache. |
| 37 | + * |
| 38 | + * @var \Apigee\Edge\Api\Management\Entity\OrganizationInterface[] |
| 39 | + */ |
| 40 | + private $cache = []; |
| 41 | + |
| 42 | + /** |
| 43 | + * The organization controller service. |
| 44 | + * |
| 45 | + * @var \Drupal\apigee_edge\Entity\Controller\OrganizationController |
| 46 | + */ |
| 47 | + private $orgController; |
| 48 | + |
| 49 | + /** |
| 50 | + * The sdk comnector service. |
| 51 | + * |
| 52 | + * @var \Drupal\apigee_edge\SDKConnectorInterface |
| 53 | + */ |
| 54 | + private $sdkConnector; |
| 55 | + |
| 56 | + /** |
| 57 | + * Constructs a ValidReferenceConstraintValidator object. |
| 58 | + * |
| 59 | + * @param \Drupal\apigee_edge\SDKConnectorInterface $sdk_connector |
| 60 | + * The SDK connector service. |
| 61 | + * @param \Drupal\apigee_edge\Entity\Controller\OrganizationController $org_controller |
| 62 | + * The organization controller service. |
| 63 | + */ |
| 64 | + public function __construct(SDKConnectorInterface $sdk_connector, OrganizationController $org_controller) { |
| 65 | + $this->sdkConnector = $sdk_connector; |
| 66 | + $this->orgController = $org_controller; |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * {@inheritdoc} |
| 71 | + */ |
| 72 | + public static function create(ContainerInterface $container) { |
| 73 | + return new static( |
| 74 | + $container->get('apigee_edge.sdk_connector'), |
| 75 | + $container->get('apigee_edge.controller.organization') |
| 76 | + ); |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * {@inheritdoc} |
| 81 | + */ |
| 82 | + public function validate($value, Constraint $constraint) { |
| 83 | + try { |
| 84 | + if (!isset($this->cache[$this->sdkConnector->getOrganization()])) { |
| 85 | + $this->cache[$this->sdkConnector->getOrganization()] = $this->orgController; |
| 86 | + } |
| 87 | + // Check if organization is ApigeeX. |
| 88 | + if ($this->cache[$this->sdkConnector->getOrganization()]->isOrganizationApigeeX()) { |
| 89 | + foreach ($value as $item) { |
| 90 | + if (preg_match('/[A-Z]/', $item->value)) { |
| 91 | + // The value contains uppercase character, the error, is applied. |
| 92 | + $this->context->addViolation($constraint->notLowercase, ['%value' => $item->value]); |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | + } |
| 97 | + catch (\Exception $e) { |
| 98 | + // If not able to connect to Apigee Edge. |
| 99 | + \Drupal::logger('apigee_edge')->error($e->getMessage()); |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | +} |
0 commit comments