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

PageValidatorComposite::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 6
dl 0
loc 13
rs 10
c 1
b 0
f 1
cc 1
nc 1
nop 4
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\Application\WikiBotConfig;
16
use App\Application\WikiPageAction;
17
use RuntimeException;
18
19
/**
20
 * needs WikiPageAction injection.
21
 */
22
class PageValidatorComposite implements ValidatorInterface
23
{
24
    /** @var ValidatorInterface[] */
25
    protected $validators;
26
27
    public function __construct(
28
        WikiBotConfig $botConfig,
29
        array $pageCitationCollection,
30
        DbAdapterInterface $db,
31
        WikiPageAction $wikiPageAction
32
    )
33
    {
34
        $title = $pageCitationCollection[0]['page'];
35
        $this->validators = [
36
            new CitationsAllCompletedValidator($pageCitationCollection, $db),
37
            new ArticleValidForEditionValidator($title, $botConfig->getLogger(), $db, $wikiPageAction),
38
            new ArticleRestrictedValidator($title, $botConfig->getLogger(), $db, $wikiPageAction),
39
            new HumanDelayValidator($title, $botConfig),
40
        ];
41
    }
42
43
    public function validate(): bool
44
    {
45
        foreach ($this->validators as $validator) {
46
            if (!$validator instanceof ValidatorInterface) {
47
                throw new RuntimeException(get_class($validator) . ' must implement ValidatorInterface.');
48
            }
49
            if ($validator->validate() === false) {
50
                return false;
51
            }
52
        }
53
54
        return true;
55
    }
56
}