Passed
Pull Request — master (#195)
by
unknown
12:46 queued 05:37
created

RisWosFileImporter::getDefaultXsltTransformation()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 7
rs 10
1
<?php
2
3
namespace EWW\Dpf\Services\ImportExternalMetadata;
4
5
use EWW\Dpf\Domain\Model\RisWosMetadata;
6
use EWW\Dpf\Domain\Model\ExternalMetadata;
7
8
class RisWosFileImporter extends AbstractImporter implements FileImporter
9
{
10
11
    /**
12
     * @var array
13
     */
14
    protected $mandatoryErrors = [];
15
16
    /**
17
     * Returns the list of all publication types
18
     *
19
     * @return array
20
     */
21
    public static function types()
22
    {
23
        return [
24
            'J' => 'Journal',
25
            'B' => 'Book',
26
            'S' => 'Series',
27
            'P' => 'Patent'
28
        ];
29
    }
30
31
    /**
32
     * @param string $filePath
33
     * @param string $mandatoryFieldSettings
34
     * @param bool $contentOnly Determines if $file is a path or content as a string
35
     * @return array
36
     */
37
    public function loadFile($filePath, $mandatoryFieldSettings, $contentOnly = false)
38
    {
39
        $results = [];
40
        $mandatoryErrors = [];
41
        $mandatoryFieldErrors = [];
0 ignored issues
show
Unused Code introduced by
The assignment to $mandatoryFieldErrors is dead and can be removed.
Loading history...
42
43
        $mandatoryFields = array_map(
44
            'trim',
45
            explode(',', $mandatoryFieldSettings)
46
        );
47
48
        foreach ($mandatoryFields as $key => $value) {
49
            $orFields = array_map(
50
                'trim',
51
                explode('|', $value)
52
            );
53
            $mandatoryFields[$key] = $orFields;
54
        }
55
56
        $risWosReader = new RisReader();
57
58
        if ($contentOnly) {
59
            $risWosEntries = $risWosReader->parseFile($filePath, $contentOnly);
60
        } else {
61
            $risWosEntries = $risWosReader->parseFile($filePath);
62
        }
63
64
        foreach ($risWosEntries as $index => $risWosItem) {
65
66
            $mandatoryFieldErrors = [];
67
            foreach ($mandatoryFields as $combinedMandatoryField) {
68
69
                $mandatoryOk = false;
70
                foreach ($combinedMandatoryField as $key => $value) {
71
                    $mandatoryOk = $mandatoryOk || (
72
                        array_key_exists($value, $risWosItem)
73
                        && $risWosItem[$value]
74
                    );
75
                }
76
77
                if (!$mandatoryOk) {
78
                    $mandatoryFieldErrors[implode('|', $combinedMandatoryField)] = implode(
79
                        '|',
80
                        $combinedMandatoryField
81
                    );
82
                    $mandatoryErrors[$index] = [
83
                        'index' => $index + 1,
84
                        'title' => $risWosItem['TI'],
85
                        'fields' => $mandatoryFieldErrors
86
                    ];
87
                }
88
            }
89
90
            if (!$mandatoryErrors[$index]) {
91
                /** @var RisWosMetadata $risWosMetadata */
92
                $risWosMetadata = $this->objectManager->get(RisWosMetadata::class);
93
                $risWosMetadata->setSource(get_class($this));
94
                $risWosMetadata->setFeUser($this->security->getUser()->getUid());
95
                $risWosMetadata->setData($risWosReader->risRecordToXML($risWosItem));
96
                $results[] = $risWosMetadata;
97
            }
98
99
        }
100
        $this->mandatoryErrors = $mandatoryErrors;
101
102
        return $results;
103
    }
104
105
    /**
106
     * @return array
107
     */
108
    public function getMandatoryErrors()
109
    {
110
        return $this->mandatoryErrors;
111
    }
112
113
    /**
114
     * @return bool
115
     */
116
    public function hasMandatoryErrors()
117
    {
118
        return !empty($this->mandatoryErrors);
119
    }
120
121
    /**
122
     * @return \EWW\Dpf\Domain\Model\TransformationFile|void
123
     */
124
    protected function getDefaultXsltTransformation()
125
    {
126
        /** @var \EWW\Dpf\Domain\Model\Client $client */
127
        $client = $this->clientRepository->findAll()->current();
128
129
        /** @var \EWW\Dpf\Domain\Model\TransformationFile $xsltTransformationFile */
130
        return $client->getRisWosTransformation()->current();
131
    }
132
133
    /**
134
     * @return string|void
135
     */
136
    protected function getDefaultXsltFilePath()
137
    {
138
        return \TYPO3\CMS\Core\Utility\GeneralUtility::getFileAbsFileName(
139
            'EXT:dpf/Resources/Private/Xslt/riswos-default.xsl'
140
        );
141
    }
142
143
    /**
144
     * @return string|void
145
     */
146
    protected function getImporterName()
147
    {
148
        return 'riswos';
149
    }
150
151
}