Test Failed
Push — master ( 319840...0bb54c )
by Dispositif
06:21
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
cc 10
eloc 19
nc 384
nop 0
dl 0
loc 30
rs 4.5333
c 0
b 0
f 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/2020 © Philippe M. <[email protected]>
6
 * For the full copyright and MIT license information, please view the license file.
7
 */
8
declare(strict_types=1);
9
10
namespace App\Application;
11
12
use Codedungeon\PHPCliColors\Color;
13
use Throwable;
14
15
/**
16
 * Class Ref2ArticleWorker
17
 *
18
 * @package App\Application\Examples
19
 */
20
class ExternRefWorker extends RefBotWorker
21
{
22
    const TASK_BOT_FLAG                       = true;
23
    const SLEEP_AFTER_EDITION                 = 10; // sec
24
    const MINUTES_DELAY_AFTER_LAST_HUMAN_EDIT = 20; // minutes
25
    const CHECK_EDIT_CONFLICT                 = true;
26
    const ARTICLE_ANALYZED_FILENAME           = __DIR__.'/resources/article_externRef_edited.txt';
27
28
    protected $modeAuto = true;
29
    /**
30
     * @var ExternRefTransformer
31
     */
32
    protected $transformer;
33
34
    protected function setUpInConstructor(): void
35
    {
36
        $transformer = new ExternRefTransformer($this->log);
37
        $transformer->skipUnauthorised = false;
38
39
        $this->transformer = $transformer;
40
        //todo? move in __constructor + parent::__constructor()
41
    }
42
43
    // todo private (refac constructor->run())
44
45
    /**
46
     * Traite contenu d'une <ref> ou bien lien externe (précédé d'une puce).
47
     *
48
     * @param $refContent
49
     *
50
     * @return string
51
     */
52
    public function processRefContent($refContent): string
53
    {
54
        // todo // hack Temporary Skip URL
55
        if (preg_match('#books\.google#', $refContent)) {
56
            return $refContent;
57
        }
58
59
        try {
60
            $result = $this->transformer->process($refContent, $this->summary);
61
        } catch (Throwable $e) {
62
            echo "** Problème détecté 234242\n";
63
            $this->log->critical($e->getMessage()." ".$e->getFile().":".$e->getLine());
64
65
            // TODO : parse $e->message pour traitement, taskName, botflag...
66
            return $refContent;
67
        }
68
69
        if ($result === $refContent) {
70
            return $refContent;
71
        }
72
73
        // Gestion semi-auto
74
        if (!$this->transformer->skipUnauthorised) {
75
            echo Color::BG_LIGHT_RED."--".Color::NORMAL." ".$refContent."\n";
76
            echo Color::BG_LIGHT_GREEN."++".Color::NORMAL." $result \n\n";
77
78
            if (!$this->modeAuto) {
79
                $ask = readline(Color::LIGHT_MAGENTA."*** Conserver cette modif ? [y/n/auto]".Color::NORMAL);
80
                if ($ask === 'auto') {
81
                    $this->modeAuto = true;
82
                }
83
                if ($ask !== 'y' && $ask !== 'auto') {
84
                    return $refContent;
85
                }
86
            }
87
        }
88
        if (preg_match('#{{lien brisé#i', $result)) {
89
            $this->summary->memo['count lien brisé'] = 1 + ($this->summary->memo['count lien brisé'] ?? 0);
90
            $this->summary->setBotFlag(false);
91
        }
92
        if ($this->summary->citationNumber >= 8) {
93
            $this->summary->setBotFlag(false);
94
        }
95
96
        $this->summary->memo['count URL'] = 1 + ($this->summary->memo['count URL'] ?? 0);
97
98
        return $result;
99
    }
100
101
    /**
102
     * todo move to a Summary child ?
103
     * Rewriting default Summary::serialize()
104
     *
105
     * @return string
106
     */
107
    protected function generateSummaryText(): string
108
    {
109
        $prefixSummary = ($this->summary->isBotFlag()) ? 'bot ' : '';
110
        $suffix = '';
111
        if (isset($this->summary->memo['count article'])) {
112
            $suffix .= ' '.$this->summary->memo['count article'].'x {article}';
113
        }
114
        if (isset($this->summary->memo['count lien web'])) {
115
            $suffix .= ' '.$this->summary->memo['count lien web'].'x {lien web}';
116
        }
117
        if (isset($this->summary->memo['presse'])) {
118
            $suffix .= ' 🗞️'; // 🗞️ 📰
119
        }
120
        if (isset($this->summary->memo['science'])) {
121
            $suffix .= ' 🧪'; // 🧪 🔬
122
        }
123
        if (isset($this->summary->memo['count lien brisé'])) {
124
            $suffix .= ', ⚠️️️lien brisé'; //⚠️💩
125
            $suffix .= ($this->summary->memo['count lien brisé'] > 1) ? ' x'.$this->summary->memo['count lien brisé'] :
126
                '';
127
        }
128
        if (isset($this->summary->memo['accès url non libre'])) {
129
            $suffix .= ' 🔒';
130
        }
131
132
        if ($this->summary->citationNumber >= 8) {
133
            $suffix .= ' 🔥';
134
        }
135
136
        return $prefixSummary.$this->summary->taskName.$suffix;
137
    }
138
139
}
140