BotScout   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 113
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 4
dl 0
loc 113
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A multi() 0 4 1
A all() 0 4 1
A name() 0 4 1
A mail() 0 4 1
A ip() 0 4 1
A makeRequest() 0 13 2
A decodeResponse() 0 8 2
1
<?php
2
3
namespace NicolasBeauvais\BotScout;
4
5
use GuzzleHttp\Client;
6
7
class BotScout
8
{
9
    protected $client;
10
11
    protected $baseUrl = 'http://botscout.com/test/';
12
13
    private $apiKey;
14
15
    /**
16
     * @param \GuzzleHttp\Client $client
17
     * @param string $apiKey
18
     */
19
    public function __construct(Client $client, string $apiKey)
20
    {
21
        $this->client = $client;
22
        $this->apiKey = $apiKey;
23
    }
24
25
    /**
26
     * Test matches all parameters at once.
27
     *
28
     * @param string $name
29
     * @param string $mail
30
     * @param string $ip
31
     *
32
     * @return BotScoutResponse
33
     */
34
    public function multi(string $name = null, string $mail = null, string $ip = null)
35
    {
36
        return $this->makeRequest('multi', compact('name', 'mail', 'ip'));
37
    }
38
39
    /**
40
     * Test matches a single item against all fields in the botscout database.
41
     *
42
     * @param string $all
43
     *
44
     * @return BotScoutResponse
45
     */
46
    public function all(string $all)
47
    {
48
        return $this->makeRequest('multi', compact('all'));
49
    }
50
51
    /**
52
     * Test matches a name.
53
     *
54
     * @param string $name
55
     *
56
     * @return BotScoutResponse
57
     */
58
    public function name(string $name = null)
59
    {
60
        return $this->makeRequest(null, compact('name'));
61
    }
62
63
    /**
64
     * Test matches an email.
65
     *
66
     * @param string $mail
67
     *
68
     * @return BotScoutResponse
69
     */
70
    public function mail(string $mail = null)
71
    {
72
        return $this->makeRequest(null, compact('mail'));
73
    }
74
75
    /**
76
     * Test matches an ip.
77
     *
78
     * @param string $ip
79
     *
80
     * @return BotScoutResponse
81
     */
82
    public function ip(string $ip = null)
83
    {
84
        return $this->makeRequest(null, compact('ip'));
85
    }
86
87
    /**
88
     * @param array  $query
89
     *
90
     * @return BotScoutResponse
91
     */
92
    public function makeRequest(string $type = null, array $query = [])
93
    {
94
        if ($type) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $type of type null|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
95
            $query[$type] = null;
96
        }
97
98
        $query['key'] = $this->apiKey;
99
100
        return $this->decodeResponse($this->client
101
            ->get($this->baseUrl, compact('query'))
102
            ->getBody()
103
            ->getContents());
104
    }
105
106
    /**
107
     * @param string $response
108
     *
109
     * @return BotScoutResponse
110
     */
111
    private function decodeResponse(string $response)
112
    {
113
        if (strpos($response, '!') !== false) {
114
            throw new \Exception($response);
115
        }
116
117
        return new BotScoutResponse($response);
118
    }
119
}
120