Passed
Push — master ( b3f7dd...feb615 )
by Dispositif
06:47
created

LienWebOptimizer::cleanAuthor()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 7
nc 4
nop 0
dl 0
loc 14
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * This file is part of dispositif/wikibot application (@github)
4
 * 2019/2020 © 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\Domain;
11
12
use App\Domain\Utils\WikiTextUtil;
13
14
class LienWebOptimizer extends AbstractTemplateOptimizer
15
{
16
17
    public function doTasks()
18
    {
19
        $this->doublonSitePeriodique();
20
        $this->cleanAuthor();
21
        $this->siteNameInTitle();
22
23
        return $this;
24
    }
25
26
    private function doublonSitePeriodique()
27
    {
28
        // doublon site - périodique
29
        if (!empty($this->getParam('site'))
30
            && ($this->getParam('site') === $this->getParam('périodique'))
31
        ) {
32
            $this->unsetParam('site');
33
        }
34
    }
35
36
    private function cleanAuthor()
37
    {
38
        if ($this->getParam('auteur1') === 'Rédaction') {
39
            $this->unsetParam('auteur1');
40
        }
41
        // doublon auteur - site  ou doublon auteur - périodique
42
        if ((WikiTextUtil::unWikify($this->getParam('auteur1') ?? '') === WikiTextUtil::unWikify(
43
                    $this->getParam('site') ?? ''
44
                ))
45
            || (WikiTextUtil::unWikify($this->getParam('auteur1') ?? '') === WikiTextUtil::unWikify(
46
                    $this->getParam('périodique') ?? ''
47
                ))
48
        ) {
49
            $this->unsetParam('auteur1');
50
        }
51
    }
52
53
    private function siteNameInTitle()
54
    {
55
        // "Mali - Vidéo Dailymotion"
56
        // "bla - PubMed"
57
        $siteName = WikiTextUtil::unWikify($this->getParam('site') ?? '');
58
        $newTitle = preg_replace('#[- ]*(vidéo|site de|site|sur) ?'.$siteName.'$#i', '', $this->getParam('titre'));
59
        $this->setParam('titre', trim($newTitle));
60
    }
61
}
62