Completed
Pull Request — master (#30)
by
unknown
02:28
created

AzineMailgunHetrixtoolsService   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
c 1
b 0
f 0
lcom 1
cbo 1
dl 0
loc 71
ccs 0
cts 24
cp 0
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
B checkIpAddressInBlacklist() 0 24 2
A prepareBlacklistIpCheckUrl() 0 12 2
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
    public function __construct($apiKey, $url)
29
    {
30
        $this->apiKey = $apiKey;
31
        $this->blacklistIpCheckUrl = $url;
32
    }
33
34
    /**
35
     * @param string $ip
36
     * @return HetrixtoolsServiceResponse $response
37
     */
38
    public function checkIpAddressInBlacklist($ip)
39
    {
40
        $url = $this->prepareBlacklistIpCheckUrl($ip);
41
        $ch = curl_init($url);
42
43
        curl_setopt($ch, CURLOPT_HEADER, true);
44
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
45
46
        $server_output = curl_exec($ch);
47
        $info = curl_getinfo($ch);
48
        $header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
49
        curl_close($ch);
50
51
        $response = substr($server_output, $header_size);
52
53
        if($info['http_code'] != 200){
54
55
            return null;
56
        }
57
58
        $response = HetrixtoolsServiceResponse::fromJson($response);
59
60
        return $response;
61
    }
62
63
    /**
64
     * @param string $ip
65
     * @throws \Exception
66
     * @return string $url
67
     */
68
    private function prepareBlacklistIpCheckUrl($ip)
69
    {
70
        if($this->apiKey == null){
71
72
            throw new \Exception('Api key for Hetrixtools blacklist check service is not set');
73
        }
74
75
        $url = str_replace('<API_TOKEN>', $this->apiKey, $this->blacklistIpCheckUrl);
76
        $url = str_replace('<IP_ADDRESS>', $ip, $url);
77
78
        return $url;
79
    }
80
}