Completed
Push — issue/AZ_62_add_command_to_tes... ( be3d81 )
by Dominik
09:46
created

AzineMailgunHetrixtoolsService::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 2
crap 1
1
<?php
2
3
namespace Azine\MailgunWebhooksBundle\Services\HetrixtoolsService;
4
5
/**
6
 * AzineMailgunHetrixtoolsService
7
 *
8
 * This service is a wrapper for using Hetrixtools blacklist check functionality https://hetrixtools.com/.
9
 */
10
class AzineMailgunHetrixtoolsService
11
{
12
    /**
13
     * @var string
14
     */
15
    private $apiKey;
16
17
    /**
18
     * @var string
19
     */
20
    private $blacklistIpCheckUrl;
21
22
    /**
23
     * AzineMailgunHetrixtoolsService constructor.
24
     *
25
     * @param string $apiKey
26
     * @param string $url
27
     */
28 4
    public function __construct($apiKey, $url)
29
    {
30 4
        $this->apiKey = $apiKey;
31 4
        $this->blacklistIpCheckUrl = $url;
32 4
    }
33
34
    /**
35
     * @param string $ip
36
     * @return HetrixtoolsServiceResponse $response
37
     */
38 4
    public function checkIpAddressInBlacklist($ip)
39
    {
40 4
        $url = $this->prepareBlacklistIpCheckUrl($ip);
41
42 1
        $jsonResponse = $this->executeCheck($url);
43 1
        $hetrixtoolsServiceResponse = HetrixtoolsServiceResponse::fromJson($jsonResponse);
44
45 1
        return $hetrixtoolsServiceResponse;
46
    }
47
48
    /**
49
     * sends a get request to the given url
50
     *
51
     * @param  string $url
52
     * @return string $response| null
53
     */
54
    protected function executeCheck($url)
55
    {
56
        $ch = curl_init($url);
57
58
        curl_setopt($ch, CURLOPT_HEADER, true);
59
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
60
61
        $server_output = curl_exec($ch);
62
        $info = curl_getinfo($ch);
63
        $header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
64
        curl_close($ch);
65
66
        if($info['http_code'] != 200){
67
68
            return null;
69
        }
70
71
        $response = substr($server_output, $header_size);
72
73
        return $response;
74
    }
75
76
    /**
77
     * @param string $ip
78
     * @throws \InvalidArgumentException
79
     * @return string $url
80
     */
81 4
    private function prepareBlacklistIpCheckUrl($ip)
82
    {
83 4
        if ($ip == null || !filter_var($ip, FILTER_VALIDATE_IP)) {
84
85 2
            throw new \InvalidArgumentException('Given Ip address is invalid');
86
        }
87
88 2
        if($this->apiKey == null){
89
90 1
            throw new \InvalidArgumentException('Api key for Hetrixtools blacklist check service is not set');
91
        }
92
93 1
        $url = str_replace('<API_TOKEN>', $this->apiKey, $this->blacklistIpCheckUrl);
94 1
        $url = str_replace('<IP_ADDRESS>', $ip, $url);
95
96 1
        return $url;
97
    }
98
}