Passed
Push — master ( 2f16d6...f55b85 )
by Dispositif
02:32
created

CheckURL::getRegistrableDomain()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 4
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 7
rs 10
1
<?php
2
/*
3
 * This file is part of dispositif/wikibot application (@github)
4
 * 2019-2023 © Philippe M./Irønie  <[email protected]>
5
 * For the full copyright and MIT license information, view the license file.
6
 */
7
8
declare(strict_types=1);
9
10
namespace App\Domain\ExternLink;
11
12
use App\Application\Http\ExternHttpClient;
13
use App\Infrastructure\InternetDomainParser;
14
use Exception;
15
use Psr\Log\LoggerInterface;
16
use Psr\Log\NullLogger;
17
18
/**
19
 * Todo move infra ?
20
 */
21
class CheckURL
22
{
23
    /**
24
     * @var LoggerInterface
25
     */
26
    protected $log;
27
28
    /**
29
     * @var string
30
     */
31
    protected $registrableDomain;
32
    /**
33
     * @var string
34
     */
35
    protected $url;
36
37
    public function __construct(?LoggerInterface $logger = null)
38
    {
39
        $this->log = $logger ?? new NullLogger();
40
    }
41
42
    public function isURLAuthorized(string $url): bool
43
    {
44
        $this->url = $url;
45
        $this->registrableDomain = null;
46
        if (!ExternHttpClient::isHttpURL($url)) {
47
            $this->log->debug('Skip : not a valid URL : ' . $url);
48
            return false;
49
        }
50
51
        if ($this->hasForbiddenFilenameExtension()) {
52
            return false;
53
        }
54
        if (!ExternHttpClient::isHttpURL($url)) {
55
            throw new Exception('string is not an URL ' . $url);
56
        }
57
58
        $this->findRegistrableDomain();
59
60
        return true;
61
    }
62
63
    public function getRegistrableDomain($url): ?string
64
    {
65
        if ($url === $this->url && $this->registrableDomain) {
66
            return $this->registrableDomain;
67
        }
68
        $this->url = $url;
69
        return $this->findRegistrableDomain();
70
    }
71
72
    protected function findRegistrableDomain(): ?string
73
    {
74
        try {
75
            $this->registrableDomain = (new InternetDomainParser())->getRegistrableDomainFromURL($this->url);
76
        } catch (Exception $e) {
77
            $this->log->warning('Skip : not a valid URL : ' . $this->url);
78
            return null;
79
        }
80
        return $this->registrableDomain;
81
    }
82
83
    /**
84
     * todo move URL parsing
85
     * Skip PDF GIF etc
86
     * https://fr.wikipedia.org/wiki/Liste_d%27extensions_de_fichiers
87
     *
88
     * @param string $url
89
     *
90
     * @return bool
91
     */
92
    protected function hasForbiddenFilenameExtension(): bool
93
    {
94
        return (bool)preg_match(
95
            '#\.(pdf|jpg|jpeg|gif|png|xls|xlsx|xlr|xml|xlt|txt|csv|js|docx|exe|gz|zip|ini|movie|mp3|mp4|ogg|raw|rss|tar|tgz|wma)$#i',
96
            $this->url
97
        );
98
    }
99
}