Passed
Push — master ( cd664b...7baf30 )
by Dispositif
13:51
created

ExternHttpErrorLogic::generateTitleFromURLText()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 8
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 Psr\Log\LoggerInterface;
13
use Psr\Log\NullLogger;
14
15
class ExternHttpErrorLogic
16
{
17
    public const LOG_REQUEST_ERROR = __DIR__ . '/../../Application/resources/external_request_error.log';
18
19
    public function __construct(
20
        protected DeadLinkTransformer $deadLinkTransformer,
21
        private readonly LoggerInterface $log = new NullLogger()
22
    )
23
    {
24
    }
25
26
    public function manageByHttpErrorMessage(string $errorMessage, string $url): string
27
    {
28
        // "410 gone" => {lien brisé}
29
        if (preg_match('#410 Gone#i', $errorMessage)) {
30
            $this->log->notice('410 Gone');
31
32
            if (ExternRefTransformer::REPLACE_410) {
33
                return $this->deadLinkTransformer->formatFromUrl($url);
34
            }
35
            return $url;
36
        } // 403
37
        elseif (preg_match('#403 Forbidden#i', $errorMessage)) {
38
            $this->log403($url);
39
40
            return $url;
41
        } elseif (preg_match('#404 Not Found#i', $errorMessage)) {
42
            $this->log->notice('404 Not Found');
43
44
            if (ExternRefTransformer::REPLACE_404) {
45
                return $this->deadLinkTransformer->formatFromUrl($url);
46
            }
47
            return $url;
48
        } elseif (preg_match('#401 Unauthorized#i', $errorMessage)) {
49
            $this->log->notice('401 Unauthorized : skip ' . $url);
50
51
            return $url;
52
        } else {
53
            //  autre : ne pas générer de {lien brisé}, car peut-être 404 temporaire
54
            $this->log->warning('erreur sur extractWebData ' . $errorMessage);
55
56
            //file_put_contents(self::LOG_REQUEST_ERROR, $this->domain."\n", FILE_APPEND);
57
58
            return $url;
59
        }
60
    }
61
62
    protected function log403(string $url): void
63
    {
64
        $this->log->warning('403 Forbidden : ' . $url);
65
        //file_put_contents(self::LOG_REQUEST_ERROR, '403 Forbidden : ' . $url . "\n", FILE_APPEND);
66
    }
67
}