Passed
Branch dev3 (ae391d)
by Dispositif
02:29
created

CitationValidator   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 9
eloc 28
dl 0
loc 67
rs 10
c 1
b 0
f 1

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A validate() 0 21 5
A stringFound() 0 11 2
A isTextCreatingError() 0 4 1
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
11
namespace App\Application\OuvrageEdit\Validators;
12
13
14
use App\Application\InfrastructurePorts\DbAdapterInterface;
15
use App\Domain\Utils\WikiTextUtil;
16
use Psr\Log\LoggerInterface;
17
18
class CitationValidator implements ValidatorInterface
19
{
20
    /**
21
     * @var array
22
     */
23
    protected $ouvrageData;
24
    /**
25
     * @var LoggerInterface
26
     */
27
    protected $log;
28
    /**
29
     * @var DbAdapterInterface
30
     */
31
    protected $db;
32
    /**
33
     * @var string
34
     */
35
    protected $wikiText;
36
37
    public function __construct(array $ouvrageData, string $wikiText, LoggerInterface $logger, DbAdapterInterface $db)
38
    {
39
        $this->ouvrageData = $ouvrageData;
40
        $this->wikiText = $wikiText;
41
        $this->log = $logger;
42
        $this->db = $db;
43
    }
44
45
    public function validate(): bool
46
    {
47
        if (empty($this->ouvrageData['opti'])) {
48
            $this->log->notice("SKIP: pas d'optimisation proposée.");
49
50
            return false;
51
        }
52
        if (
53
            WikiTextUtil::isCommented($this->ouvrageData['raw'])
54
            || $this->isTextCreatingError($this->ouvrageData['raw'])
55
        ) {
56
            $this->log->notice("SKIP: template avec commentaire HTML ou modèle problématique.");
57
            $this->db->skipRow((int)$this->ouvrageData['id']);
58
59
            return false;
60
        }
61
        if (!$this->stringFound()) {
62
            return false;
63
        }
64
65
        return true;
66
    }
67
68
    protected function isTextCreatingError(string $string): bool
69
    {
70
        // mauvaise Modèle:Sp
71
        return (preg_match('#\{\{-?(sp|s|sap)-?\|#', $string) === 1);
72
    }
73
74
    protected function stringFound(): bool
75
    {
76
        $find = mb_strpos($this->wikiText, $this->ouvrageData['raw']);
77
        if ($find === false) {
78
            $this->log->notice("String non trouvée.");
79
            $this->db->skipRow((int)$this->ouvrageData['raw']['id']);
80
81
            return false;
82
        }
83
84
        return true;
85
    }
86
}