Completed
Push — master ( aaff7d...f806fe )
by Sullivan
03:03
created

AbstractRequest::hasSubscriberRef()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace Nexy\PayboxDirect\Request;
4
5
use Greg0ire\Enum\Bridge\Symfony\Validator\Constraint\Enum;
6
use Nexy\PayboxDirect\Enum\Activity;
7
use Symfony\Component\Validator\Constraints as Assert;
8
9
/**
10
 * @author Sullivan Senechal <[email protected]>
11
 */
12
abstract class AbstractRequest implements RequestInterface
13
{
14
    /**
15
     * @var int
16
     *
17
     * @Assert\NotBlank
18
     * @Enum(class="Nexy\PayboxDirect\Enum\Activity", showKeys=true)
19
     */
20
    private $activity = Activity::WEB_REQUEST;
21
22
    /**
23
     * @var \DateTime
24
     *
25
     * @Assert\Type("\DateTime")
26
     */
27
    private $date = null;
28
29
    /**
30
     * @var bool
31
     *
32
     * @Assert\Type("bool")
33
     */
34
    private $showCountry = false;
35
36
    /**
37
     * @var bool
38
     *
39
     * @Assert\Type("bool")
40
     */
41
    private $showSha1 = false;
42
43
    /**
44
     * @var bool
45
     *
46
     * @Assert\Type("bool")
47
     */
48
    private $showCardType = false;
49
50
    /**
51
     * @var string|null
52
     *
53
     * @Assert\Type("string")
54
     * @Assert\Length(min=1, max=250)
55
     */
56
    private $subscriberRef = null;
57
58
    /**
59
     * @param null|string $subscriberRef
60
     */
61
    public function __construct($subscriberRef = null)
62
    {
63
        $this->subscriberRef = $subscriberRef;
64
    }
65
66
    /**
67
     * @param int $activity
68
     *
69
     * @return $this
70
     */
71
    final public function setActivity($activity)
72
    {
73
        $this->activity = $activity;
74
75
        return $this;
76
    }
77
78
    /**
79
     * @param \DateTime|null $date
80
     *
81
     * @return $this
82
     */
83
    final public function setDate(\DateTime $date = null)
84
    {
85
        $this->date = $date;
86
87
        return $this;
88
    }
89
90
    /**
91
     * @param bool $showCountry
92
     *
93
     * @return $this
94
     */
95
    final public function setShowCountry($showCountry)
96
    {
97
        $this->showCountry = $showCountry;
98
99
        return $this;
100
    }
101
102
    /**
103
     * @param bool $showSha1
104
     *
105
     * @return $this
106
     */
107
    final public function setShowSha1($showSha1)
108
    {
109
        $this->showSha1 = $showSha1;
110
111
        return $this;
112
    }
113
114
    /**
115
     * @param bool $showCardType
116
     *
117
     * @return $this
118
     */
119
    final public function setShowCardType($showCardType)
120
    {
121
        $this->showCardType = $showCardType;
122
123
        return $this;
124
    }
125
126
    /**
127
     * @return bool
128
     */
129
    final protected function hasSubscriberRef()
130
    {
131
        return !empty($this->subscriberRef);
132
    }
133
134
    /**
135
     * @return null|string
136
     */
137
    final protected function getSubscriberRef()
138
    {
139
        return $this->subscriberRef;
140
    }
141
142
    /**
143
     * {@inheritdoc}
144
     */
145
    public function getParameters()
146
    {
147
        $parameters = [
148
            'ACTIVITE' => $this->activity,
149
            'DATEQ' => $this->date instanceof \DateTime ? $this->date->format('dmYHis') : null,
150
        ];
151
152
        if ($this->showCountry) {
153
            $parameters['PAYS'] = '';
154
        }
155
        if ($this->showSha1) {
156
            $parameters['SHA-1'] = '';
157
        }
158
        if ($this->showCardType) {
159
            $parameters['TYPECARTE'] = '';
160
        }
161
162
        if (method_exists($this, 'getTransactionNumber')) {
163
            $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...
164
        }
165
        if (method_exists($this, 'getCallNumber')) {
166
            $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...
167
        }
168
        if (method_exists($this, 'hasAuthorization') && $this->hasAuthorization()) {
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 hasAuthorization() 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...
169
            $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...
170
        }
171
172
        // Direct Plus requests special case.
173
        if ($this->hasSubscriberRef()) {
174
            $parameters['REFABONNE'] = $this->getSubscriberRef();
175
        }
176
177
        return $parameters;
178
    }
179
}
180