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

ExternPageFactory::fromURL()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 3
eloc 8
nc 3
nop 2
dl 0
loc 13
rs 10
c 1
b 0
f 1
1
<?php
2
/**
3
 * This file is part of dispositif/wikibot application (@github)
4
 * 2019/2020 © Philippe M. <[email protected]>
5
 * For the full copyright and MIT license information, please view the license file.
6
 */
7
8
declare(strict_types=1);
9
10
11
namespace App\Domain;
12
13
14
use App\Application\Http\ExternHttpClient;
15
use Psr\Log\LoggerInterface as Log;
16
17
class ExternPageFactory
18
{
19
    private function __construct() { }
20
21
    /**
22
     * @param          $url
23
     * @param Log|null $log
24
     *
25
     * @return ExternPage
26
     * @throws \Exception
27
     */
28
    public static function fromURL($url, ?Log $log = null): ExternPage
29
    {
30
        if (!ExternHttpClient::isWebURL($url)) {
31
            throw new \Exception('string is not an URL');
32
        }
33
        $adapter = new ExternHttpClient($log);
34
        $html = $adapter->getHTML($url);
35
        $html = \Normalizer::normalize($html);
36
        if (empty($html)) {
37
            throw new \DomainException('No HTML from requested URL');
38
        }
39
40
        return new ExternPage($url, $html, $log);
41
    }
42
}
43