Passed
Branch dev3 (493baa)
by Dispositif
02:30
created

CitationValidator::isTextCreatingError()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 1
c 1
b 0
f 1
dl 0
loc 4
rs 10
cc 1
nc 1
nop 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 (
48
            WikiTextUtil::isCommented($this->ouvrageData['raw'])
49
            || $this->isTextCreatingError($this->ouvrageData['raw'])
50
        ) {
51
            $this->log->notice("SKIP: template avec commentaire HTML ou modèle problématique.");
52
            $this->db->skipRow((int)$this->ouvrageData['id']);
53
54
            return false;
55
        }
56
        if (!$this->stringFound()) {
57
            return false;
58
        }
59
60
        return true;
61
    }
62
63
    protected function isTextCreatingError(string $string): bool
64
    {
65
        // mauvaise Modèle:Sp
66
        return (preg_match('#\{\{-?(sp|s|sap)-?\|#', $string) === 1);
67
    }
68
69
    protected function stringFound(): bool
70
    {
71
        $find = mb_strpos($this->wikiText, $this->ouvrageData['raw']);
72
        if ($find === false) {
73
            $this->log->notice("String non trouvée.");
74
            $this->db->skipRow((int)$this->ouvrageData['raw']['id']);
75
76
            return false;
77
        }
78
79
        return true;
80
    }
81
}