Passed
Pull Request — master (#155)
by
unknown
08:39
created

DocumentValidator   A

Complexity

Total Complexity 32

Size/Duplication

Total Lines 136
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 49
c 1
b 0
f 0
dl 0
loc 136
rs 9.84
wmc 32

5 Methods

Rating   Name   Duplication   Size   Complexity  
B hasAllMandatoryFieldValues() 0 16 8
A hasFiles() 0 3 2
B validate() 0 24 7
B hasFieldWithValue() 0 21 8
B hasAllMandatoryGroupValues() 0 18 7
1
<?php
2
namespace EWW\Dpf\Helper;
3
4
/*
5
 * This file is part of the TYPO3 CMS project.
6
 *
7
 * It is free software; you can redistribute it and/or modify it under
8
 * the terms of the GNU General Public License, either version 2
9
 * of the License, or any later version.
10
 *
11
 * For the full copyright and license information, please read the
12
 * LICENSE.txt file that was distributed with this source code.
13
 *
14
 * The TYPO3 project - inspiring people to share!
15
 */
16
17
use EWW\Dpf\Domain\Model\Document;
18
use EWW\Dpf\Domain\Model\DocumentFormGroup;
19
use EWW\Dpf\Domain\Model\MetadataMandatoryInterface;
20
21
22
class DocumentValidator
23
{
24
    /**
25
     * objectManager
26
     *
27
     * @var \TYPO3\CMS\Extbase\Object\ObjectManager
28
     * @inject
29
     */
30
    protected $objectManager;
31
32
33
    /**
34
     * documentMapper
35
     *
36
     * @var \EWW\Dpf\Helper\DocumentMapper
37
     * @inject
38
     */
39
    protected $documentMapper;
40
41
42
    /**
43
     * @param DocumentFormGroup $group
44
     * @return bool
45
     */
46
    protected function hasFieldWithValue(DocumentFormGroup $group)
47
    {
48
        foreach ($group->getItems() as $fields) {
49
            foreach ($fields as $field) {
50
                switch ($group->getMandatory()) {
51
                    case MetadataMandatoryInterface::MANDATORY:
52
                    case MetadataMandatoryInterface::MANDATORY_FILE_ONLY:
53
                        if ($field->getValue()) {
54
                            return TRUE;
55
                        }
56
                        break;
57
                    default:
58
                        if ($field->getValue() && !$field->getHasDefaultValue()) {
59
                            return TRUE;
60
                        }
61
                        break;
62
                }
63
            }
64
        }
65
66
        return FALSE;
67
    }
68
69
70
    /**
71
     * @param DocumentFormGroup $group
72
     * @param bool $hasFiles
73
     * @return bool
74
     */
75
    protected function hasAllMandatoryFieldValues(DocumentFormGroup $group, $hasFiles)
76
    {
77
        foreach ($group->getItems() as $fields) {
78
            foreach ($fields as $field) {
79
                switch ($field->getMandatory()) {
80
                    case MetadataMandatoryInterface::MANDATORY:
81
                        if (!$field->getValue()) return FALSE;
82
                        break;
83
                    case MetadataMandatoryInterface::MANDATORY_FILE_ONLY:
84
                        if ($hasFiles && !$field->getValue()) return FALSE;
85
                        break;
86
                }
87
            }
88
        }
89
90
        return TRUE;
91
    }
92
93
94
    /**
95
     * @param DocumentFormGroup $group
96
     * @param bool $hasFiles
97
     * @return bool
98
     */
99
    protected function hasAllMandatoryGroupValues(DocumentFormGroup $group, $hasFiles)
100
    {
101
        switch ($group->getMandatory()) {
102
            case MetadataMandatoryInterface::MANDATORY:
103
                return $this->hasFieldWithValue($group) && $this->hasAllMandatoryFieldValues($group, $hasFiles);
104
            case MetadataMandatoryInterface::MANDATORY_FILE_ONLY:
105
                if ($hasFiles) {
106
                    return $this->hasFieldWithValue($group) && $this->hasAllMandatoryFieldValues($group, $hasFiles);
107
                }
108
                break;
109
            default:
110
                if ($this->hasFieldWithValue($group)) {
111
                    return $this->hasAllMandatoryFieldValues($group, $hasFiles);
112
                }
113
                break;
114
        }
115
116
        return TRUE;
117
    }
118
119
120
    /**
121
     * @param \EWW\Dpf\Domain\Model\DocumentForm $documentForm
122
     */
123
    protected function hasFiles(\EWW\Dpf\Domain\Model\DocumentForm $documentForm)
124
    {
125
        return $documentForm->getPrimaryFile() || $documentForm->getSecondaryFiles();
126
    }
127
128
129
    /**
130
     * @param Document $document
131
     * @param bool $checkPrimaryFile
132
     * @return bool
133
     */
134
    public function validate(Document $document, $checkPrimaryFile = FALSE) {
135
136
        /** @var  \EWW\Dpf\Domain\Model\DocumentForm $docForm */
137
        $docForm = $this->documentMapper->getDocumentForm($document);
138
139
        $hasFiles = $this->hasFiles($docForm);
140
141
        if ($checkPrimaryFile && !$hasFiles) return FALSE;
142
143
        $pages = $docForm->getItems();
144
        foreach ($pages as $page) {
145
            foreach ($page[0]->getItems() as $groups) {
146
                /** @var  \EWW\Dpf\Domain\Model\DocumentFormGroup $groupItem */
147
148
                foreach ($groups as $groupItem) {
149
                    if (!$this->hasAllMandatoryGroupValues($groupItem, $hasFiles)) {
150
                        //die();
151
                        return FALSE;
152
                    }
153
                }
154
            }
155
        }
156
157
        return TRUE;
158
    }
159
160
}