AzineMailgunHetrixtoolsService::__construct()   A
last analyzed

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
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 3
dl 0
loc 5
ccs 4
cts 4
cp 1
crap 1
rs 10
1
<?php
2
3
namespace Azine\MailgunWebhooksBundle\Services\HetrixtoolsService;
4
5
use Psr\Log\LoggerInterface;
6
7
/**
8
 * AzineMailgunHetrixtoolsService.
9
 *
10
 * This service is a wrapper for using Hetrixtools blacklist check functionality https://hetrixtools.com/.
11
 */
12
class AzineMailgunHetrixtoolsService
13
{
14
    /**
15
     * @var string
16
     */
17
    private $apiKey;
18
19
    /**
20
     * @var string
21
     */
22
    private $blacklistIpCheckUrl;
23
24
    /**
25
     * @var LoggerInterface
26
     */
27
    private $logger;
28
29
    /**
30
     * AzineMailgunHetrixtoolsService constructor.
31
     *
32
     * @param LoggerInterface $logger
33
     * @param string          $apiKey
34
     * @param string          $url
35
     */
36 4
    public function __construct(LoggerInterface $logger, $apiKey, $url)
37
    {
38 4
        $this->apiKey = $apiKey;
39 4
        $this->blacklistIpCheckUrl = $url;
40 4
        $this->logger = $logger;
41 4
    }
42
43
    /**
44
     * @param string $ip
45
     *
46
     * @return HetrixtoolsServiceResponse $response
47
     */
48 4
    public function checkIpAddressInBlacklist($ip)
49
    {
50 4
        $url = $this->prepareBlacklistIpCheckUrl($ip);
51
52 1
        $jsonResponse = $this->executeCheck($url);
53 1
        $hetrixtoolsServiceResponse = HetrixtoolsServiceResponse::fromJson($jsonResponse);
54
55 1
        return $hetrixtoolsServiceResponse;
56
    }
57
58
    /**
59
     * sends a get request to the given url.
60
     *
61
     * @param string $url
62
     *
63
     * @return string $response| null
64
     */
65
    protected function executeCheck($url)
66
    {
67
        $this->logger->debug("Calling $url to check HetrixTools for SPAM-List entries.");
68
        $ch = curl_init($url);
69
70
        curl_setopt($ch, CURLOPT_HEADER, true);
71
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
72
73
        $server_output = curl_exec($ch);
74
        $info = curl_getinfo($ch);
75
        $header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
76
        curl_close($ch);
77
78
        $this->logger->debug('Hetrix returned: '.print_r($info, true));
0 ignored issues
show
Bug introduced by
Are you sure print_r($info, true) of type string|true can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

78
        $this->logger->debug('Hetrix returned: './** @scrutinizer ignore-type */ print_r($info, true));
Loading history...
79
80
        if (200 != $info['http_code']) {
81
            return null;
82
        }
83
84
        $response = substr($server_output, $header_size);
0 ignored issues
show
Bug introduced by
It seems like $server_output can also be of type true; however, parameter $string of substr() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

84
        $response = substr(/** @scrutinizer ignore-type */ $server_output, $header_size);
Loading history...
85
86
        return $response;
87
    }
88
89
    /**
90
     * @param string $ip
91
     *
92
     * @throws \InvalidArgumentException
93
     *
94
     * @return string $url
95
     */
96 4
    private function prepareBlacklistIpCheckUrl($ip)
97
    {
98 4
        if (null == $ip || !filter_var($ip, FILTER_VALIDATE_IP)) {
99 2
            throw new \InvalidArgumentException('Given Ip address is invalid');
100
        }
101
102 2
        if (null == $this->apiKey) {
103 1
            throw new \InvalidArgumentException('Api key for Hetrixtools blacklist check service is not set');
104
        }
105
106 1
        $url = str_replace('<API_TOKEN>', $this->apiKey, $this->blacklistIpCheckUrl);
107 1
        $url = str_replace('<IP_ADDRESS>', $ip, $url);
108
109 1
        return $url;
110
    }
111
}
112