Passed
Push — master ( e75f8e...dafac1 )
by Dispositif
11:00
created

ExternRefWorker::processRefContent()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 32
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 14
nc 5
nop 1
dl 0
loc 32
rs 9.4888
c 0
b 0
f 0
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               = false;
23
    const SLEEP_AFTER_EDITION         = 30; // sec
24
    const DELAY_AFTER_LAST_HUMAN_EDIT = 15; // minutes
25
    const CHECK_EDIT_CONFLICT         = true;
26
27
    protected $botFlag = false;
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);
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
            //            $ask = readline(Color::LIGHT_MAGENTA."*** Conserver cette modif ? [y/n]".Color::NORMAL);
78
            //            if ($ask !== 'y') {
79
            //                return $refContent;
80
            //            }
81
        }
82
83
        return $result;
84
    }
85
86
}
87