Passed
Push — master ( 67a5da...c35f7b )
by Jared
01:13
created

Orders::get()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 28
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 28
rs 8.8571
c 0
b 0
f 0
cc 2
eloc 17
nc 2
nop 1
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\OrderDetails;
8
use CultureKings\Afterpay\Model\Merchant\OrderToken;
9
use CultureKings\Afterpay\Traits;
10
use GuzzleHttp\Client;
11
use GuzzleHttp\Exception\BadResponseException;
12
use JMS\Serializer\SerializerInterface;
13
14
/**
15
 * Class Orders
16
 *
17
 * @package CultureKings\Afterpay\Service\Merchant
18
 */
19
class Orders
20
{
21
    use Traits\ClientTrait;
22
    use Traits\AuthorizationTrait;
23
    use Traits\SerializerTrait;
24
25
    /**
26
     * Orders constructor.
27
     *
28
     * @param Client              $client
29
     * @param Authorization       $authorization
30
     * @param SerializerInterface $serializer
31
     */
32
    public function __construct(
33
        Client $client,
34
        Authorization $authorization,
35
        SerializerInterface $serializer
36
    ) {
37
        $this->setClient($client);
38
        $this->setAuthorization($authorization);
39
        $this->setSerializer($serializer);
40
    }
41
42
    /**
43
     * @param OrderDetails $order
44
     * @return OrderToken|object
45
     */
46
    public function create(OrderDetails $order)
47
    {
48
        try {
49
            $result = $this->getClient()->post(
50
                'orders',
51
                [
52
                    'auth' => [
53
                        $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...
54
                        $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...
55
                    ],
56
                    'headers' => [
57
                        'Accept' => 'application/json',
58
                        'Content-Type' => 'application/json',
59
                    ],
60
                    'body' => $this->getSerializer()->serialize($order, 'json'),
61
                ]
62
            );
63
        } catch (BadResponseException $e) {
64
            throw new ApiException(
65
                $this->getSerializer()->deserialize(
66
                    (string) $e->getResponse()->getBody(),
67
                    ErrorResponse::class,
68
                    'json'
69
                )
70
            );
71
        }
72
73
        return $this->getSerializer()->deserialize(
74
            (string) $result->getBody(),
75
            OrderToken::class,
76
            'json'
77
        );
78
    }
79
80
    /**
81
     * @param string $token
82
     * @return OrderDetails|object
83
     */
84
    public function get($token)
85
    {
86
        try {
87
            $result = $this->getClient()->get(
88
                sprintf('orders/%s', $token),
89
                [
90
                    'auth' => [
91
                        $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...
92
                        $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...
93
                    ],
94
                ]
95
            );
96
        } catch (BadResponseException $e) {
97
            throw new ApiException(
98
                $this->getSerializer()->deserialize(
99
                    (string) $e->getResponse()->getBody(),
100
                    ErrorResponse::class,
101
                    'json'
102
                )
103
            );
104
        }
105
106
        return $this->getSerializer()->deserialize(
107
            (string) $result->getBody(),
108
            OrderDetails::class,
109
            'json'
110
        );
111
    }
112
}
113