Passed
Push — dev ( 863855...4ef148 )
by Dispositif
02:53
created

Ref2ArticleProcess   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 21
c 2
b 0
f 0
dl 0
loc 53
rs 10
wmc 7

3 Methods

Rating   Name   Duplication   Size   Complexity  
A replaceRefInText() 0 7 1
A hasWarning() 0 3 1
A processText() 0 24 5
1
<?php
2
/**
3
 * This file is part of dispositif/wikibot application
4
 * 2019 © Philippe M. <[email protected]>
5
 * For the full copyright and MIT license information, please view the LICENSE file.
6
 */
7
8
declare(strict_types=1);
9
10
namespace App\Application;
11
12
use App\Domain\Models\Wiki\ArticleOrLienBriseInterface;
13
use App\Domain\Models\Wiki\LienBriseTemplate;
14
use App\Domain\Publisher\ArticleFromURL;
15
use App\Domain\Utils\WikiTextUtil;
16
17
/**
18
 * Class Ref2ArticleProcess
19
 *
20
 * @package App\Application
21
 */
22
class Ref2ArticleProcess
23
{
24
    /**
25
     * @var bool
26
     */
27
    private $warning = false;
28
29
    /**
30
     * Change tous les <ref>http://libe|lemonde|figaro</ref> en {article}.
31
     *
32
     * @param $text
33
     *
34
     * @return string|string[]
35
     * @throws \Exception
36
     */
37
    public function processText($text)
38
    {
39
        $refs = WikiTextUtil::extractAllRefs($text);
40
        if (empty($refs)) {
41
            return $text;
42
        }
43
44
        foreach ($refs as $ref) {
45
            $url = WikiTextUtil::stripFinalPoint(trim($ref[1]));
46
            $converter = new ArticleFromURL(new PublisherAction($url));
47
            $articleOrLienBrise = $converter->getResult();
48
49
            if (!$articleOrLienBrise instanceof ArticleOrLienBriseInterface) {
50
                continue;
51
            }
52
            if($articleOrLienBrise instanceof LienBriseTemplate){
53
                $this->warning = true;
54
            }
55
56
            $serial = $articleOrLienBrise->serialize(true);
0 ignored issues
show
Bug introduced by
The method serialize() does not exist on App\Domain\Models\Wiki\ArticleOrLienBriseInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to App\Domain\Models\Wiki\ArticleOrLienBriseInterface. ( Ignorable by Annotation )

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

56
            /** @scrutinizer ignore-call */ 
57
            $serial = $articleOrLienBrise->serialize(true);
Loading history...
57
            $text = $this->replaceRefInText($ref, $serial, $text);
58
        }
59
60
        return $text;
61
    }
62
63
    public function hasWarning():bool
64
    {
65
        return (bool)$this->warning;
66
    }
67
68
    private function replaceRefInText(array $ref, string $replace, string $text)
69
    {
70
        $replace .= '.'; // ending point
71
        $result = str_replace($ref[1], $replace, $ref[0]);
72
        echo "$result \n";
73
74
        return str_replace($ref[0], $result, $text);
75
    }
76
77
}
78