Completed
Pull Request — master (#187)
by
unknown
26:48
created

ApiOperationBase::afterExecute()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 1
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 1
ccs 0
cts 0
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 0
crap 2
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\LogFactory as LogFactory;
13
14
15
abstract class ApiOperationBase implements IApiOperation
16
{
17
    /**
18
     * @var \net\authorize\api\contract\v1\AnetApiRequestType
19
     */
20
    private $apiRequest = null;
21
22
    /**
23
     * @var \net\authorize\api\contract\v1\AnetApiResponseType
24
     */
25
    private $apiResponse = null;
26
27
    /**
28
     * @var String
29
     */
30
    private $apiResponseType = '';
31
32
    /**
33
     * @var \JMS\Serializer\Serializer;
34
     */
35
    public $serializer = null;
36
37
    /**
38
     * @var \net\authorize\util\HttpClient;
39
     */
40
    public $httpClient = null;
41
42
    /**
43
     * @var \net\authorize\util\Log
44
     */
45
    private $logger;
46
47
    /**
48
     * Constructor.
49 8
     *
50
     * @param \net\authorize\api\contract\v1\AnetApiRequestType $request ApiRequest to send
51 8
     * @param string $responseType response type expected
52 8
     * @throws InvalidArgumentException if invalid request
53
     */
54 8
    public function __construct(\net\authorize\api\contract\v1\AnetApiRequestType $request, $responseType)
55
    {
56
        $this->logger = LogFactory::getLog(get_class($this));
57
58
        if ( null == $request)
59 8
        {
60
            throw new InvalidArgumentException( "request cannot be null");
61
        }
62
63
        if ( null == $responseType || '' == $responseType)
64 8
        {
65
            throw new InvalidArgumentException( "responseType cannot be null or empty");
66
        }
67
68
        if ( null != $this->apiResponse)
69 8
        {
70 8
            throw new InvalidArgumentException( "response has to be null");
71
        }
72 8
73 8
        $this->apiRequest = $request;
74
        $this->validate();
75 8
76 8
        $this->apiResponseType = $responseType;
77 8
        $this->httpClient = new HttpClient;
78 8
79
        $serializerBuilder = SerializerBuilder::create();
80
        $serializerBuilder->addMetadataDir( __DIR__ . '/../../yml/v1', 'net\authorize\api\contract\v1');//..\..\yml\v1\ //'/../lib/net/authorize/api/yml/v1'
81
        $serializerBuilder->configureHandlers(
82 8
            function (HandlerRegistryInterface $h)
83 8
84 8
            use($serializerBuilder)
85 8
            {
86
                $serializerBuilder->addDefaultHandlers();
87 8
                $h->registerSubscribingHandler(new BaseTypesHandler()); // XMLSchema List handling
88 8
                $h->registerSubscribingHandler(new XmlSchemaDateHandler()); // XMLSchema date handling
89
            }
90
        );
91
        $this->serializer = $serializerBuilder->build();
92
    }
93
94 1
    /**
95
     * Retrieves response
96 1
     * @return \net\authorize\api\contract\v1\AnetApiResponseType
97
     */
98
    public function getApiResponse()
99
    {
100
        return $this->apiResponse;
101
    }
102
103 7
    /**
104
     * Sends request and retrieves response
105 7
     * @return \net\authorize\api\contract\v1\AnetApiResponseType
106 7
     */
107
    public function executeWithApiResponse($endPoint = \net\authorize\api\constants\ANetEnvironment::CUSTOM)
108
    {
109 8
        $this->execute($endPoint);
110
        return $this->apiResponse;
111 8
    }
112
113 8
    public function execute($endPoint = \net\authorize\api\constants\ANetEnvironment::CUSTOM)
114 8
    {
115 8
        $this->beforeExecute();
116 8
117
	$this->apiRequest->setClientId("sdk-php-" . \net\authorize\api\constants\ANetEnvironment::VERSION);
118
119
        $this->logger->info("Request Serialization Begin");
120 8
        $this->logger->debug($this->apiRequest);
121 8
        $xmlRequest = $this->serializer->serialize($this->apiRequest, 'xml');
122 8
	
123
        $this->logger->info("Request  Serialization End");
124
        /*
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...
125
                //$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>  ';
126 8
        */
127 8
        $this->httpClient->setPostUrl( $endPoint);
128 8
        $xmlResponse = $this->httpClient->_sendRequest($xmlRequest);
129
        if ( null == $xmlResponse)
130 8
        {
131 8
            throw new \Exception( "Error getting valid response from api. Check log file for error details");
132
        }
133 8
        $this->logger->info("Response De-Serialization Begin");
134
        $this->apiResponse = $this->serializer->deserialize( $xmlResponse, $this->apiResponseType , 'xml');
135 8
        $this->logger->info("Response De-Serialization End");
136 8
137
        $this->afterExecute();
138
    }
139
140
    /**
141 8
     * @return \net\authorize\util\Log
142 8
     */
143
    public function getLogger()
144
    {
145
        return $this->logger;
146
    }
147
148
    private function validate()
149
    {
150
        $merchantAuthentication = $this->apiRequest->getMerchantAuthentication();
151
        if ( null == $merchantAuthentication)
152
        {
153
            throw new \InvalidArgumentException( "MerchantAuthentication cannot be null");
154
        }
155
156
        $this->validateRequest();
157
    }
158
159
    protected function beforeExecute() {}
160
    protected function afterExecute()  {}
161
    protected function validateRequest() {} //need to make this abstract
162
163
    protected function now()
164
    {
165
        return date( DATE_RFC2822);
166
    }
167
}
168