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

ArticleValidForEditionValidator::botIsLastEditor()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 5
c 1
b 0
f 1
dl 0
loc 11
rs 10
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\Application\OuvrageEdit\Validators;
11
12
use App\Application\InfrastructurePorts\DbAdapterInterface;
13
use App\Application\WikiPageAction;
14
use Psr\Log\LoggerInterface;
15
16
class ArticleValidForEditionValidator implements ValidatorInterface
17
{
18
    /**
19
     * @var string
20
     */
21
    protected $title;
22
    /**
23
     * @var LoggerInterface
24
     */
25
    protected $log;
26
    /**
27
     * @var DbAdapterInterface
28
     */
29
    protected $db;
30
    /**
31
     * @var WikiPageAction
32
     */
33
    protected $wikiPageAction;
34
35
    public function __construct(string $title, LoggerInterface $logger, DbAdapterInterface $db, WikiPageAction $wikiPageAction)
36
    {
37
        $this->title = $title;
38
        $this->log = $logger;
39
        $this->db = $db;
40
        $this->wikiPageAction = $wikiPageAction;
41
    }
42
43
    public function validate(): bool
44
    {
45
        if (
46
            !$this->hasLastRevision()
47
            || $this->botIsLastEditor()
48
            || !$this->isInMainNamespace()
49
            || !$this->hasWikitext()
50
        ) {
51
            return false;
52
        }
53
54
        return true;
55
    }
56
57
    private function hasLastRevision(): bool
58
    {
59
        // Page supprimée ?
60
        if ($this->wikiPageAction->getLastRevision() === null) {
61
            $this->log->warning("SKIP : page supprimée !\n");
62
            $this->db->deleteArticle($this->title);
1 ignored issue
show
Bug introduced by
The method deleteArticle() does not exist on App\Application\Infrastr...orts\DbAdapterInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to App\Application\Infrastr...orts\DbAdapterInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

62
            $this->db->/** @scrutinizer ignore-call */ 
63
                       deleteArticle($this->title);
Loading history...
63
64
            return false;
65
        }
66
67
        return true;
68
    }
69
70
    private function botIsLastEditor(): bool
71
    {
72
        // HACK
73
        if ($this->wikiPageAction->getLastEditor() === getenv('BOT_NAME')) {
74
            $this->log->notice("SKIP : édité recemment par le bot.\n");
75
            $this->db->skipArticle($this->title);
1 ignored issue
show
Bug introduced by
The method skipArticle() does not exist on App\Application\Infrastr...orts\DbAdapterInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to App\Application\Infrastr...orts\DbAdapterInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

75
            $this->db->/** @scrutinizer ignore-call */ 
76
                       skipArticle($this->title);
Loading history...
76
77
            return true;
78
        }
79
80
        return false;
81
    }
82
83
    private function isInMainNamespace(): bool
84
    {
85
        // todo include a sandbox page ?
86
        if ($this->wikiPageAction->getNs() !== 0) {
87
            $this->log->notice("SKIP : page n'est pas dans Main (ns 0)\n");
88
            $this->db->skipArticle($this->title);
89
90
            return false;
91
        }
92
        return true;
93
    }
94
95
    private function hasWikitext(): bool
96
    {
97
        $wikiText = $this->wikiPageAction->getText();
98
99
        if (empty($wikiText)) {
100
            $this->log->warning("SKIP : this->wikitext vide\n");
101
            $this->db->skipArticle($this->title);
102
103
            return false;
104
        }
105
106
        return true;
107
    }
108
}