Completed
Push — master ( e74ef3...7f23cc )
by Adrien
15:26 queued 08:10
created

Web   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 18
c 1
b 0
f 0
dl 0
loc 43
rs 10
wmc 6

1 Method

Rating   Name   Duplication   Size   Complexity  
A WEBSERVICE() 0 31 6
1
<?php
2
3
namespace PhpOffice\PhpSpreadsheet\Calculation;
4
5
use GuzzleHttp\Psr7\Request;
6
use PhpOffice\PhpSpreadsheet\Settings;
7
use Psr\Http\Client\ClientExceptionInterface;
8
9
class Web
10
{
11
    /**
12
     * WEBSERVICE.
13
     *
14
     * Returns data from a web service on the Internet or Intranet.
15
     *
16
     * Excel Function:
17
     *        Webservice(url)
18
     *
19
     * @return string the output resulting from a call to the webservice
20
     */
21
    public static function WEBSERVICE(string $url)
22
    {
23
        $url = trim($url);
24
        if (strlen($url) > 2048) {
25
            return Functions::VALUE(); // Invalid URL length
26
        }
27
28
        if (!preg_match('/^http[s]?:\/\//', $url)) {
29
            return Functions::VALUE(); // Invalid protocol
30
        }
31
32
        // Get results from the the webservice
33
        $client = Settings::getHttpClient();
34
        $request = new Request('GET', $url);
35
36
        try {
37
            $response = $client->sendRequest($request);
38
        } catch (ClientExceptionInterface $e) {
39
            return Functions::VALUE(); // cURL error
40
        }
41
42
        if ($response->getStatusCode() != 200) {
43
            return Functions::VALUE(); // cURL error
44
        }
45
46
        $output = (string) $response->getBody();
47
        if (strlen($output) > 32767) {
48
            return Functions::VALUE(); // Output not a string or too long
49
        }
50
51
        return $output;
52
    }
53
}
54