Completed
Push — master ( afd149...4a584e )
by Andrii
01:51
created

AbstractMerchant::request()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 3
Bugs 0 Features 1
Metric Value
c 3
b 0
f 1
dl 0
loc 9
ccs 0
cts 7
cp 0
rs 9.6666
cc 1
eloc 6
nc 1
nop 2
crap 2
1
<?php
2
3
/*
4
 * Generalization over Omnipay and Payum
5
 *
6
 * @link      https://github.com/hiqdev/php-merchant
7
 * @package   php-merchant
8
 * @license   BSD-3-Clause
9
 * @copyright Copyright (c) 2015-2016, HiQDev (http://hiqdev.com/)
10
 */
11
12
namespace hiqdev\php\merchant;
13
14
abstract class AbstractMerchant implements MerchantInterface
15
{
16
    /**
17
     * @var string Unique merchant identification. E.g. paypal, webmoney_usd, webmoney_rub.
18
     */
19
    public $id;
20
21
    public $library;
22
23
    /**
24
     * Gateway name, corresponding to Omnipay namespace. E.g. PayPal, WebMoney, YandexMoney.
25
     */
26
    public $gateway;
27
28
    /**
29
     * @var array Data that will be passed to Request
30
     * @see request
31
     */
32
    public $data = [];
33
34
    /**
35
     * @var string request class name.
36
     */
37
    public $requestClass;
38
39
    /**
40
     * @var string response class name.
41
     */
42
    public $responseClass;
43
44
    /**
45
     * @return string
46
     */
47 1
    public function getLabel()
48
    {
49 1
        return $this->gateway;
50
    }
51
52
    /**
53
     * Returns simplified name.
54
     *
55
     * @return string
56
     */
57 1
    public function getSimpleName()
58
    {
59 1
        return preg_replace('/[^a-z0-9]+/', '', strtolower($this->gateway));
60
    }
61
62
    /**
63
     * {@inheritdoc}
64
     * @return AbstractRequest
65
     */
66
    public function request($type, array $data)
67
    {
68
        return Helper::createObject([
69
            'class'     => $this->getRequestClass(),
70
            'merchant'  => $this,
71
            'type'      => $type,
72
            'data'      => array_merge((array) $this->data, (array) $data),
73
        ]);
74
    }
75
76
    /**
77
     * @param RequestInterface $request
78
     * @return AbstractResponse
79
     */
80
    public function response(RequestInterface $request)
81
    {
82
        return Helper::createObject([
83
            'class'     => $this->getResponseClass(),
84
            'merchant'  => $this,
85
            'request'   => $request,
86
        ]);
87
    }
88
89
    /**
90
     * @return string
91
     */
92
    public function getRequestClass()
93
    {
94
        return $this->requestClass ?: Helper::findClass($this->gateway, $this->library, 'Request');
95
    }
96
97
    /**
98
     * @return string
99
     */
100
    public function getResponseClass()
101
    {
102
        return $this->responseClass ?: Helper::findClass($this->gateway, $this->library, 'Response');
103
    }
104
}
105