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

Refund   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 108
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
lcom 1
cbo 7
dl 0
loc 108
rs 10
c 1
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
B create() 0 37 3
B reverse() 0 37 3
1
<?php
2
namespace CultureKings\Afterpay\Service\InStore;
3
4
use CultureKings\Afterpay\Exception\ApiException;
5
use CultureKings\Afterpay\Model;
6
use CultureKings\Afterpay\Traits;
7
use GuzzleHttp\Client;
8
use GuzzleHttp\Exception\BadResponseException;
9
use GuzzleHttp\Exception\ClientException;
10
use GuzzleHttp\HandlerStack;
11
use JMS\Serializer\SerializerInterface;
12
13
/**
14
 * Class Refund
15
 * @package CultureKings\Afterpay\Service\InStore
16
 */
17
class Refund
18
{
19
    use Traits\ClientTrait;
20
    use Traits\AuthorizationTrait;
21
    use Traits\SerializerTrait;
22
23
    /**
24
     * Device constructor.
25
     *
26
     * @param Model\InStore\Authorization $auth
27
     * @param Client                      $client
28
     * @param SerializerInterface         $serializer
29
     */
30
    public function __construct(Model\InStore\Authorization $auth, Client $client, SerializerInterface $serializer)
31
    {
32
        $this->setAuthorization($auth);
33
        $this->setClient($client);
34
        $this->setSerializer($serializer);
35
    }
36
37
    /**
38
     * @param Model\InStore\Refund $refund
39
     * @param HandlerStack|null    $stack
40
     *
41
     * @return array|\JMS\Serializer\scalar|object
42
     */
43
    public function create(Model\InStore\Refund $refund, HandlerStack $stack = null)
44
    {
45
        try {
46
            $params = [
47
                'headers' => [
48
                    'Accept' => 'application/json',
49
                    'Content-Type' => 'application/json',
50
                    'Authorization' => sprintf('Bearer %s', $this->getAuthorization()->getDeviceToken()),
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 getDeviceToken() does only exist in the following implementations of said interface: CultureKings\Afterpay\Model\InStore\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...
51
                    'Operator' => $this->getAuthorization()->getOperator(),
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 getOperator() does only exist in the following implementations of said interface: CultureKings\Afterpay\Model\InStore\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
                    'User-Agent' => $this->getAuthorization()->getUserAgent()
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 getUserAgent() does only exist in the following implementations of said interface: CultureKings\Afterpay\Model\InStore\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
                'body' => $this->getSerializer()->serialize(
55
                    $refund,
56
                    'json'
57
                ),
58
            ];
59
            if ($stack !== null) {
60
                $params['handler'] = $stack;
61
            }
62
63
            $result = $this->getClient()->post('refunds', $params);
64
65
            return $this->getSerializer()->deserialize(
66
                (string) $result->getBody(),
67
                Model\InStore\Refund::class,
68
                'json'
69
            );
70
        } catch (BadResponseException $e) {
71
            throw new ApiException(
72
                $this->getSerializer()->deserialize(
73
                    (string) $e->getResponse()->getBody(),
74
                    Model\ErrorResponse::class,
75
                    'json'
76
                )
77
            );
78
        }
79
    }
80
81
    /**
82
     * @param Model\InStore\Reversal $reversal
83
     * @param HandlerStack|null      $stack
84
     *
85
     * @return array|\JMS\Serializer\scalar|Model\InStore\Reversal
86
     */
87
    public function reverse(Model\InStore\Reversal $reversal, HandlerStack $stack = null)
88
    {
89
        try {
90
            $params = [
91
                'headers' => [
92
                    'Accept' => 'application/json',
93
                    'Content-Type' => 'application/json',
94
                    'Authorization' => sprintf('Bearer %s', $this->getAuthorization()->getDeviceToken()),
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 getDeviceToken() does only exist in the following implementations of said interface: CultureKings\Afterpay\Model\InStore\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...
95
                    'Operator' => $this->getAuthorization()->getOperator(),
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 getOperator() does only exist in the following implementations of said interface: CultureKings\Afterpay\Model\InStore\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...
96
                    'User-Agent' => $this->getAuthorization()->getUserAgent()
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 getUserAgent() does only exist in the following implementations of said interface: CultureKings\Afterpay\Model\InStore\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...
97
                ],
98
                'body' => $this->getSerializer()->serialize(
99
                    $reversal,
100
                    'json'
101
                ),
102
            ];
103
            if ($stack !== null) {
104
                $params['handler'] = $stack;
105
            }
106
107
            $result = $this->getClient()->post('refunds/reverse', $params);
108
109
            return $this->getSerializer()->deserialize(
110
                (string) $result->getBody(),
111
                Model\InStore\Reversal::class,
112
                'json'
113
            );
114
        } catch (BadResponseException $e) {
115
            throw new ApiException(
116
                $this->getSerializer()->deserialize(
117
                    (string) $e->getResponse()->getBody(),
118
                    Model\ErrorResponse::class,
119
                    'json'
120
                )
121
            );
122
        }
123
    }
124
}
125