Passed
Pull Request — master (#123)
by
unknown
12:51
created

DocumentValidator   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 110
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 32
c 1
b 0
f 0
dl 0
loc 110
rs 10
wmc 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A hasCorrectPhysicalStructure() 0 15 3
A __construct() 0 6 1
A hasAllMandatoryMetadataFields() 0 9 3
A hasCorrectLogicalStructure() 0 15 3
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * (c) Kitodo. Key to digital objects e.V. <[email protected]>
7
 *
8
 * This file is part of the Kitodo and TYPO3 projects.
9
 *
10
 * @license GNU General Public License version 3 or later.
11
 * For the full copyright and license information, please read the
12
 * LICENSE.txt file that was distributed with this source code.
13
 */
14
15
namespace Kitodo\Dlf\Validation;
16
17
use TYPO3\CMS\Core\Log\Logger;
18
use TYPO3\CMS\Core\Log\LogManager;
19
use TYPO3\CMS\Core\Utility\GeneralUtility;
20
21
/**
22
 * Class for document validation. Currently used for validating metadata
23
 * fields but in the future should be extended also for other fields.
24
 *
25
 * @package TYPO3
26
 * @subpackage dlf
27
 *
28
 * @access public
29
 */
30
class DocumentValidator
31
{
32
     /**
33
     * @access protected
34
     * @var Logger This holds the logger
35
     */
36
    protected Logger $logger;
37
38
    /**
39
     * @access private
40
     * @var array
41
     */
42
    private array $metadata;
43
44
    /**
45
     * @access private
46
     * @var array
47
     */
48
    private array $requiredMetadataFields;
49
50
    /**
51
     * @access private
52
     * @var ?\SimpleXMLElement
53
     */
54
    private ?\SimpleXMLElement $xml;
55
56
    /**
57
     * Constructs DocumentValidator instance.
58
     *
59
     * @access public
60
     *
61
     * @param array $metadata
62
     * @param array $requiredMetadataFields
63
     *
64
     * @return void
65
     */
66
    public function __construct(array $metadata = [], array $requiredMetadataFields = [], ?\SimpleXMLElement $xml = null)
67
    {
68
        $this->logger = GeneralUtility::makeInstance(LogManager::class)->getLogger(static::class);
69
        $this->metadata = $metadata;
70
        $this->requiredMetadataFields = $requiredMetadataFields;
71
        $this->xml = $xml;
72
    }
73
74
    /**
75
     * Check if metadata array contains all mandatory fields before save.
76
     *
77
     * @access public
78
     *
79
     * @return bool
80
     */
81
    public function hasAllMandatoryMetadataFields(): bool
82
    {
83
        foreach ($this->requiredMetadataFields as $requiredMetadataField) {
84
            if (empty($this->metadata[$requiredMetadataField][0])) {
85
                $this->logger->error('Missing required metadata field "' . $requiredMetadataField . '".');
86
                return false;
87
            }
88
        }
89
        return true;
90
    }
91
92
    /**
93
     * Check if xml contains at least one logical structure with given type.
94
     *
95
     * @access public
96
     * 
97
     * @param string $type e.g. documentary, newspaper or object
98
     *
99
     * @return bool
100
     */
101
    public function hasCorrectLogicalStructure(string $type): bool
102
    {
103
        $expectedNodes = $this->xml->xpath('./mets:structMap[@TYPE="LOGICAL"]/mets:div[@TYPE="' . $type . '"]');
0 ignored issues
show
Bug introduced by
The method xpath() does not exist on null. ( Ignorable by Annotation )

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

103
        /** @scrutinizer ignore-call */ 
104
        $expectedNodes = $this->xml->xpath('./mets:structMap[@TYPE="LOGICAL"]/mets:div[@TYPE="' . $type . '"]');

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
104
        if ($expectedNodes) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $expectedNodes of type array<mixed,SimpleXMLElement> is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
105
            return true;
106
        }
107
108
        $existingNodes = $this->xml->xpath('./mets:structMap[@TYPE="LOGICAL"]/mets:div');
109
        if ($existingNodes) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $existingNodes of type array<mixed,SimpleXMLElement> is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
110
            $this->logger->error('Document contains logical structure but @TYPE="' . $type . '" is missing.');
111
            return false;
112
        }
113
114
        $this->logger->error('Document does not contain logical structure.');
115
        return false;
116
    }
117
118
    /**
119
     * Check if xml contains at least one physical structure with type 'physSequence'.
120
     *
121
     * @access public
122
     *
123
     * @return bool
124
     */
125
    public function hasCorrectPhysicalStructure(): bool
126
    {
127
        $physSequenceNodes = $this->xml->xpath('./mets:structMap[@TYPE="PHYSICAL"]/mets:div[@TYPE="physSequence"]');
128
        if ($physSequenceNodes) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $physSequenceNodes of type array<mixed,SimpleXMLElement> is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
129
            return true;
130
        }
131
132
        $physicalStructureNodes = $this->xml->xpath('./mets:structMap[@TYPE="PHYSICAL"]/mets:div');
133
        if ($physicalStructureNodes) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $physicalStructureNodes of type array<mixed,SimpleXMLElement> is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
134
            $this->logger->error('Document contains physical structure but @TYPE="physSequence" is missing.');
135
            return false;
136
        }
137
138
        $this->logger->error('Document does not contain physical structure.');
139
        return false;
140
    }
141
}
142