Completed
Push — master ( 678aff...0e01cf )
by recca
07:07
created

MypayGatewayFactory::getDefaultHttpClient()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 45
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 3.6816

Importance

Changes 0
Metric Value
dl 0
loc 45
ccs 15
cts 26
cp 0.5769
rs 8.8571
c 0
b 0
f 0
cc 3
eloc 26
nc 3
nop 1
crap 3.6816
1
<?php
2
3
namespace PayumTW\Mypay;
4
5
use LogicException;
6
use Payum\Core\GatewayFactory;
7
use PayumTW\Mypay\Action\SyncAction;
8
use Payum\Core\Bridge\Spl\ArrayObject;
9
use PayumTW\Mypay\Action\NotifyAction;
10
use PayumTW\Mypay\Action\StatusAction;
11
use PayumTW\Mypay\Action\CaptureAction;
12
use PayumTW\Mypay\Action\NotifyNullAction;
13
use Payum\Core\Bridge\Httplug\HttplugClient;
14
use Http\Client\Curl\Client as HttpCurlClient;
15
use PayumTW\Mypay\Action\ConvertPaymentAction;
16
use Http\Adapter\Buzz\Client as HttpBuzzClient;
17
use Http\Client\Socket\Client as HttpSocketClient;
18
use Http\Adapter\Guzzle5\Client as HttpGuzzle5Client;
19
use Http\Adapter\Guzzle6\Client as HttpGuzzle6Client;
20
use PayumTW\Mypay\Action\Api\CancelTransactionAction;
21
use PayumTW\Mypay\Action\Api\CreateTransactionAction;
22
use PayumTW\Mypay\Action\Api\RefundTransactionAction;
23
use PayumTW\Mypay\Action\Api\GetTransactionDataAction;
24
25
class MypayGatewayFactory extends GatewayFactory
26
{
27
    /**
28
     * getDefaultHttpClient.
29
     *
30
     * @param \Payum\Core\Bridge\Spl\ArrayObject $config
31
     * @return \Http\Client\HttpClient
32
     */
33
    public function getDefaultHttpClient($config)
34
    {
35
        $classes = [
36 1
            HttpGuzzle6Client::class => function () {
37 1
                return HttpGuzzle6Client::createWithConfig([
38 1
                    'verify' => false,
39
                ]);
40 1
            },
41 1
            HttpGuzzle5Client::class => function () {
42
                return new HttpGuzzle5Client([
43
                    'defaults' => [
44
                        'verify' => false,
45
                    ],
46
                ]);
47 1
            },
48 1
            HttpSocketClient::class => function () {
49
                return new HttpSocketClient(null, [
50
                    'ssl' => [
51
                        'verify_peer' => false,
52
                        'verify_peer_name' => false,
53
                    ],
54
                ]);
55 1
            },
56 1
            HttpCurlClient::class => function () use ($config) {
57
                return new HttpCurlClient($config['httplug.message_factory'], $config['httplug.stream_factory'], [
58
                    CURLOPT_SSL_VERIFYHOST => false,
59
                    CURLOPT_SSL_VERIFYPEER => false,
60
                ]);
61 1
            },
62 1
            HttpBuzzClient::class => function () {
63
                $client = new HttpBuzzClient();
64
                $client->setVerifyPeer(false);
65
66
                return $client;
67 1
            },
68
        ];
69
70 1
        foreach ($classes as $className => $closure) {
71 1
            if (class_exists($className) === true) {
72 1
                return $closure();
73
            }
74
        }
75
76
        throw new LogicException('The httplug.message_factory could not be guessed. Install one of the following packages: php-http/guzzle6-adapter, zendframework/zend-diactoros. You can also overwrite the config option with your implementation.');
77
    }
78
79
    /**
80
     * getClientIp.
81
     *
82
     * @param array $server
83
     * @return bool
84
     */
85 1
    public function getClientIp($server)
86
    {
87
        $keys = [
88 1
            'HTTP_CLIENT_IP',
89
            'HTTP_X_FORWARDED_FOR',
90
            'HTTP_X_FORWARDED',
91
            'HTTP_FORWARDED_FOR',
92
            'HTTP_FORWARDED',
93
            'REMOTE_ADDR',
94
        ];
95
96 1
        foreach ($keys as $key) {
97 1
            if (array_key_exists($key, $server) === true) {
98 1
                return $server[$key];
99
            }
100
        }
101
102 1
        return '::1';
103
    }
104
105
    /**
106
     * {@inheritdoc}
107
     */
108 1
    protected function populateConfig(ArrayObject $config)
0 ignored issues
show
Coding Style introduced by
populateConfig uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

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

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
109
    {
110 1
        $config->defaults([
111 1
            'payum.factory_name' => 'mypay',
112 1
            'payum.factory_title' => 'Mypay',
113
114 1
            'payum.action.capture' => new CaptureAction(),
115 1
            'payum.action.notify' => new NotifyAction(),
116 1
            'payum.action.notify_null' => new NotifyNullAction(),
117 1
            'payum.action.sync' => new SyncAction(),
118 1
            'payum.action.status' => new StatusAction(),
119 1
            'payum.action.convert_payment' => new ConvertPaymentAction(),
120
121 1
            'payum.action.api.create_transaction' => new CreateTransactionAction(),
122 1
            'payum.action.api.refund_transaction' => new RefundTransactionAction(),
123 1
            'payum.action.api.cancel_transaction' => new CancelTransactionAction(),
124 1
            'payum.action.api.get_transaction_data' => new GetTransactionDataAction(),
125
        ]);
126
127 1
        $httpClient = $this->getDefaultHttpClient($config);
128 1
        $config->replace([
129 1
            'httplug.client' => $httpClient,
130 1
            'payum.http_client' => new HttplugClient($httpClient),
131
        ]);
132
133 1
        $server = isset($config['server']) === true ? $config['server'] : $_SERVER;
134 1
        if (false == $config['payum.api']) {
135 1
            $config['payum.default_options'] = [
136 1
                'store_uid' => null,
137
                'key' => null,
138 1
                'ip' => $this->getClientIp($server),
139
                'sandbox' => true,
140
            ];
141
142 1
            $config->defaults($config['payum.default_options']);
143 1
            $config['payum.required_options'] = ['store_uid', 'key'];
144
145 1
            $config['payum.api'] = function (ArrayObject $config) {
146 1
                $config->validateNotEmpty($config['payum.required_options']);
147
148 1
                return new Api((array) $config, $config['payum.http_client'], $config['httplug.message_factory']);
149
            };
150
        }
151 1
    }
152
}
153