vendor/sylius/shop-api-plugin/src/Controller/Customer/CustomerController.php line 24

  1. <?php
  2. /*
  3.  * This file is part of the Sylius package.
  4.  * (c) Paweł Jędrzejewski
  5.  * For the full copyright and license information, please view the LICENSE
  6.  * file that was distributed with this source code.
  7.  */
  8. declare(strict_types=1);
  9. namespace Sylius\ShopApiPlugin\Controller\Customer;
  10. use FOS\RestBundle\View\View;
  11. use FOS\RestBundle\View\ViewHandlerInterface;
  12. use Sylius\Bundle\ResourceBundle\Controller\ResourceController;
  13. use Sylius\ShopApiPlugin\View\ValidationErrorView;
  14. use Symfony\Component\HttpFoundation\Request;
  15. use Symfony\Component\HttpFoundation\Response;
  16. use Webmozart\Assert\Assert;
  17. final class CustomerController extends ResourceController
  18. {
  19.     public function createAction(Request $request): Response
  20.     {
  21.         $response parent::createAction($request);
  22.         if (Response::HTTP_BAD_REQUEST !== $response->getStatusCode()) {
  23.             return $response;
  24.         }
  25.         /** @var ViewHandlerInterface $viewHandler */
  26.         $viewHandler $this->get('fos_rest.view_handler');
  27.         $validationResults $response->getContent();
  28.         Assert::string($validationResults);
  29.         return $viewHandler
  30.             ->handle(View::create($this->createValidationMessage($validationResults), Response::HTTP_BAD_REQUEST))
  31.         ;
  32.     }
  33.     private function createValidationMessage(string $validationResults): ValidationErrorView
  34.     {
  35.         /** @var ValidationErrorView $errorMessage */
  36.         $errorMessage = new ValidationErrorView();
  37.         $errorMessage->code Response::HTTP_BAD_REQUEST;
  38.         $errorMessage->message 'Validation failed';
  39.         $parsedValidationResults json_decode($validationResultstrue);
  40.         if (isset($parsedValidationResults['errors']['errors'])) {
  41.             $errorMessage->errors['errors'] = $parsedValidationResults['errors']['errors'];
  42.         }
  43.         $childrenErrors $parsedValidationResults['errors']['children'];
  44.         $this->addErrorFromField($errorMessage$childrenErrors'email');
  45.         $this->addErrorFromField($errorMessage$childrenErrors'firstName');
  46.         $this->addErrorFromField($errorMessage$childrenErrors'lastName');
  47.         $this->addErrorFromField($errorMessage$childrenErrors'phoneNumber');
  48.         $this->addErrorFromField($errorMessage$childrenErrors'subscribedToNewsletter');
  49.         $this->addErrorFromField(
  50.             $errorMessage,
  51.             $childrenErrors['user']['children']['plainPassword']['children']['first'],
  52.             'subscribedToNewsletter',
  53.         );
  54.         $this->addErrorFromField(
  55.             $errorMessage,
  56.             $childrenErrors['user']['children']['plainPassword']['children']['second'],
  57.             'subscribedToNewsletter',
  58.         );
  59.         return $errorMessage;
  60.     }
  61.     private function addErrorFromField(ValidationErrorView $errorMessage, array $errorsstring $field): void
  62.     {
  63.         if (isset($errors[$field]['errors'])) {
  64.             $errorMessage->errors[$field] = $errors[$field]['errors'];
  65.         }
  66.     }
  67. }