Passed
Push — master ( 94f1bd...3747ce )
by Jonathan
02:16
created

BaseRequest::requestOptions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 6
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 10
ccs 7
cts 7
cp 1
crap 1
rs 10
1
<?php
2
3
namespace CommerceGuys\AuthNet\Request;
4
5
use GuzzleHttp\Client;
6
use GuzzleHttp\Exception\RequestException;
7
use CommerceGuys\AuthNet\Configuration;
8
use CommerceGuys\AuthNet\DataTypes\DataTypeInterface;
9
use CommerceGuys\AuthNet\Exception\AuthNetException;
10
use CommerceGuys\AuthNet\Response\JsonResponse;
11
use CommerceGuys\AuthNet\Response\XmlResponse;
12
13
/**
14
 * Class AuthNetRequest
15
 * @package CommerceGuys\AuthNet\Request
16
 */
17
abstract class BaseRequest implements RequestInterface
18
{
19
20
    const SANDBOX = 'https://apitest.authorize.net/xml/v1/request.api';
21
    const LIVE = 'https://api2.authorize.net/xml/v1/request.api';
22
23
    /**
24
     * @var \CommerceGuys\AuthNet\Configuration
25
     */
26
    protected $configuration;
27
28
    /**
29
     * @var \GuzzleHttp\Client
30
     */
31
    protected $client;
32
33
    /**
34
     * The request type.
35
     *
36
     * For example, createTransactionRequest or authenticateTestRequest.
37
     *
38
     * @var string
39
     */
40
    protected $type;
41
42
    protected $data = [];
43
    /**
44
     * BaseRequest constructor.
45
     *
46
     * @param \CommerceGuys\AuthNet\Configuration $configuration
47
     * @param \GuzzleHttp\Client $client
48
     * @param string $type
49
     * @param array $data
50
     */
51 16
    public function __construct(Configuration $configuration, Client $client, $type, array $data = [])
52
    {
53 16
        $this->configuration = $configuration;
54 16
        $this->client = $client;
55 16
        $this->type = $type;
56 16
        $this->data = $data;
57 16
    }
58
59 16
    public function addDataType(DataTypeInterface $data)
60
    {
61 16
        $this->data[$data->getType()] = $data->toArray();
62 16
    }
63
64
    /**
65
     * @param $name
66
     * @param $value
67
     */
68 13
    public function addData($name, $value)
69
    {
70 13
        $this->data[$name] = $value;
71 13
    }
72
73
    /**
74
     * @return \CommerceGuys\AuthNet\Response\ResponseInterface
75
     * @throws \CommerceGuys\AuthNet\Exception\AuthNetException
76
     */
77 13
    public function sendRequest()
78
    {
79 13
        $postUrl = ($this->configuration->getSandbox()) ? self::SANDBOX : self::LIVE;
80 13
        $opts = $this->requestOptions();
81
        try {
82 13
            $response = $this->client->post($postUrl, $opts);
83
84 13
            if ($response->getStatusCode() != 200) {
85
                throw new AuthNetException("The request returned with error code {$response->getStatusCode()}");
86 13
            } elseif (!$response->getBody()) {
87
                throw new AuthNetException("The request did not have a body");
88
            }
89 13
        } catch (RequestException $e) {
90
            throw new AuthNetException($e->getMessage(), $e->getCode(), $e);
91
        }
92
93 13
        if ($this->configuration->getRequestMode() == 'json') {
94 7
            return new JsonResponse($response);
95
        } else {
96 12
            return new XmlResponse($response);
97
        }
98
    }
99
100
    /**
101
     * @return array
102
     */
103 13
    protected function requestOptions()
104
    {
105
        $opts = [
106 13
            'verify' => $this->configuration->getCertificateVerify(),
107
            'headers' => [
108 13
              'Content-Type' => $this->getContentType(),
109 13
            ],
110 13
            'body' => $this->getBody(),
111 13
        ];
112 13
        return $opts;
113
    }
114
}
115