AbstractService   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 0
Metric Value
wmc 3
lcom 0
cbo 4
dl 0
loc 49
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A generateParams() 0 22 2
1
<?php
2
namespace CultureKings\Afterpay\Service\InStore;
3
4
use CultureKings\Afterpay\Model;
5
use CultureKings\Afterpay\Traits;
6
use GuzzleHttp\Client;
7
use GuzzleHttp\HandlerStack;
8
use JMS\Serializer\SerializerInterface;
9
10
/**
11
 * Class AbstractService
12
 * @package CultureKings\Afterpay\Service\InStore
13
 */
14
abstract class AbstractService
15
{
16
    use Traits\ClientTrait;
17
    use Traits\AuthorizationTrait;
18
    use Traits\SerializerTrait;
19
20
    /**
21
     * Device constructor.
22
     *
23
     * @param Model\InStore\Authorization $auth
24
     * @param Client                      $client
25
     * @param SerializerInterface         $serializer
26
     */
27
    public function __construct(Model\InStore\Authorization $auth, Client $client, SerializerInterface $serializer)
28
    {
29
        $this->setAuthorization($auth);
30
        $this->setClient($client);
31
        $this->setSerializer($serializer);
32
    }
33
34
    /**
35
     * @param object|array      $model
36
     * @param HandlerStack|null $stack
37
     *
38
     * @return array
39
     */
40
    protected function generateParams($model, HandlerStack $stack = null)
41
    {
42
        $params = [
43
            'headers' => [
44
                'Accept' => 'application/json',
45
                'Content-Type' => 'application/json',
46
                '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...
47
                '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...
48
                'User-Agent' => $this->getAuthorization()->getUserAgent(),
49
            ],
50
            'body' => $this->getSerializer()->serialize(
51
                $model,
52
                'json'
53
            ),
54
            'timeout' => Model\InStore\Authorization::REQUEST_TIMEOUT_SECONDS,
55
        ];
56
        if ($stack !== null) {
57
            $params['handler'] = $stack;
58
        }
59
60
        return $params;
61
    }
62
}
63