GsuiteChecker::getHttpClient()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 0
dl 0
loc 13
rs 9.8333
c 0
b 0
f 0
1
<?php
2
3
namespace Irazasyed\GsuiteChecker;
4
5
use Generator;
6
use GuzzleHttp\Client;
7
use GuzzleHttp\Exception\RequestException;
8
use GuzzleHttp\Promise;
9
use Psr\Http\Message\ResponseInterface;
10
11
/**
12
 * Class GsuiteChecker.
13
 */
14
class GsuiteChecker
15
{
16
    /** @var array The domains */
17
    protected $domains = [];
18
19
    /** @var null|Client */
20
    protected $httpClient = null;
21
22
    /** @var int Pool Concurrency. Default: 25 */
23
    protected $concurrency = 25;
24
25
    /** @var int Connection Timeout. Default: 5 seconds */
26
    protected $connectTimeout = 5;
27
28
    /**
29
     * Create a new instance of GsuiteChecker with provided domains.
30
     *
31
     * @param mixed $domains
32
     */
33
    public function __construct($domains = [])
34
    {
35
        $this->domains = (array) $domains;
36
    }
37
38
    /**
39
     * Create a new instance if the value isn't one already.
40
     *
41
     * @param mixed $domains
42
     *
43
     * @return static
44
     */
45
    public static function make($domains = [])
46
    {
47
        return new static($domains);
48
    }
49
50
    /**
51
     * Check whether the domain(s) have a Google Suite account associated with them.
52
     *
53
     * @return $this
54
     */
55
    public function check()
56
    {
57
        Promise\each_limit(
58
            $this->getPromises(),
59
            $this->concurrency,
60
            [$this, 'responseHandler'],
61
            [$this, 'responseHandler']
62
        )->wait();
63
64
        return $this;
65
    }
66
67
    /**
68
     * Get all promises.
69
     *
70
     * @return Generator
71
     */
72
    protected function getPromises()
73
    {
74
        foreach ($this->domains as $domain) {
75
            $uri = "https://www.google.com/a/{$domain}/ServiceLogin?https://docs.google.com/a/{$domain}";
76
77
            yield $this->getHttpClient()->requestAsync('GET', $uri);
78
        }
79
    }
80
81
    /**
82
     * Get HTTP Client.
83
     *
84
     * @return Client
85
     */
86
    protected function getHttpClient()
87
    {
88
        if (null === $this->httpClient) {
89
            $this->httpClient = new Client([
90
                'connect_timeout' => $this->connectTimeout,
91
                'headers'         => [
92
                    'User-Agent' => $this->defaultUA(),
93
                ],
94
            ]);
95
        }
96
97
        return $this->httpClient;
98
    }
99
100
    /**
101
     * Default User Agent.
102
     *
103
     * @return string
104
     */
105
    protected function defaultUA()
106
    {
107
        return 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.116 Safari/537.36';
108
    }
109
110
    /**
111
     * Get all of the domains.
112
     *
113
     * @return array
114
     */
115
    public function all()
116
    {
117
        return $this->domains;
118
    }
119
120
    /**
121
     * Handle Response and Update Status of Domains.
122
     *
123
     * @param ResponseInterface|RequestException $response
124
     * @param                                    $index
125
     */
126
    public function responseHandler($response, $index)
127
    {
128
        /** @var string $domain Get the domain */
129
        $domain = $this->domains[$index];
130
131
        /* Remove it from the list */
132
        unset($this->domains[$index]);
133
134
        /** @var $status Gsuite status of the domain */
135
        $status = $this->status($response);
136
137
        /* Put the domain and status to the list */
138
        $this->domains[$domain] = $status;
139
    }
140
141
    /**
142
     * Determine status of GSuite based on response body.
143
     *
144
     * @param ResponseInterface|RequestException $response
145
     *
146
     * @return int
147
     */
148
    protected function status($response)
149
    {
150
        if ($response instanceof RequestException) {
151
            return -1;
152
        }
153
154
        return $this->strContains($response->getBody(), 'Server error') ? 0 : 1;
155
    }
156
157
    /**
158
     * Determine if a given string contains a given substring.
159
     *
160
     * @param string       $haystack
161
     * @param string|array $needles
162
     *
163
     * @return bool
164
     */
165
    protected function strContains($haystack, $needles)
166
    {
167
        foreach ((array) $needles as $needle) {
168
            if ($needle != '' && mb_strpos($haystack, $needle) !== false) {
169
                return true;
170
            }
171
        }
172
173
        return false;
174
    }
175
}
176