Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
19 / 19
100.00% covered (success)
100.00%
4 / 4
CRAP
100.00% covered (success)
100.00%
1 / 1
FileDescriptionPageValidator
100.00% covered (success)
100.00%
19 / 19
100.00% covered (success)
100.00%
4 / 4
11
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 validateTemplates
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
3
 validateCategories
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
3
 hasRequiredTemplate
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
4
1<?php
2
3namespace FileImporter\Services;
4
5use FileImporter\Data\WikitextConversions;
6use FileImporter\Exceptions\CommunityPolicyException;
7
8/**
9 * This class checks a file description page for required and forbidden categories and templates. It
10 * does not have any knowledge about the wikitext syntax.
11 *
12 * @license GPL-2.0-or-later
13 */
14class FileDescriptionPageValidator {
15
16    public function __construct(
17        private readonly WikitextConversions $wikitextConversions,
18    ) {
19    }
20
21    /**
22     * @param string[] $templates List of case-insensitive page names without namespace prefix
23     *
24     * @throws CommunityPolicyException
25     */
26    public function validateTemplates( array $templates ): void {
27        foreach ( $templates as $template ) {
28            if ( $this->wikitextConversions->isTemplateBad( $template ) ) {
29                throw new CommunityPolicyException( [
30                    'fileimporter-file-contains-blocked-category-template',
31                    $template
32                ] );
33            }
34        }
35    }
36
37    /**
38     * @param string[] $categories List of case-insensitive page names without namespace prefix
39     *
40     * @throws CommunityPolicyException
41     */
42    public function validateCategories( array $categories ): void {
43        foreach ( $categories as $category ) {
44            if ( $this->wikitextConversions->isCategoryBad( $category ) ) {
45                throw new CommunityPolicyException( [
46                    'fileimporter-file-contains-blocked-category-template',
47                    $category
48                ] );
49            }
50        }
51    }
52
53    /**
54     * @param string[] $templates List of case-insensitive page names without namespace prefix
55     *
56     * @throws CommunityPolicyException
57     */
58    public function hasRequiredTemplate( array $templates ): void {
59        if ( !$this->wikitextConversions->hasGoodTemplates() ) {
60            return;
61        }
62
63        foreach ( $templates as $template ) {
64            if ( $this->wikitextConversions->isTemplateGood( $template ) ) {
65                return;
66            }
67        }
68
69        throw new CommunityPolicyException( [ 'fileimporter-file-missing-required-template' ] );
70    }
71
72}