Completed
Pull Request — master (#114)
by Naman
27:16
created

HttpClient::_getPostUrl()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1
Metric Value
dl 0
loc 5
ccs 1
cts 1
cp 1
rs 9.4285
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
namespace net\authorize\util;
3
4
use net\authorize\util\LogFactory;
5
use net\authorize\util\Log;
6
7
/**
8
 * A class to send a request to the XML API.
9
 *
10
 * @package    AuthorizeNet
11
 * @subpackage net\authorize\util
12
 */
13
class HttpClient
14
{
15
    private $_Url = "";
16
17
    public $VERIFY_PEER = true; // attempt trust validation of SSL certificates when establishing secure connections.
18
    private $logger = NULL;
19
    /**
20
     * Constructor.
21
     *
22
     */
23
    public function __construct()
24 8
    {
25
        $this->logger = LogFactory::getLog(get_class($this));
26 8
        date_default_timezone_set('UTC');
27 8
    }
28 8
29 8
    /**
30
     * Set a log file.
31
     *
32
     * @param string $endPoint end point to hit from  \net\authorize\api\constants\ANetEnvironment
33
     */
34
    public function setPostUrl( $endPoint = \net\authorize\api\constants\ANetEnvironment::CUSTOM)
35
    {
36 8
        $this->_Url = sprintf( "%s/xml/v1/request.api", $endPoint);
37
    }
38 8
39 8
    /**
40
     * @return string
41
     */
42
    public function _getPostUrl()
43
    {
44 8
        //return (self::URL);
0 ignored issues
show
Unused Code Comprehensibility introduced by
75% 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...
45
        return ($this->_Url);
46
    }
47 8
48
    /**
49
     * Set a log file.
50
     *
51
     * @param string $filepath Path to log file.
52
     */
53
    public function setLogFile($filepath)
54
    {
55
        $this->logger->setLogFile($filepath);
56
    }
57
58
    /**
59
     * Posts the request to AuthorizeNet endpoint using Curl & returns response.
60
     *
61
     * @param string $xmlRequest
62
     * @return string $xmlResponse The response.
63
     */
64
    public function _sendRequest($xmlRequest)
65
    {
66 8
        $xmlResponse = "";
67
68 8
        $post_url = $this->_getPostUrl();
69
        $curl_request = curl_init($post_url);
70 8
        curl_setopt($curl_request, CURLOPT_POSTFIELDS, $xmlRequest);
71 8
        curl_setopt($curl_request, CURLOPT_HEADER, 0);
72 8
        curl_setopt($curl_request, CURLOPT_TIMEOUT, 45);
73 8
        curl_setopt($curl_request, CURLOPT_RETURNTRANSFER, 1);
74 8
        curl_setopt($curl_request, CURLOPT_SSL_VERIFYHOST, 2);
75 8
76 8
        $this->logger->info(sprintf(" Url: %s", $post_url));
77
        // Do not log requests that could contain CC info.
78 8
        $this->logger->info(sprintf("Request to AnetApi: \n%s", $xmlRequest));
79
80 8
        if ($this->VERIFY_PEER) {
81
            curl_setopt($curl_request, CURLOPT_CAINFO, dirname(dirname(__FILE__)) . '/../../ssl/cert.pem');
82 8
        } else {
83 8
            $this->logger->error("Invalid SSL option for the request");
84
            return false;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return false; (false) is incompatible with the return type documented by net\authorize\util\HttpClient::_sendRequest of type string.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

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

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
85
        }
86
87
        if (preg_match('/xml/',$post_url)) {
88
            curl_setopt($curl_request, CURLOPT_HTTPHEADER, Array("Content-Type: text/xml"));
89 8
//            file_put_contents($this->_log_file, "\nSending 'XML' Request type", FILE_APPEND);
0 ignored issues
show
Unused Code Comprehensibility introduced by
58% 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...
90 8
            $this->logger->info("Sending 'XML' Request type");
91
        }
92 8
93
        try
94
        {
95
            $this->logger->info("Sending http request via Curl");
96
            $xmlResponse = curl_exec($curl_request);
97 8
            $this->logger->info("Response from AnetApi: $xmlResponse");
98 8
99 8
        } catch (\Exception $ex)
100
        {
101
            $errorMessage = sprintf("\n%s:Error making http request via curl: Code:'%s', Message:'%s', Trace:'%s', File:'%s':'%s'",
102
                $this->now(), $ex->getCode(), $ex->getMessage(), $ex->getTraceAsString(), $ex->getFile(), $ex->getLine() );
103
            $this->logger->error($errorMessage);
104
        }
105
        if ($this->logger && $this->logger->getLogFile()) {
106
            if ($curl_error = curl_error($curl_request)) {
107 8
                $this->logger->error("CURL ERROR: $curl_error");
108 8
            }
109
110
        }
111
        curl_close($curl_request);
112
113 8
        return $xmlResponse;
114
    }
115 8
116
    private function now()
117
    {
118
        return date( DATE_RFC2822);
119
    }
120
}