Passed
Push — dev ( 043eb4...bf3609 )
by Dispositif
06:23
created

Ref2ArticleProcess   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 17
dl 0
loc 40
rs 10
c 1
b 0
f 0
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A replaceRefInText() 0 7 1
A processText() 0 20 4
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\Publisher\ArticleFromURL;
14
use App\Domain\Utils\WikiTextUtil;
15
16
/**
17
 * Class Ref2ArticleProcess
18
 *
19
 * @package App\Application
20
 */
21
class Ref2ArticleProcess
22
{
23
24
    /**
25
     * Change tous les <ref>http://libe|lemonde|figaro</ref> en {article}.
26
     *
27
     * @param $text
28
     *
29
     * @return string|string[]
30
     * @throws \Exception
31
     */
32
    public function processText($text)
33
    {
34
        $refs = WikiTextUtil::extractAllRefs($text);
35
        if (empty($refs)) {
36
            return $text;
37
        }
38
39
        foreach ($refs as $ref) {
40
            $url = WikiTextUtil::stripFinalPoint(trim($ref[1]));
41
            $converter = new ArticleFromURL(new PublisherAction($url));
42
            $articleOrLienBrise = $converter->getResult();
43
44
            if (!$articleOrLienBrise instanceof ArticleOrLienBriseInterface) {
45
                continue;
46
            }
47
            $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

47
            /** @scrutinizer ignore-call */ 
48
            $serial = $articleOrLienBrise->serialize(true);
Loading history...
48
            $text = $this->replaceRefInText($ref, $serial, $text);
49
        }
50
51
        return $text;
52
    }
53
54
    private function replaceRefInText(array $ref, string $replace, string $text)
55
    {
56
        $replace .= '.'; // ending point
57
        $result = str_replace($ref[1], $replace, $ref[0]);
58
        echo "$result \n";
59
60
        return str_replace($ref[0], $result, $text);
61
    }
62
63
}
64