Completed
Push — master ( 648604...7ce793 )
by Sullivan
02:29
created

AbstractRequest::setShowCardType()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
3
namespace Nexy\PayboxDirect\Request;
4
5
use Nexy\PayboxDirect\Enum\Activity;
6
7
/**
8
 * @author Sullivan Senechal <[email protected]>
9
 */
10
abstract class AbstractRequest implements RequestInterface
11
{
12
    /**
13
     * @var int
14
     */
15
    private $activity = Activity::WEB_REQUEST;
16
17
    /**
18
     * @var \DateTime
19
     */
20
    private $date = null;
21
22
    /**
23
     * @var bool
24
     */
25
    private $showCountry = false;
26
27
    /**
28
     * @var bool
29
     */
30
    private $showSha1 = false;
31
32
    /**
33
     * @var bool
34
     */
35
    private $showCardType = false;
36
37
    /**
38
     * @var string|null
39
     */
40
    private $subscriberRef = null;
41
42
    /**
43
     * @param null|string $subscriberRef
44
     */
45
    public function __construct($subscriberRef = null)
46
    {
47
        $this->subscriberRef = $subscriberRef;
48
    }
49
50
    /**
51
     * @param int $activity
52
     *
53
     * @return $this
54
     */
55
    final public function setActivity($activity)
56
    {
57
        $this->activity = $activity;
58
59
        return $this;
60
    }
61
62
    /**
63
     * @param \DateTime|null $date
64
     *
65
     * @return $this
66
     */
67
    final public function setDate(\DateTime $date = null)
68
    {
69
        $this->date = $date;
70
71
        return $this;
72
    }
73
74
    /**
75
     * @param bool $showCountry
76
     *
77
     * @return $this
78
     */
79
    final public function setShowCountry($showCountry)
80
    {
81
        $this->showCountry = $showCountry;
82
83
        return $this;
84
    }
85
86
    /**
87
     * @param bool $showSha1
88
     *
89
     * @return $this
90
     */
91
    final public function setShowSha1($showSha1)
92
    {
93
        $this->showSha1 = $showSha1;
94
95
        return $this;
96
    }
97
98
    /**
99
     * @param bool $showCardType
100
     *
101
     * @return $this
102
     */
103
    final public function setShowCardType($showCardType)
104
    {
105
        $this->showCardType = $showCardType;
106
107
        return $this;
108
    }
109
110
    /**
111
     * @return null|string
112
     */
113
    final protected function getSubscriberRef()
114
    {
115
        return $this->subscriberRef;
116
    }
117
118
    /**
119
     * {@inheritdoc}
120
     */
121
    public function getParameters()
122
    {
123
        $parameters = [
124
            'ACTIVITE' => $this->activity,
125
            'DATEQ' => $this->date instanceof \DateTime ? $this->date->format('dmYHis') : null,
126
        ];
127
128
        if ($this->showCountry) {
129
            $parameters['PAYS'] = '';
130
        }
131
        if ($this->showSha1) {
132
            $parameters['SHA-1'] = '';
133
        }
134
        if ($this->showCardType) {
135
            $parameters['TYPECARTE'] = '';
136
        }
137
138
        if (method_exists($this, 'getTransactionNumber')) {
139
            $parameters['NUMTRANS'] = $this->getTransactionNumber();
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Nexy\PayboxDirect\Request\AbstractRequest as the method getTransactionNumber() does only exist in the following sub-classes of Nexy\PayboxDirect\Request\AbstractRequest: Nexy\PayboxDirect\Reques...encedTransactionRequest, Nexy\PayboxDirect\Reques...beredTransactionRequest, Nexy\PayboxDirect\Request\CancelRequest, Nexy\PayboxDirect\Request\DebitRequest, Nexy\PayboxDirect\Request\InquiryRequest, Nexy\PayboxDirect\Request\RefundRequest, Nexy\PayboxDirect\Reques...ancelTransactionRequest, Nexy\PayboxDirect\Request\UpdateAmountRequest. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

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

class MyUser extends 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 sub-classes 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 parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
140
        }
141
        if (method_exists($this, 'getCallNumber')) {
142
            $parameters['NUMAPPEL'] = $this->getCallNumber();
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Nexy\PayboxDirect\Request\AbstractRequest as the method getCallNumber() does only exist in the following sub-classes of Nexy\PayboxDirect\Request\AbstractRequest: Nexy\PayboxDirect\Reques...encedTransactionRequest, Nexy\PayboxDirect\Reques...beredTransactionRequest, Nexy\PayboxDirect\Request\CancelRequest, Nexy\PayboxDirect\Request\DebitRequest, Nexy\PayboxDirect\Request\InquiryRequest, Nexy\PayboxDirect\Request\RefundRequest, Nexy\PayboxDirect\Reques...ancelTransactionRequest, Nexy\PayboxDirect\Request\UpdateAmountRequest. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

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

class MyUser extends 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 sub-classes 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 parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
143
        }
144
        if (method_exists($this, 'getAuthorization') && null !== $this->getAuthorization()) {
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Nexy\PayboxDirect\Request\AbstractRequest as the method getAuthorization() does only exist in the following sub-classes of Nexy\PayboxDirect\Request\AbstractRequest: Nexy\PayboxDirect\Reques...horizeAndCaptureRequest, Nexy\PayboxDirect\Request\AuthorizeRequest, Nexy\PayboxDirect\Reques...bscriberRegisterRequest, Nexy\PayboxDirect\Request\SubscriberUpdateRequest, Nexy\PayboxDirect\Request\UpdateAmountRequest. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

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

class MyUser extends 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 sub-classes 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 parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
145
            $parameters['AUTORISATION'] = $this->getAuthorization();
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Nexy\PayboxDirect\Request\AbstractRequest as the method getAuthorization() does only exist in the following sub-classes of Nexy\PayboxDirect\Request\AbstractRequest: Nexy\PayboxDirect\Reques...horizeAndCaptureRequest, Nexy\PayboxDirect\Request\AuthorizeRequest, Nexy\PayboxDirect\Reques...bscriberRegisterRequest, Nexy\PayboxDirect\Request\SubscriberUpdateRequest, Nexy\PayboxDirect\Request\UpdateAmountRequest. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

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

class MyUser extends 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 sub-classes 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 parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
146
        }
147
148
        // Direct Plus requests special case.
149
        if (null !== $this->getSubscriberRef()) {
150
            $parameters['REFABONNE'] = $this->getSubscriberRef();
151
        }
152
153
        return $parameters;
154
    }
155
}
156