Code Duplication    Length = 54-59 lines in 5 locations

src/Controller/Cart/AddCouponAction.php 1 location

@@ 18-76 (lines=59) @@
15
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
16
use Symfony\Component\Validator\Validator\ValidatorInterface;
17
18
final class AddCouponAction
19
{
20
    /** @var ViewHandlerInterface */
21
    private $viewHandler;
22
23
    /** @var CommandBus */
24
    private $bus;
25
26
    /** @var ValidatorInterface */
27
    private $validator;
28
29
    /** @var ValidationErrorViewFactoryInterface */
30
    private $validationErrorViewFactory;
31
32
    /** @var CartViewRepositoryInterface */
33
    private $cartQuery;
34
35
    public function __construct(
36
        ViewHandlerInterface $viewHandler,
37
        CommandBus $bus,
38
        ValidatorInterface $validator,
39
        ValidationErrorViewFactoryInterface $validationErrorViewFactory,
40
        CartViewRepositoryInterface $cartQuery
41
    ) {
42
        $this->viewHandler = $viewHandler;
43
        $this->bus = $bus;
44
        $this->validator = $validator;
45
        $this->validationErrorViewFactory = $validationErrorViewFactory;
46
        $this->cartQuery = $cartQuery;
47
    }
48
49
    /**
50
     * @param Request $request
51
     *
52
     * @return Response
53
     */
54
    public function __invoke(Request $request): Response
55
    {
56
        $addCouponRequest = new AddCouponRequest($request);
57
58
        $validationResults = $this->validator->validate($addCouponRequest);
59
60
        if (0 === count($validationResults)) {
61
            $addCouponCommand = $addCouponRequest->getCommand();
62
63
            $this->bus->handle($addCouponCommand);
64
65
            try {
66
                return $this->viewHandler->handle(
67
                    View::create($this->cartQuery->getOneByToken($addCouponCommand->orderToken()), Response::HTTP_OK)
68
                );
69
            } catch (\InvalidArgumentException $exception) {
70
                throw new BadRequestHttpException($exception->getMessage());
71
            }
72
        }
73
74
        return $this->viewHandler->handle(View::create($this->validationErrorViewFactory->create($validationResults), Response::HTTP_BAD_REQUEST));
75
    }
76
}
77

src/Controller/Cart/ChangeItemQuantityAction.php 1 location

@@ 18-71 (lines=54) @@
15
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
16
use Symfony\Component\Validator\Validator\ValidatorInterface;
17
18
final class ChangeItemQuantityAction
19
{
20
    /** @var ViewHandlerInterface */
21
    private $viewHandler;
22
23
    /** @var CommandBus */
24
    private $bus;
25
26
    /** @var ValidatorInterface */
27
    private $validator;
28
29
    /** @var ValidationErrorViewFactoryInterface */
30
    private $validationErrorViewFactory;
31
32
    /** @var CartViewRepositoryInterface */
33
    private $cartQuery;
34
35
    public function __construct(
36
        ViewHandlerInterface $viewHandler,
37
        CommandBus $bus,
38
        ValidatorInterface $validator,
39
        ValidationErrorViewFactoryInterface $validationErrorViewFactory,
40
        CartViewRepositoryInterface $cartQuery
41
    ) {
42
        $this->viewHandler = $viewHandler;
43
        $this->bus = $bus;
44
        $this->validator = $validator;
45
        $this->validationErrorViewFactory = $validationErrorViewFactory;
46
        $this->cartQuery = $cartQuery;
47
    }
48
49
    public function __invoke(Request $request): Response
50
    {
51
        $changeItemQuantityRequest = new ChangeItemQuantityRequest($request);
52
53
        $validationResults = $this->validator->validate($changeItemQuantityRequest);
54
55
        if (0 !== count($validationResults)) {
56
            return $this->viewHandler->handle(View::create($this->validationErrorViewFactory->create($validationResults), Response::HTTP_BAD_REQUEST));
57
        }
58
59
        $changeItemQuantityCommand = $changeItemQuantityRequest->getCommand();
60
61
        $this->bus->handle($changeItemQuantityCommand);
62
63
        try {
64
            return $this->viewHandler->handle(
65
                View::create($this->cartQuery->getOneByToken($changeItemQuantityCommand->orderToken()), Response::HTTP_OK)
66
            );
67
        } catch (\InvalidArgumentException $exception) {
68
            throw new BadRequestHttpException($exception->getMessage());
69
        }
70
    }
71
}
72

src/Controller/Cart/PickupAction.php 1 location

@@ 18-71 (lines=54) @@
15
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
16
use Symfony\Component\Validator\Validator\ValidatorInterface;
17
18
final class PickupAction
19
{
20
    /** @var ViewHandlerInterface */
21
    private $viewHandler;
22
23
    /** @var CommandBus */
24
    private $bus;
25
26
    /** @var ValidatorInterface */
27
    private $validator;
28
29
    /** @var ValidationErrorViewFactoryInterface */
30
    private $validationErrorViewFactory;
31
32
    /** @var CartViewRepositoryInterface */
33
    private $cartQuery;
34
35
    public function __construct(
36
        ViewHandlerInterface $viewHandler,
37
        CommandBus $bus,
38
        ValidatorInterface $validator,
39
        ValidationErrorViewFactoryInterface $validationErrorViewFactory,
40
        CartViewRepositoryInterface $cartQuery
41
    ) {
42
        $this->viewHandler = $viewHandler;
43
        $this->bus = $bus;
44
        $this->validator = $validator;
45
        $this->validationErrorViewFactory = $validationErrorViewFactory;
46
        $this->cartQuery = $cartQuery;
47
    }
48
49
    public function __invoke(Request $request): Response
50
    {
51
        $pickupRequest = new PickupCartRequest($request);
52
53
        $validationResults = $this->validator->validate($pickupRequest);
54
55
        if (0 === count($validationResults)) {
56
            $pickupCartCommand = $pickupRequest->getCommand();
57
58
            $this->bus->handle($pickupCartCommand);
59
60
            try {
61
                return $this->viewHandler->handle(
62
                    View::create($this->cartQuery->getOneByToken($pickupCartCommand->orderToken()), Response::HTTP_CREATED)
63
                );
64
            } catch (\InvalidArgumentException $exception) {
65
                throw new BadRequestHttpException($exception->getMessage());
66
            }
67
        }
68
69
        return $this->viewHandler->handle(View::create($this->validationErrorViewFactory->create($validationResults), Response::HTTP_BAD_REQUEST));
70
    }
71
}
72

src/Controller/Cart/RemoveCouponAction.php 1 location

@@ 18-76 (lines=59) @@
15
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
16
use Symfony\Component\Validator\Validator\ValidatorInterface;
17
18
final class RemoveCouponAction
19
{
20
    /** @var ViewHandlerInterface */
21
    private $viewHandler;
22
23
    /** @var CommandBus */
24
    private $bus;
25
26
    /** @var ValidatorInterface */
27
    private $validator;
28
29
    /** @var ValidationErrorViewFactoryInterface */
30
    private $validationErrorViewFactory;
31
32
    /** @var CartViewRepositoryInterface */
33
    private $cartQuery;
34
35
    public function __construct(
36
        ViewHandlerInterface $viewHandler,
37
        CommandBus $bus,
38
        ValidatorInterface $validator,
39
        ValidationErrorViewFactoryInterface $validationErrorViewFactory,
40
        CartViewRepositoryInterface $cartQuery
41
    ) {
42
        $this->viewHandler = $viewHandler;
43
        $this->bus = $bus;
44
        $this->validator = $validator;
45
        $this->validationErrorViewFactory = $validationErrorViewFactory;
46
        $this->cartQuery = $cartQuery;
47
    }
48
49
    /**
50
     * @param Request $request
51
     *
52
     * @return Response
53
     */
54
    public function __invoke(Request $request): Response
55
    {
56
        $removeCouponRequest = new RemoveCouponRequest($request);
57
58
        $validationResults = $this->validator->validate($removeCouponRequest);
59
60
        if (0 === count($validationResults)) {
61
            $removeCouponCommand = $removeCouponRequest->getCommand();
62
63
            $this->bus->handle($removeCouponCommand);
64
65
            try {
66
                return $this->viewHandler->handle(
67
                    View::create($this->cartQuery->getOneByToken($removeCouponCommand->orderToken()), Response::HTTP_OK)
68
                );
69
            } catch (\InvalidArgumentException $exception) {
70
                throw new BadRequestHttpException($exception->getMessage());
71
            }
72
        }
73
74
        return $this->viewHandler->handle(View::create($this->validationErrorViewFactory->create($validationResults), Response::HTTP_BAD_REQUEST));
75
    }
76
}
77

src/Controller/Cart/RemoveItemFromCartAction.php 1 location

@@ 18-71 (lines=54) @@
15
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
16
use Symfony\Component\Validator\Validator\ValidatorInterface;
17
18
final class RemoveItemFromCartAction
19
{
20
    /** @var ViewHandlerInterface */
21
    private $viewHandler;
22
23
    /** @var CommandBus */
24
    private $bus;
25
26
    /** @var ValidatorInterface */
27
    private $validator;
28
29
    /** @var ValidationErrorViewFactoryInterface */
30
    private $validationErrorViewFactory;
31
32
    /** @var CartViewRepositoryInterface */
33
    private $cartQuery;
34
35
    public function __construct(
36
        ViewHandlerInterface $viewHandler,
37
        CommandBus $bus,
38
        ValidatorInterface $validator,
39
        ValidationErrorViewFactoryInterface $validationErrorViewFactory,
40
        CartViewRepositoryInterface $cartQuery
41
    ) {
42
        $this->viewHandler = $viewHandler;
43
        $this->bus = $bus;
44
        $this->validator = $validator;
45
        $this->validationErrorViewFactory = $validationErrorViewFactory;
46
        $this->cartQuery = $cartQuery;
47
    }
48
49
    public function __invoke(Request $request): Response
50
    {
51
        $removeItemFromCartRequest = new RemoveItemFromCartRequest($request);
52
53
        $validationResults = $this->validator->validate($removeItemFromCartRequest);
54
55
        if (0 !== count($validationResults)) {
56
            return $this->viewHandler->handle(View::create($this->validationErrorViewFactory->create($validationResults), Response::HTTP_BAD_REQUEST));
57
        }
58
59
        $removeItemFromCartCommand = $removeItemFromCartRequest->getCommand();
60
61
        $this->bus->handle($removeItemFromCartCommand);
62
63
        try {
64
            return $this->viewHandler->handle(
65
                View::create($this->cartQuery->getOneByToken($removeItemFromCartCommand->orderToken()), Response::HTTP_OK)
66
            );
67
        } catch (\InvalidArgumentException $exception) {
68
            throw new BadRequestHttpException($exception->getMessage());
69
        }
70
    }
71
}
72