Passed
Push — dev ( 8e8e3b...6bb8f6 )
by Dispositif
03:16 queued 15s
created

ExternHttpClient::isWebURL()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 3
eloc 3
nc 2
nop 1
dl 0
loc 7
rs 10
c 1
b 0
f 1
1
<?php
2
3
/**
4
 * This file is part of dispositif/wikibot application (@github)
5
 * 2019/2020 © Philippe M. <[email protected]>
6
 * For the full copyright and MIT license information, please view the license file.
7
 */
8
declare(strict_types=1);
9
10
namespace App\Application\Http;
11
12
use DomainException;
13
use GuzzleHttp\Client;
14
use Psr\Log\LoggerInterface;
15
16
class ExternHttpClient implements HttpClientInterface
17
{
18
    private $mapper;
0 ignored issues
show
introduced by
The private property $mapper is not used, and could be removed.
Loading history...
19
    /**
20
     * @var Client
21
     */
22
    private $client;
23
    /**
24
     * @var Logger
0 ignored issues
show
Bug introduced by
The type App\Application\Http\Logger was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
25
     */
26
    private $log;
27
28
    public function __construct(?LoggerInterface $log = null)
29
    {
30
        $this->log = $log;
0 ignored issues
show
Documentation Bug introduced by
It seems like $log can also be of type Psr\Log\LoggerInterface. However, the property $log is declared as type App\Application\Http\Logger. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
31
        $this->client = new Client(
32
            [
33
                'timeout' => 60,
34
                'allow_redirects' => true,
35
                'headers' => ['User-Agent' => getenv('USER_AGENT')],
36
                //                'proxy'           => '192.168.16.1:10',
37
            ]
38
        );
39
    }
40
41
    /**
42
     * import source from URL with Guzzle.
43
     * todo abstract + refac async request
44
     *
45
     * @param string $url
46
     *
47
     * @return string|null
48
     */
49
    public function getHTML(string $url): ?string
50
    {
51
        // todo : check banned domains ?
52
        // todo : check DNS record => ban ?
53
        // todo : accept non-ascii URL ?
54
        // idn_to_ascii($url);
55
        // idn_to_ascii('teßt.com',IDNA_NONTRANSITIONAL_TO_ASCII,INTL_IDNA_VARIANT_UTS46)
56
        // checkdnsrr($string, "A") // check DNS record
57
        if (!self::isWebURL($url)) {
58
            throw new DomainException('URL not compatible : '.$url);
59
        }
60
        $response = $this->client->get($url);
61
62
        if (200 !== $response->getStatusCode()) {
63
            $this->log->error('HTTP error '.$response->getStatusCode().' '.$response->getReasonPhrase);
0 ignored issues
show
Bug introduced by
Accessing getReasonPhrase on the interface Psr\Http\Message\ResponseInterface suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
64
65
            return null;
66
        }
67
68
        return (string)$response->getBody()->getContents() ?? '';
69
    }
70
71
    public static function isWebURL(string $url): bool
72
    {
73
        if (!filter_var($url, FILTER_VALIDATE_URL) || !preg_match('#^https?://#i', $url)) {
74
            return false;
75
        }
76
77
        return true;
78
    }
79
80
}
81