Completed
Pull Request — master (#5)
by Haralan
09:48 queued 08:13
created

RefundRequest::getEndpoint()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Omnipay\Emp\Message;
4
5
/**
6
 * @author    Ivan Kerin <[email protected]>
7
 * @copyright 2014, Clippings Ltd.
8
 * @license   http://spdx.org/licenses/BSD-3-Clause
9
 */
10
class RefundRequest extends AbstractRequest
11
{
12
    /**
13
     * @return string
14
     */
15 1
    public function getEndpoint()
16
    {
17 1
        return 'https://my.emerchantpay.com/service/order/credit';
18
    }
19
20
    /**
21
     * @return array
22
     */
23 1
    public function getRequestData()
24
    {
25 1
        return $this->getParameter('requestData');
26
    }
27
28
    /**
29
     * @param array $value
30
     */
31 1
    public function setRequestData(array $value)
32
    {
33 1
        return $this->setParameter('requestData', $value);
34
    }
35
36
    /**
37
     * @param  string $code
38
     * @return string
39
     */
40 1
    public function getRequestDataItem($code)
41
    {
42 1
        $data = $this->getRequestData();
43
44 1
        if (isset($data['cart']['item'])) {
45 1
            $items = $data['cart']['item'];
46
47 1
            if (isset($items['code']) and $items['code'] == $code) {
48 1
                return $items['id'];
49
            }
50
51 1
            foreach ($items as $item) {
52 1
                if (isset($item['code']) and $item['code'] == $code) {
53 1
                    return $item['id'];
54
                }
55
            }
56
        }
57
58
        return $code;
59
    }
60
61
    /**
62
     * @param  mixed $data
63
     * @return \Omnipay\Emp\Message\RefundResponse
64
     */
65 1
    public function sendData($data)
66
    {
67 1
        $responseData = parent::sendData($data);
68
69 1
        $this->response = new RefundResponse($this, $responseData);
70
71 1
        return $this->response;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->response; (Omnipay\Emp\Message\RefundResponse) is incompatible with the return type of the parent method Omnipay\Emp\Message\AbstractRequest::sendData of type array.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
72
    }
73
74
    /**
75
     * @return array
76
     */
77 2
    public function getData()
78
    {
79 2
        $data = parent::getData();
80
81 2
        $this->validate('transactionReference', 'transactionId');
82
83 2
        $items = $this->getItems();
84
85 2
        if ($items) {
86 1
            $this->validate('requestData');
87
88 1
            foreach ($items as $index => $item) {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
89
90 1
                $i = $index + 1;
91
92 1
                $data["item_{$i}_id"] = $this->getRequestDataItem($item->getName());
93 1
                $data["item_{$i}_amount"] = $item->getPrice();
94
            }
95
        } else {
96 1
            $this->validate('amount');
97
98 1
            $data['amount'] = $this->getAmount();
99
        }
100
101 2
        $data['reason'] = $this->getDescription();
102 2
        $data['order_id'] = $this->getTransactionId();
103 2
        $data['trans_id'] = $this->getTransactionReference();
104
105 2
        return $data;
106
    }
107
}
108