Passed
Branch master (f55b85)
by Dispositif
03:58 queued 01:26
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
    /**
20
     * @var LoggerInterface
21
     */
22
    private $log;
23
24
    public function __construct(?LoggerInterface $log = null)
25
    {
26
        $this->log = $log ?? new NullLogger();
27
    }
28
29
    public function manageHttpErrors(string $errorMessage, string $url): string
30
    {
31
        // "410 gone" => {lien brisé}
32
        if (preg_match('#410 Gone#i', $errorMessage)) {
33
            $this->log->notice('410 Gone');
34
35
            return $this->formatLienBrise($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->formatLienBrise($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 formatLienBrise(string $url): string
63
    {
64
        return sprintf(
65
            '{{Lien brisé |url= %s |titre=%s |brisé le=%s}}',
66
            $url,
67
            $this->generateTitleFromURLText($url),
68
            date('d-m-Y')
69
        );
70
    }
71
72
    // todo move template
73
74
    /**
75
     * URL => "parismatch.com/People/bla…"
76
     */
77
    protected function generateTitleFromURLText(string $url): string
78
    {
79
        $text = str_replace(['https://', 'http://', 'www.'], '', $url);
80
        if (strlen($text) > 30) {
81
            $text = substr($text, 0, 30) . '…';
82
        }
83
84
        return $text;
85
    }
86
87
    protected function log403(string $url): void
88
    {
89
        $this->log->warning('403 Forbidden : ' . $url);
90
        file_put_contents(self::LOG_REQUEST_ERROR, '403 Forbidden : ' . $url . "\n", FILE_APPEND);
91
    }
92
}