Passed
Push — master ( 42303c...a9e041 )
by Dispositif
02:43
created

WebSitePeriodiqueHandler::siteNameInTitle()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 8
dl 0
loc 14
rs 10
c 1
b 0
f 0
cc 2
nc 2
nop 0
1
<?php
2
/*
3
 * This file is part of dispositif/wikibot application (@github)
4
 * 2019-2023 © Philippe M./Irønie  <[email protected]>
5
 * For the full copyright and MIT license information, view the license file.
6
 */
7
8
declare(strict_types=1);
9
10
namespace App\Domain\WikiOptimizer\Handlers;
11
12
use App\Domain\Models\Wiki\LienWebTemplate;
13
use App\Domain\Utils\WikiTextUtil;
14
use Psr\Log\LoggerInterface;
15
16
class WebSitePeriodiqueHandler implements OptimizeHandlerInterface
17
{
18
    /**
19
     * @var LienWebTemplate
20
     */
21
    protected $template;
22
    /**
23
     * @var LoggerInterface
24
     */
25
    protected $log;
26
27
    public function __construct(LienWebTemplate $template, LoggerInterface $log)
28
    {
29
        $this->template = $template;
30
        $this->log = $log;
31
    }
32
33
    public function handle()
34
    {
35
        $this->siteNameInTitle();
36
37
        if (empty($this->template->getParam('périodique'))) {
38
            return;
39
        }
40
        // doublon site - périodique
41
        if ($this->template->getParam('site') === $this->template->getParam('périodique')) {
42
            $this->template->unsetParam('périodique');
43
            $this->log->info('doublon site/périodique');
44
45
            return;
46
        }
47
48
        //quasi doublon site - périodique
49
        $periodiqueWords = strtolower(str_replace(
50
            [' ', '-'],
51
            '',
52
            $this->template->getParam('périodique')
53
        ));
54
        $siteWords = strtolower(str_replace([' ', '-'], '', $this->template->getParam('site')));
55
        var_dump($periodiqueWords, $siteWords);
0 ignored issues
show
Security Debugging Code introduced by
var_dump($periodiqueWords, $siteWords) looks like debug code. Are you sure you do not want to remove it?
Loading history...
56
        if (strpos($siteWords, $periodiqueWords) !== false) {
57
            $this->template->unsetParam('périodique');
58
            $this->log->info('quasi doublon site/périodique');
59
        }
60
    }
61
62
    /**
63
     * Supprime nom du site présent dans le titre (doublon)
64
     */
65
    private function siteNameInTitle()
66
    {
67
        // "Mali - Vidéo Dailymotion"
68
        // "bla - PubMed"
69
        $siteName = WikiTextUtil::unWikify($this->template->getParam('site') ?? '');
70
        if (empty($siteName)) {
71
            return;
72
        }
73
        $newTitle = preg_replace(
74
            '#[- ]*(vidéo|site de|site|sur) ?' . $siteName . '$#i',
75
            '',
76
            $this->template->getParam('titre')
77
        );
78
        $this->template->setParam('titre', trim($newTitle));
79
    }
80
}