BaseApiRequest::execute()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 10
c 0
b 0
f 0
nc 2
nop 0
dl 0
loc 15
ccs 10
cts 10
cp 1
crap 2
rs 9.9332
1
<?php
2
3
namespace CommerceGuys\AuthNet;
4
5
use GuzzleHttp\Client;
6
use CommerceGuys\AuthNet\DataTypes\MerchantAuthentication;
7
use CommerceGuys\AuthNet\Request\JsonRequest;
8
use CommerceGuys\AuthNet\Request\RequestInterface;
9
use CommerceGuys\AuthNet\Request\XmlRequest;
10
11
abstract class BaseApiRequest implements ApiRequestInterface
12
{
13
    /**
14
     * @var \CommerceGuys\AuthNet\Configuration
15
     */
16
    protected $configuration;
17
18
    /**
19
     * @var \GuzzleHttp\Client
20
     */
21
    protected $client;
22
23
    protected $merchantAuthentication;
24
25
    /**
26
     * BaseRequest constructor.
27
     *
28
     * @param \CommerceGuys\AuthNet\Configuration $configuration
29
     * @param \GuzzleHttp\Client $client
30
     */
31
    public function __construct(Configuration $configuration, Client $client)
32 14
    {
33
        $this->configuration = $configuration;
34 14
        $this->client = $client;
35 14
        $this->merchantAuthentication = new MerchantAuthentication([
36 14
          'name' => $this->configuration->getApiLogin(),
37 14
          'transactionKey' => $this->configuration->getTransactionKey(),
38 14
        ]);
39 14
    }
40 14
41
    public function getType()
42 12
    {
43
        return lcfirst((new \ReflectionClass($this))->getShortName());
44 12
    }
45
46
    /**
47
     * Allows child classes to attach data to the request.
48
     *
49
     * @param \CommerceGuys\AuthNet\Request\RequestInterface $request
50
     * @return RequestInterface
51
     */
52
    abstract protected function attachData(RequestInterface $request);
53
54
    public function execute()
55 13
    {
56
        if ($this->configuration->getRequestMode() == 'json') {
57 13
            $request = new JsonRequest($this->configuration, $this->client, $this->getType());
58 7
        } else {
59 7
            $request = new XmlRequest($this->configuration, $this->client, $this->getType());
60 12
        }
61
62
        $request->addDataType($this->merchantAuthentication);
63 13
        $request->addData('clientId', 'CG-PHP-SDK');
64 13
        $request->addData('refId', 'ref' . time());
65 13
        $this->attachData($request);
66 13
67
        $response = $request->sendRequest();
68 13
        return $response;
69 13
    }
70
}
71