Passed
Pull Request — master (#155)
by
unknown
09:50
created

DocumentValidator::hasFieldWithValue()   B

Complexity

Conditions 8
Paths 8

Size

Total Lines 21
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 8
eloc 13
c 1
b 0
f 0
nc 8
nop 1
dl 0
loc 21
rs 8.4444
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
20
21
class DocumentValidator
22
{
23
    /**
24
     * objectManager
25
     *
26
     * @var \TYPO3\CMS\Extbase\Object\ObjectManager
27
     * @inject
28
     */
29
    protected $objectManager;
30
31
32
    /**
33
     * documentMapper
34
     *
35
     * @var \EWW\Dpf\Helper\DocumentMapper
36
     * @inject
37
     */
38
    protected $documentMapper;
39
40
41
    /**
42
     * @param DocumentFormGroup $group
43
     * @return bool
44
     */
45
    protected function hasFieldWithValue(DocumentFormGroup $group)
46
    {
47
        foreach ($group->getItems() as $fields) {
48
            foreach ($fields as $field) {
49
                switch ($group->getMandatory()) {
50
                    case '1':
51
                    case 'FILE_ONLY':
52
                        if ($field->getValue()) {
53
                            return TRUE;
54
                        }
55
                        break;
56
                    default:
57
                        if ($field->getValue() && !$field->getHasDefaultValue()) {
58
                            return TRUE;
59
                        }
60
                        break;
61
                }
62
            }
63
        }
64
65
        return FALSE;
66
    }
67
68
69
    /**
70
     * @param DocumentFormGroup $group
71
     * @param bool $hasFiles
72
     * @return bool
73
     */
74
    protected function hasAllMandatoryFieldValues(DocumentFormGroup $group, $hasFiles)
75
    {
76
        foreach ($group->getItems() as $fields) {
77
            foreach ($fields as $field) {
78
                switch ($field->getMandatory()) {
79
                    case '1':
80
                        if (!$field->getValue()) return FALSE;
81
                        break;
82
                    case 'FILE_ONLY':
83
                        if ($hasFiles && !$field->getValue()) return FALSE;
84
                        break;
85
                }
86
            }
87
        }
88
89
        return TRUE;
90
    }
91
92
93
    /**
94
     * @param DocumentFormGroup $group
95
     * @param bool $hasFiles
96
     * @return bool
97
     */
98
    protected function hasAllMandatoryGroupValues(DocumentFormGroup $group, $hasFiles)
99
    {
100
        switch ($group->getMandatory()) {
101
            case '1':
102
                return $this->hasFieldWithValue($group) && $this->hasAllMandatoryFieldValues($group, $hasFiles);
103
            case 'FILE_ONLY':
104
                if ($hasFiles) {
105
                    return $this->hasFieldWithValue($group) && $this->hasAllMandatoryFieldValues($group, $hasFiles);
106
                }
107
                break;
108
            default:
109
                if ($this->hasFieldWithValue($group)) {
110
                    return $this->hasAllMandatoryFieldValues($group, $hasFiles);
111
                }
112
                break;
113
        }
114
115
        return TRUE;
116
    }
117
118
119
    /**
120
     * @param \EWW\Dpf\Domain\Model\DocumentForm $documentForm
121
     */
122
    protected function hasFiles(\EWW\Dpf\Domain\Model\DocumentForm $documentForm)
123
    {
124
        return $documentForm->getPrimaryFile() || $documentForm->getSecondaryFiles();
125
    }
126
127
128
    /**
129
     * @param Document $document
130
     * @param bool $checkPrimaryFile
131
     * @return bool
132
     */
133
    public function validate(Document $document, $checkPrimaryFile = FALSE) {
134
135
        /** @var  \EWW\Dpf\Domain\Model\DocumentForm $docForm */
136
        $docForm = $this->documentMapper->getDocumentForm($document);
137
138
        $hasFiles = $this->hasFiles($docForm);
139
140
        if ($checkPrimaryFile && !$hasFiles) return FALSE;
141
142
        $pages = $docForm->getItems();
143
        foreach ($pages as $page) {
144
            foreach ($page[0]->getItems() as $groups) {
145
                /** @var  \EWW\Dpf\Domain\Model\DocumentFormGroup $groupItem */
146
147
                foreach ($groups as $groupItem) {
148
                    if (!$this->hasAllMandatoryGroupValues($groupItem, $hasFiles)) {
149
                        //die();
150
                        return FALSE;
151
                    }
152
                }
153
            }
154
        }
155
156
        return TRUE;
157
    }
158
159
}