Passed
Branch master (309757)
by Dispositif
03:20 queued 54s
created

ExternRefWorker::generateSummaryText()   D

Complexity

Conditions 10
Paths 384

Size

Total Lines 30
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 19
dl 0
loc 30
rs 4.5333
c 0
b 0
f 0
cc 10
nc 384
nop 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/*
4
 * This file is part of dispositif/wikibot application (@github)
5
 * 2019-2023 © Philippe M./Irønie  <[email protected]>
6
 * For the full copyright and MIT license information, view the license file.
7
 */
8
declare(strict_types=1);
9
10
namespace App\Application\ExternLink;
11
12
use App\Application\AbstractRefBotWorker;
13
use App\Application\Http\ExternHttpClient;
14
use App\Domain\Exceptions\ConfigException;
15
use App\Domain\ExternLink\ExternRefTransformer;
16
use App\Domain\InfrastructurePorts\InternetDomainParserInterface;
17
use App\Domain\Publisher\ExternMapper;
18
use Throwable;
19
20
/**
21
 * Class Ref2ArticleWorker
22
 * @package App\Application\Examples
23
 */
24
class ExternRefWorker extends AbstractRefBotWorker
25
{
26
    public const TASK_BOT_FLAG = true;
27
    public const MAX_REFS_PROCESSED_IN_ARTICLE = 30;
28
    public const SLEEP_AFTER_EDITION = 15; // sec
29
    public const MINUTES_DELAY_AFTER_LAST_HUMAN_EDIT = 10; // minutes
30
    public const CHECK_EDIT_CONFLICT = true;
31
    public const ARTICLE_ANALYZED_FILENAME = __DIR__ . '/../resources/article_externRef_edited.txt';
32
    public const SKIP_ADQ = false;
33
    public const SKIP_LASTEDIT_BY_BOT = false;
34
    public const CITATION_NUMBER_ON_FIRE = 15;
35
    public const CITATION_NUMBER_NO_BOTFLAG = 20;
36
    public const DEAD_LINK_NO_BOTFLAG = 5;
37
    public const SKIP_SITE_BLACKLISTED = true;
38
    public const SKIP_ROBOT_NOINDEX = true;
39
40
    protected $modeAuto = true;
41
    /**
42
     * @var ExternRefTransformer
43
     */
44
    protected $transformer;
45
46
    /**
47
     * Traite contenu d'une <ref> ou bien lien externe (précédé d'une puce).
48
     *
49
     * @param $refContent
50
     *
51
     * @return string
52
     */
53
    public function processRefContent($refContent): string
54
    {
55
        // todo // hack Temporary Skip URL
56
        if (preg_match('#books\.google#', $refContent)) {
57
            return $refContent;
58
        }
59
60
        try {
61
            $result = $this->transformer->process($refContent, $this->summary);
62
        } catch (Throwable $e) {
63
            echo "** Problème détecté 234242\n";
64
            $this->log->critical($e->getMessage() . " " . $e->getFile() . ":" . $e->getLine());
65
            // TODO : parse $e->message -> variable process, taskName, botflag...
66
67
            return $refContent;
68
        }
69
70
        if (trim($result) === trim($refContent)) {
71
            return $refContent;
72
        }
73
74
        // Gestion semi-auto : todo CONDITION POURRI FAUSSE $this->transformer->skipUnauthorised
75
76
        $this->printDiff($refContent, $result, 'echo');
77
        if (!$this->autoOrYesConfirmation('Conserver cette modif ?')) {
78
            return $refContent;
79
        }
80
81
        if (preg_match('#{{lien brisé#i', $result)) {
82
            $this->summary->memo['count lien brisé'] = 1 + ($this->summary->memo['count lien brisé'] ?? 0);
83
            if ($this->summary->memo['count lien brisé'] >= self::DEAD_LINK_NO_BOTFLAG) {
84
                $this->summary->setBotFlag(false);
85
            }
86
        }
87
        if ($this->summary->citationNumber >= self::CITATION_NUMBER_NO_BOTFLAG) {
88
            $this->summary->setBotFlag(false);
89
        }
90
91
        $this->summary->memo['count URL'] = 1 + ($this->summary->memo['count URL'] ?? 0);
92
93
        return $result;
94
    }
95
96
    protected function setUpInConstructor(): void
97
    {
98
        if (!$this->domainParser instanceof InternetDomainParserInterface) {
99
            dump($this->domainParser);
100
            throw new ConfigException('DomainParser not set');
101
        }
102
        // TODO extract ExternRefTransformerInterface
103
        $this->transformer = new ExternRefTransformer(
104
            new ExternMapper($this->log),
105
            new ExternHttpClient($this->log),
106
            $this->domainParser,
107
            $this->log
108
        );
109
        $this->transformer->skipSiteBlacklisted = self::SKIP_SITE_BLACKLISTED;
110
        $this->transformer->skipRobotNoIndex = self::SKIP_ROBOT_NOINDEX;
111
        //todo? move in __constructor + parent::__constructor()
112
    }
113
114
    /**
115
     * todo move to a Summary child ?
116
     * Rewriting default Summary::serialize()
117
     * @return string
118
     */
119
    protected function generateSummaryText(): string
120
    {
121
        $prefixSummary = ($this->summary->isBotFlag()) ? 'bot ' : '';
122
        $suffix = '';
123
        if (isset($this->summary->memo['count article'])) {
124
            $suffix .= ' ' . $this->summary->memo['count article'] . 'x {article}';
125
        }
126
        if (isset($this->summary->memo['count lien web'])) {
127
            $suffix .= ' ' . $this->summary->memo['count lien web'] . 'x {lien web}';
128
        }
129
        if (isset($this->summary->memo['presse'])) {
130
            $suffix .= ' 🗞️'; // 🗞️ 📰
131
        }
132
        if (isset($this->summary->memo['science'])) {
133
            $suffix .= ' 🧪'; // 🧪 🔬
134
        }
135
        if (isset($this->summary->memo['count lien brisé'])) {
136
            $suffix .= ', ⚠️️️lien brisé'; //⚠️💩
137
            $suffix .= ($this->summary->memo['count lien brisé'] > 1) ? ' x' . $this->summary->memo['count lien brisé'] :
138
                '';
139
        }
140
        if (isset($this->summary->memo['accès url non libre'])) {
141
            $suffix .= ' 🔒';
142
        }
143
144
        if ($this->summary->citationNumber >= self::CITATION_NUMBER_ON_FIRE) {
145
            $suffix .= ' 🔥';
146
        }
147
148
        return $prefixSummary . $this->summary->taskName . $suffix;
149
    }
150
151
}
152