RequestProcessor::getQuery()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
/*
4
 * This file has been created by developers from BitBag.
5
 * Feel free to contact us once you face any issues or want to start
6
 * another great project.
7
 * You can find more information about us on https://bitbag.io and write us
8
 * an email on [email protected].
9
 */
10
11
declare(strict_types=1);
12
13
namespace BitBag\SyliusVueStorefrontPlugin\Processor;
14
15
use BitBag\SyliusVueStorefrontPlugin\Command\CommandInterface;
16
use BitBag\SyliusVueStorefrontPlugin\Query\QueryInterface;
17
use BitBag\SyliusVueStorefrontPlugin\Request\RequestInterface;
18
use Symfony\Component\HttpFoundation\Request;
19
use Symfony\Component\Serializer\SerializerInterface;
20
use Symfony\Component\Validator\ConstraintViolationListInterface;
21
use Symfony\Component\Validator\Validator\ValidatorInterface;
22
23
final class RequestProcessor implements RequestProcessorInterface
24
{
25
    /** @var ValidatorInterface */
26
    private $validator;
27
28
    /** @var SerializerInterface */
29
    private $serializer;
30
31
    /** @var string */
32
    private $className;
33
34
    public function __construct(ValidatorInterface $validator, SerializerInterface $serializer, string $className)
35
    {
36
        $this->validator = $validator;
37
        $this->serializer = $serializer;
38
        $this->className = $className;
39
    }
40
41
    public function validate(Request $httpRequest): ConstraintViolationListInterface
42
    {
43
        return $this->validator->validate($this->transformHttpRequest($httpRequest));
44
    }
45
46
    public function getCommand(Request $httpRequest): CommandInterface
47
    {
48
        return $this->transformHttpRequest($httpRequest)->getCommand();
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface BitBag\SyliusVueStorefro...equest\RequestInterface as the method getCommand() does only exist in the following implementations of said interface: BitBag\SyliusVueStorefro...Cart\ApplyCouponRequest, BitBag\SyliusVueStorefro...\Cart\CreateCartRequest, BitBag\SyliusVueStorefro...\Cart\DeleteCartRequest, BitBag\SyliusVueStorefro...art\DeleteCouponRequest, BitBag\SyliusVueStorefro...ppingInformationRequest, BitBag\SyliusVueStorefro...\Cart\UpdateCartRequest, BitBag\SyliusVueStorefro...rder\CreateOrderRequest, BitBag\SyliusVueStorefro...r\ChangePasswordRequest, BitBag\SyliusVueStorefro...\User\CreateUserRequest, BitBag\SyliusVueStorefro...er\ResetPasswordRequest, BitBag\SyliusVueStorefro...\User\UpdateUserRequest.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

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

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
49
    }
50
51
    public function getQuery(Request $httpRequest): QueryInterface
52
    {
53
        return $this->transformHttpRequest($httpRequest)->getQuery();
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface BitBag\SyliusVueStorefro...equest\RequestInterface as the method getQuery() does only exist in the following implementations of said interface: BitBag\SyliusVueStorefro...GetAppliedCouponRequest, BitBag\SyliusVueStorefro...tShippingMethodsRequest, BitBag\SyliusVueStorefro...st\Cart\PullCartRequest, BitBag\SyliusVueStorefro...\Cart\SyncTotalsRequest, BitBag\SyliusVueStorefro...Stock\CheckStockRequest, BitBag\SyliusVueStorefro...Stock\ListStocksRequest, BitBag\SyliusVueStorefro...\GetOrderHistoryRequest.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

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

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
54
    }
55
56
    private function transformHttpRequest(Request $httpRequest): RequestInterface
57
    {
58
        $requestBody = [];
59
60
        if (!empty($httpRequest->getContent())) {
61
            $requestBody = $this->serializer->decode($httpRequest->getContent(), self::FORMAT_JSON);
62
        }
63
64
        return $this->serializer->denormalize(
65
            \array_merge($requestBody, $httpRequest->attributes->all(), $httpRequest->query->all()),
66
            $this->className
67
        );
68
    }
69
}
70