DataTrait::appendData()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 1
dl 0
loc 13
ccs 8
cts 8
cp 1
crap 2
rs 9.8333
c 0
b 0
f 0
1
<?php
2
3
namespace Omnipay\BillPay\Message\RequestData;
4
5
use Omnipay\BillPay\Message\AuthorizeRequest;
6
use Omnipay\Common\Exception\InvalidRequestException;
7
use Omnipay\Common\Message\AbstractRequest;
8
use SimpleXMLElement;
9
10
/**
11
 * Class DataTrait
12
 *
13
 * @author    Andreas Lange <[email protected]>
14
 * @copyright 2016, Connox GmbH
15
 * @license   MIT
16
 */
17
trait DataTrait
18
{
19
    private static $paymentTypes = [
20
        AuthorizeRequest::PAYMENT_TYPE_INVOICE => 1,
21
        AuthorizeRequest::PAYMENT_TYPE_DIRECT_DEBIT => 2,
22
        AuthorizeRequest::PAYMENT_TYPE_TRANSACTION_CREDIT => 3,
23
        AuthorizeRequest::PAYMENT_TYPE_PAY_LATER => 4,
24
        AuthorizeRequest::PAYMENT_TYPE_COLLATERAL_PROMISE => 7,
25
    ];
26
27
    /**
28
     * @return int
29
     */
30 11
    public function getCaptureRequestNecessary()
31
    {
32 11
        return (int)$this->getParameter('captureRequestNecessary');
33
    }
34
35
    /**
36
     * Gets the expected delay in shipping
37
     *
38
     * @return int
39
     */
40 11
    public function getExpectedDaysTillShipping()
41
    {
42 11
        return (int)$this->getParameter('expectedDaysTillShipping');
43
    }
44
45
    /**
46
     * Get the payment issuer.
47
     *
48
     * @return string
49
     *
50
     * @codeCoverageIgnore
51
     */
52
    abstract public function getPaymentMethod();
53
54
    /**
55
     * @param int|bool $value
56
     *
57
     * @return AuthorizeRequest
58
     */
59 2
    public function setCaptureRequestNecessary($value)
60
    {
61 2
        return $this->setParameter('captureRequestNecessary', $value ? 1 : 0);
62
    }
63
64
    /**
65
     * Sets the expected delay in shipping, required for authorize and pay
66
     *
67
     * @param int $value the expected delay in shipping
68
     *
69
     * @return AuthorizeRequest
70
     */
71 11
    public function setExpectedDaysTillShipping($value)
72
    {
73 11
        return $this->setParameter('expectedDaysTillShipping', $value);
74
    }
75
76
    /**
77
     * @param SimpleXMLElement $data
78
     *
79
     * @throws InvalidRequestException
80
     */
81 12
    protected function appendData(SimpleXMLElement $data)
82
    {
83 12
        if (!$this->getPaymentMethod()) {
84 1
            throw new InvalidRequestException('This request requires a payment method.');
85
        }
86
87
        // the customer has accepted the BillPay terms of service / the data protection policy, we assume that the
88
        // gateway is only used after acceptance
89 11
        $data['tcaccepted'] = 1;
90 11
        $data['expecteddaystillshipping'] = $this->getExpectedDaysTillShipping();
91 11
        $data['capturerequestnecessary'] = $this->getCaptureRequestNecessary();
92 11
        $data['paymenttype'] = self::$paymentTypes[$this->getPaymentMethod()];
93 11
    }
94
95
    /**
96
     * Get a single parameter.
97
     *
98
     * @param string $key The parameter key
99
     *
100
     * @return mixed
101
     *
102
     * @codeCoverageIgnore
103
     */
104
    abstract protected function getParameter($key);
105
106
    /**
107
     * Set a single parameter
108
     *
109
     * @param string $key   The parameter key
110
     * @param mixed  $value The value to set
111
     *
112
     * @return AbstractRequest Provides a fluent interface
113
     *
114
     * @codeCoverageIgnore
115
     */
116
    abstract protected function setParameter($key, $value);
117
}
118