Passed
Push — master ( 209f8f...1618ee )
by Gabriel
06:05
created

createRequestWithInternalCheck()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3.0261

Importance

Changes 0
Metric Value
eloc 6
c 0
b 0
f 0
dl 0
loc 12
ccs 6
cts 7
cp 0.8571
rs 10
cc 3
nc 3
nop 2
crap 3.0261
1
<?php
2
3
namespace ByTIC\Payments\Gateways\Providers\AbstractGateway\Traits;
4
5
use BadMethodCallException;
6
use Omnipay\Common\Message\RequestInterface;
7
use Omnipay\Common\Message\ResponseInterface as MessageResponseInterface;
8
9
/**
10
 * Trait MagicMessagesTrait
11
 * @package ByTIC\Payments\Gateways\Providers\AbstractGateway\Traits
12
 *
13
 * @method MessageResponseInterface authorize(array $options = [])
14
 * @method MessageResponseInterface completeAuthorize(array $options = [])
15
 * @method MessageResponseInterface capture(array $options = [])
16
 * @method MessageResponseInterface refund(array $options = [])
17
 * @method MessageResponseInterface void(array $options = [])
18
 * @method MessageResponseInterface createCard(array $options = [])
19
 * @method MessageResponseInterface updateCard(array $options = [])
20
 * @method MessageResponseInterface deleteCard(array $options = [])
21
 */
22
trait MagicMessagesTrait
23
{
24
    protected $requestsMethods = ['purchase', 'completePurchase', 'serverCompletePurchase'];
25
26
    /**
27
     * @param $name
28
     * @param $arguments
29
     * @return RequestInterface
30
     */
31
    public function __call($name, $arguments)
32
    {
33
        if (in_array($name, $this->requestsMethods)) {
34
            $name = ucfirst($name) . 'Request';
35
            return $this->createNamespacedRequest($name, $arguments);
36
        }
37
        throw new BadMethodCallException("Invalid method $name");
38
    }
39
40
    // ------------ REQUESTS OVERLOADING ------------ //
41
    /**
42
     * @param $request
43
     * @param array $parameters
44
     * @return null|RequestInterface
45
     */
46 22
    protected function createRequestWithInternalCheck($request, array $parameters = [])
47
    {
48 22
        $return = $this->createNamespacedRequest($request, $parameters);
49 22
        if ($return) {
50 22
            return $return;
51
        }
52 1
        if (!method_exists($this, $request)) {
53
            return null;
54
        }
55
        /** @noinspection PhpUndefinedMethodInspection */
56
        /** @noinspection PhpUndefinedClassInspection */
57 1
        return parent::{$request}($parameters);
58
    }
59
60
    /**
61
     * @param $class
62
     * @param array $parameters
63
     * @return RequestInterface|null
64
     */
65 22
    protected function createNamespacedRequest($class, array $parameters)
66
    {
67 22
        if (strpos($class, 'Request') === false) {
68 10
            $class .= 'Request';
69
        }
70 22
        $class = $this->getRequestClass($class);
71
72 22
        if (class_exists($class)) {
73 22
            return $this->createRequest($class, $parameters);
0 ignored issues
show
Bug introduced by
The method createRequest() does not exist on ByTIC\Payments\Gateways\...aits\MagicMessagesTrait. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

73
            return $this->/** @scrutinizer ignore-call */ createRequest($class, $parameters);
Loading history...
74
        }
75
76 1
        return null;
77
    }
78
79
    /**
80
     * @param $class
81
     * @param array $parameters
82
     * @return RequestInterface|null
83
     * @deprecated Bad name
84
     */
85
    protected function createNamepacedRequest($class, array $parameters)
86
    {
87
        return $this->createNamespacedRequest($class, $parameters);
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->createNamespacedR...st($class, $parameters) targeting ByTIC\Payments\Gateways\...eateNamespacedRequest() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
88
    }
89
90
    /**
91
     * @param $class
92
     * @return string
93
     */
94 22
    protected function getRequestClass($class)
95
    {
96 22
        $class = $this->getNamespacePath() . '\Message\\' . ucfirst($class);
0 ignored issues
show
Bug introduced by
The method getNamespacePath() does not exist on ByTIC\Payments\Gateways\...aits\MagicMessagesTrait. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

96
        $class = $this->/** @scrutinizer ignore-call */ getNamespacePath() . '\Message\\' . ucfirst($class);
Loading history...
Bug introduced by
Are you sure $this->getNamespacePath() of type Omnipay\Common\Message\RequestInterface can be used in concatenation? Consider adding a __toString()-method. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

96
        $class = /** @scrutinizer ignore-type */ $this->getNamespacePath() . '\Message\\' . ucfirst($class);
Loading history...
97
98 22
        if (class_exists($class)) {
99 22
            return $class;
100
        }
101 1
        return str_replace('ByTIC\Payments', 'ByTIC\Common\Payments', $class);
102
    }
103
}
104