Advertisement
Guest User

Untitled

a guest
Dec 8th, 2019
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.27 KB | None | 0 0
  1. final class CreateCartRequest
  2. {
  3.     /** @var string|null */
  4.     private $token;
  5.     /** @var string */
  6.     private $cartId;
  7.     public function __construct(Request $request)
  8.     {
  9.         $this->token = $request->query->get('token');
  10.         $this->cartId = Uuid::uuid4()->toString();
  11.     }
  12.     public static function fromHttpRequest(Request $request): self
  13.     {
  14.         return new self($request);
  15.     }
  16.     public function getCommand(): CreateCart
  17.     {
  18.         return new CreateCart($this->token, $this->cartId);
  19.     }
  20. }
  21.  
  22. //**************************************************************
  23.  
  24.     function it_creates_cart(
  25.         ValidatorInterface $validator,
  26.         ConstraintViolationListInterface $violationList,
  27.         MessageBusInterface $bus,
  28.         GenericSuccessViewFactoryInterface $genericSuccessViewFactory,
  29.         ViewHandlerInterface $viewHandler
  30.     ): void
  31.     {
  32.         $request = new Request([
  33.             'token' => 'token'
  34.         ]);
  35.  
  36.         $createCartRequest = new CreateCartRequest($request);
  37.  
  38.         $validator->validate($createCartRequest)->willReturn($violationList);
  39.  
  40.         $envelope = new Envelope($createCartRequest->getCommand(), []);
  41.         $bus->dispatch($createCartRequest->getCommand())->willReturn($envelope);
  42.  
  43.         $genericSuccessViewFactory->create('12345')->willReturn(new GenericSuccessView());
  44.  
  45.         $viewHandler->handle(Argument::any(), Argument::any())->willReturn(new Response());
  46.  
  47.         $this->__invoke($request);
  48.     }
  49.  
  50. //**************************************************************
  51.  
  52. final class CreateCartAction
  53. {
  54.     /** @var MessageBusInterface */
  55.     private $bus;
  56.  
  57.     /** @var ValidatorInterface */
  58.     private $validator;
  59.  
  60.     /** @var ViewHandlerInterface */
  61.     private $viewHandler;
  62.  
  63.     /** @var ValidationErrorViewFactoryInterface */
  64.     private $validationErrorViewFactory;
  65.  
  66.     /** @var GenericSuccessViewFactoryInterface */
  67.     private $genericSuccessViewFactory;
  68.  
  69.     public function __construct(
  70.         MessageBusInterface $bus,
  71.         ValidatorInterface $validator,
  72.         ViewHandlerInterface $viewHandler,
  73.         ValidationErrorViewFactoryInterface $validationErrorViewFactory,
  74.         GenericSuccessViewFactoryInterface $genericSuccessViewFactory
  75.     ) {
  76.         $this->bus = $bus;
  77.         $this->validator = $validator;
  78.         $this->viewHandler = $viewHandler;
  79.         $this->validationErrorViewFactory = $validationErrorViewFactory;
  80.         $this->genericSuccessViewFactory = $genericSuccessViewFactory;
  81.     }
  82.  
  83.     public function __invoke(Request $request): Response
  84.     {
  85.         $createCartRequest = CreateCartRequest::fromHttpRequest($request);
  86.  
  87.         $validationResults = $this->validator->validate($createCartRequest);
  88.  
  89.         if (0 !== count($validationResults)) {
  90.             return $this->viewHandler->handle(View::create(
  91.                 $this->validationErrorViewFactory->create($validationResults),
  92.                 Response::HTTP_BAD_REQUEST
  93.             ));
  94.         }
  95.  
  96.         $createCartCommand = $createCartRequest->getCommand();
  97.  
  98.         $this->bus->dispatch($createCartCommand);
  99.  
  100.         return $this->viewHandler->handle(View::create(
  101.             $this->genericSuccessViewFactory->create($createCartCommand->cartId())
  102.         ));
  103.     }
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement