Completed
Pull Request — master (#188)
by
unknown
29:19
created

ApiOperationBase   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 153
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Test Coverage

Coverage 86.27%

Importance

Changes 0
Metric Value
wmc 17
c 0
b 0
f 0
lcom 1
cbo 9
dl 0
loc 153
rs 10
ccs 44
cts 51
cp 0.8627

10 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 39 6
A getApiResponse() 0 4 1
A executeWithApiResponse() 0 5 1
B execute() 0 26 2
A setLogLevel() 0 4 1
A validate() 0 10 2
A beforeExecute() 0 1 1
A afterExecute() 0 1 1
A validateRequest() 0 1 1
A now() 0 4 1
1
<?php
2
namespace net\authorize\api\controller\base;
3
4
use InvalidArgumentException;
5
use JMS\Serializer\SerializerBuilder;
6
use JMS\Serializer\handler\HandlerRegistryInterface;
7
use GoetasWebservices\Xsd\XsdToPhpRuntime\Jms\Handler\BaseTypesHandler;
8
use GoetasWebservices\Xsd\XsdToPhpRuntime\Jms\Handler\XmlSchemaDateHandler;
9
10
use \net\authorize\util\HttpClient;
11
use \net\authorize\util\Helpers;
12
use net\authorize\util\Log;
13
use \net\authorize\util\LogFactory as LogFactory;
14
15
16
abstract class ApiOperationBase implements IApiOperation
17
{
18
    /**
19
     * @var \net\authorize\api\contract\v1\AnetApiRequestType
20
     */
21
    private $apiRequest = null;
22
23
    /**
24
     * @var \net\authorize\api\contract\v1\AnetApiResponseType
25
     */
26
    private $apiResponse = null;
27
28
    /**
29
     * @var String
30
     */
31
    private $apiResponseType = '';
32
33
    /**
34
     * @var \JMS\Serializer\Serializer;
35
     */
36
    public $serializer = null;
37
38
    /**
39
     * @var \net\authorize\util\HttpClient;
40
     */
41
    public $httpClient = null;
42
43
    /**
44
     * @var \net\authorize\util\Log
45
     */
46
    private $logger;
47
48
    /**
49 8
     * Constructor.
50
     *
51 8
     * @param \net\authorize\api\contract\v1\AnetApiRequestType $request      ApiRequest to send
52 8
     * @param string                                            $responseType response type expected
53
     * @param Log                                               $logger
54 8
     */
55
    public function __construct(\net\authorize\api\contract\v1\AnetApiRequestType $request, $responseType, Log $logger = null)
56
    {
57
        $this->logger = $logger ?: LogFactory::getLog( get_class( $this ) );
58
59 8
        if ( null == $request)
60
        {
61
            throw new InvalidArgumentException( "request cannot be null");
62
        }
63
64 8
        if ( null == $responseType || '' == $responseType)
65
        {
66
            throw new InvalidArgumentException( "responseType cannot be null or empty");
67
        }
68
69 8
        if ( null != $this->apiResponse)
70 8
        {
71
            throw new InvalidArgumentException( "response has to be null");
72 8
        }
73 8
74
        $this->apiRequest = $request;
75 8
        $this->validate();
76 8
77 8
        $this->apiResponseType = $responseType;
78 8
        $this->httpClient = new HttpClient;
79
80
        $serializerBuilder = SerializerBuilder::create();
81
        $serializerBuilder->addMetadataDir( __DIR__ . '/../../yml/v1', 'net\authorize\api\contract\v1');//..\..\yml\v1\ //'/../lib/net/authorize/api/yml/v1'
82 8
        $serializerBuilder->configureHandlers(
83 8
            function (HandlerRegistryInterface $h)
84 8
85 8
            use($serializerBuilder)
86
            {
87 8
                $serializerBuilder->addDefaultHandlers();
88 8
                $h->registerSubscribingHandler(new BaseTypesHandler()); // XMLSchema List handling
89
                $h->registerSubscribingHandler(new XmlSchemaDateHandler()); // XMLSchema date handling
90
            }
91
        );
92
        $this->serializer = $serializerBuilder->build();
93
    }
94 1
95
    /**
96 1
     * Retrieves response
97
     * @return \net\authorize\api\contract\v1\AnetApiResponseType
98
     */
99
    public function getApiResponse()
100
    {
101
        return $this->apiResponse;
102
    }
103 7
104
    /**
105 7
     * Sends request and retrieves response
106 7
     * @return \net\authorize\api\contract\v1\AnetApiResponseType
107
     */
108
    public function executeWithApiResponse($endPoint = \net\authorize\api\constants\ANetEnvironment::CUSTOM)
109 8
    {
110
        $this->execute($endPoint);
111 8
        return $this->apiResponse;
112
    }
113 8
114 8
    public function execute($endPoint = \net\authorize\api\constants\ANetEnvironment::CUSTOM)
115 8
    {
116 8
        $this->beforeExecute();
117
118
	$this->apiRequest->setClientId("sdk-php-" . \net\authorize\api\constants\ANetEnvironment::VERSION);
119
120 8
        $this->logger->info("Request Serialization Begin");
121 8
        $this->logger->debug($this->apiRequest);
122 8
        $xmlRequest = $this->serializer->serialize($this->apiRequest, 'xml');
123
	
124
        $this->logger->info("Request  Serialization End");
125
        /*
0 ignored issues
show
Unused Code Comprehensibility introduced by
43% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
126 8
                //$xmlRequest = '<?xml version="1.0" encoding="UTF-8"?>  <ARBGetSubscriptionListRequest xmlns="AnetApi/xml/v1/schema/AnetApiSchema.xsd">  <merchantAuthentication>  <name>4YJmeW7V77us</name>  <transactionKey>4qHK9u63F753be4Z</transactionKey>  </merchantAuthentication>  <refId><![CDATA[ref1416999093]]></refId>  <searchType><![CDATA[subscriptionActive]]></searchType>  <sorting>  <orderBy><![CDATA[firstName]]></orderBy>  <orderDescending>false</orderDescending>  </sorting>  <paging>  <limit>10</limit>  <offset>1</offset>  </paging>  </ARBGetSubscriptionListRequest>  ';
127 8
        */
128 8
        $this->httpClient->setPostUrl( $endPoint);
129
        $xmlResponse = $this->httpClient->_sendRequest($xmlRequest);
130 8
        if ( null == $xmlResponse)
131 8
        {
132
            throw new \Exception( "Error getting valid response from api. Check log file for error details");
133 8
        }
134
        $this->logger->info("Response De-Serialization Begin");
135 8
        $this->apiResponse = $this->serializer->deserialize( $xmlResponse, $this->apiResponseType , 'xml');
136 8
        $this->logger->info("Response De-Serialization End");
137
138
        $this->afterExecute();
139
    }
140
141 8
    /**
142 8
     * @return \net\authorize\util\Log
143
     */
144
    public function setLogLevel()
145
    {
146
        return $this->logger;
147
    }
148
149
    private function validate()
150
    {
151
        $merchantAuthentication = $this->apiRequest->getMerchantAuthentication();
152
        if ( null == $merchantAuthentication)
153
        {
154
            throw new \InvalidArgumentException( "MerchantAuthentication cannot be null");
155
        }
156
157
        $this->validateRequest();
158
    }
159
160
    protected function beforeExecute() {}
161
    protected function afterExecute()  {}
162
    protected function validateRequest() {} //need to make this abstract
163
164
    protected function now()
165
    {
166
        return date( DATE_RFC2822);
167
    }
168
}
169