Configuration::get()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 29
rs 9.456
c 0
b 0
f 0
cc 2
nc 2
nop 0
1
<?php
2
namespace CultureKings\Afterpay\Service\Merchant;
3
4
use CultureKings\Afterpay\Exception\ApiException;
5
use CultureKings\Afterpay\Model\ErrorResponse;
6
use CultureKings\Afterpay\Model\Merchant\Authorization;
7
use CultureKings\Afterpay\Model\Merchant\Configuration as ConfigurationModel;
8
use CultureKings\Afterpay\Traits;
9
use GuzzleHttp\ClientInterface;
10
use GuzzleHttp\Exception\BadResponseException;
11
use JMS\Serializer\SerializerInterface;
12
13
/**
14
 * Class Configuration
15
 *
16
 * @package CultureKings\Afterpay\Service\Merchant
17
 */
18
class Configuration
19
{
20
    use Traits\AuthorizationTrait;
21
    use Traits\ClientTrait;
22
    use Traits\SerializerTrait;
23
24
    /**
25
     * Configuration constructor.
26
     *
27
     * @param ClientInterface     $client
28
     * @param Authorization       $authorization
29
     * @param SerializerInterface $serializer
30
     */
31
    public function __construct(
32
        ClientInterface $client,
33
        Authorization $authorization,
34
        SerializerInterface $serializer
35
    ) {
36
        $this->setClient($client);
0 ignored issues
show
Compatibility introduced by
$client of type object<GuzzleHttp\ClientInterface> is not a sub-type of object<GuzzleHttp\Client>. It seems like you assume a concrete implementation of the interface GuzzleHttp\ClientInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
37
        $this->setAuthorization($authorization);
38
        $this->setSerializer($serializer);
39
    }
40
41
    /**
42
     * @return array
43
     */
44
    public function get()
45
    {
46
        try {
47
            $result = $this->getClient()->get(
48
                'configuration',
49
                [
50
                    'auth' => [
51
                        $this->getAuthorization()->getMerchantId(),
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface CultureKings\Afterpay\Co...\AuthorizationInterface as the method getMerchantId() does only exist in the following implementations of said interface: CultureKings\Afterpay\Model\Merchant\Authorization.

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...
52
                        $this->getAuthorization()->getSecret(),
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface CultureKings\Afterpay\Co...\AuthorizationInterface as the method getSecret() does only exist in the following implementations of said interface: CultureKings\Afterpay\Model\Merchant\Authorization.

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...
53
                    ],
54
                ]
55
            );
56
        } catch (BadResponseException $exception) {
57
            throw new ApiException(
58
                $this->getSerializer()->deserialize(
59
                    (string) $exception->getResponse()->getBody(),
60
                    ErrorResponse::class,
61
                    'json'
62
                ),
63
                $exception
64
            );
65
        }
66
67
        return $this->getSerializer()->deserialize(
68
            (string) $result->getBody(),
69
            sprintf('array<%s>', ConfigurationModel::class),
70
            'json'
71
        );
72
    }
73
}
74