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

DeadLinkTransformer::externRefProcessOnArchive()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 11
dl 0
loc 18
rs 9.9
c 1
b 0
f 0
cc 3
nc 4
nop 1
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\Domain\InfrastructurePorts\DeadlinkArchiverInterface;
14
use App\Domain\InfrastructurePorts\InternetDomainParserInterface;
15
use App\Domain\Models\Summary;
16
use App\Domain\Models\WebarchiveDTO;
17
use App\Domain\Publisher\ExternMapper;
18
use App\Infrastructure\InternetDomainParser;
19
use Psr\Log\LoggerInterface;
20
use Psr\Log\NullLogger;
21
22
/**
23
 * Transform dead link url in {lien brisé}.
24
 * TODO : check wikiwix and return {lien web|url=wikiwix…
25
 */
26
class DeadLinkTransformer
27
{
28
    public function __construct(
29
        protected ?DeadlinkArchiverInterface     $archiver = null,
30
        protected ?InternetDomainParserInterface $domainParser = null,
31
        protected ?ExternRefTransformerInterface $externRefTransformer = null,
32
        protected LoggerInterface                $log = new NullLogger()
33
    )
34
    {
35
    }
36
37
    public function formatFromUrl(string $url, \DateTimeInterface $now = new \DateTimeImmutable()): string
38
    {
39
        if ($this->archiver instanceof DeadlinkArchiverInterface) {
40
            $webarchive = $this->archiver->searchWebarchive($url);
41
            if ($webarchive instanceof WebarchiveDTO) {
42
                $this->log->notice('🥝wikiwix found');
43
                return $this->generateLienWebFromArchive($webarchive);
44
            }
45
            $this->log->notice('wikiwix not found');
46
        }
47
48
        return $this->generateLienBrise($url, $now);
49
    }
50
51
    private function generateLienWebFromArchive(WebarchiveDTO $dto): string
52
    {
53
        sleep(1);
54
55
        return $this->externRefProcessOnArchive($dto);
56
57
        // OLD SOLUTION without a second GET request to wikiwix (todo make an switch option to the current class?)
58
//        return sprintf(
59
//            '{{Lien web |url= %s |titre=%s |site= %s |consulté le=%s |archive-date=%s}}',
60
//            $dto->getArchiveUrl(),
61
//            'Archive '. $this->generateTitleFromURLText($dto->getOriginalUrl()),
62
//            $dto->getArchiver(),
63
//            date('d-m-Y'),
64
//            $dto->getArchiveDate() ? $dto->getArchiveDate()->format('d-m-Y') : ''
65
//        );
66
    }
67
68
    /**
69
     * To extract the title+author+lang+… from the webarchive page.
70
     */
71
    private function externRefProcessOnArchive(WebarchiveDTO $dto): string
72
    {
73
        $summary = new Summary('test');
74
        if (!$this->externRefTransformer instanceof ExternRefTransformerInterface) {
75
            $this->externRefTransformer = new ExternRefTransformer(
76
                new ExternMapper($this->log),
77
                new ExternHttpClient($this->log),
78
                new InternetDomainParser(),
79
                $this->log,
80
                null
81
            ); // todo inverse dependency
82
        }
83
84
        $options = $this->domainParser
85
            ? ['originalRegistrableDomain' => $this->domainParser->getRegistrableDomainFromURL($dto->getOriginalUrl())]
86
            : [];
87
88
        return $this->externRefTransformer->process($dto->getArchiveUrl(), $summary, $options);
0 ignored issues
show
Bug introduced by
The method process() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

88
        return $this->externRefTransformer->/** @scrutinizer ignore-call */ process($dto->getArchiveUrl(), $summary, $options);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
89
    }
90
91
    protected function generateLienBrise(string $url, \DateTimeInterface $now): string
92
    {
93
        return sprintf(
94
            '{{Lien brisé |url= %s |titre=%s |brisé le=%s}}',
95
            $url,
96
            $this->generateTitleFromURLText($url),
97
            $now->format('d-m-Y')
98
        );
99
    }
100
101
    protected function generateTitleFromURLText(string $url): string
102
    {
103
        $text = str_replace(['https://', 'http://', 'www.'], '', $url);
104
        if (strlen($text) > 30) {
105
            $text = substr($text, 0, 30) . '…';
106
        }
107
108
        return $text;
109
    }
110
}