Passed
Push — master ( 49acfe...3324e2 )
by Ralf
09:54 queued 32s
created

Coins::generateCoins()   D

Complexity

Conditions 66
Paths 58

Size

Total Lines 146
Code Lines 92

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 92
c 1
b 0
f 0
dl 0
loc 146
rs 4.1666
cc 66
nc 58
nop 1

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
namespace EWW\Dpf\Plugins\Coins;
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 TYPO3\CMS\Core\Utility\GeneralUtility;
18
19
/**
20
 * Plugin 'DPF: Coins' for the 'dlf / dpf' extension.
21
 *
22
 * @author    Erik Sommer <[email protected]>
23
 * @package    TYPO3
24
 * @subpackage    tx_dpf
25
 * @access    public
26
 */
27
class Coins extends \Kitodo\Dlf\Common\AbstractPlugin
0 ignored issues
show
Bug introduced by
The type Kitodo\Dlf\Common\AbstractPlugin was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
28
{
29
    public $scriptRelPath = 'Classes/Plugins/Coins.php';
30
31
    /**
32
     * The main method of the PlugIn
33
     *
34
     * @access    public
35
     *
36
     * @param    string        $content: The PlugIn content
37
     * @param    array        $conf: The PlugIn configuration
38
     *
39
     * @return    string        The content that is displayed on the website
40
     */
41
    public function main($content, $conf)
42
    {
43
        $this->init($conf);
44
45
        // get the tx_dpf.settings too
46
        // Flexform wins over TS
47
        $dpfTSconfig = $GLOBALS['TSFE']->tmpl->setup['plugin.']['tx_dpf.'];
48
        if (is_array($dpfTSconfig['settings.'])) {
49
            \TYPO3\CMS\Core\Utility\ArrayUtility::mergeRecursiveWithOverrule($dpfTSconfig['settings.'], $this->conf, true, false);
50
            $this->conf = $dpfTSconfig['settings.'];
0 ignored issues
show
Bug Best Practice introduced by
The property conf does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
51
        }
52
53
        $this->setCache(true);
54
55
        $this->loadDocument();
56
        if ($this->doc === null) {
57
            return;
58
        } else {
59
            // Set default values if not set.
60
            if (!isset($this->conf['rootline'])) {
61
                $this->conf['rootline'] = 0;
62
            }
63
        }
64
65
        $metadata = array();
0 ignored issues
show
Unused Code introduced by
The assignment to $metadata is dead and can be removed.
Loading history...
66
        $metadata = $this->doc->getTitleData($this->conf['pages']);
67
68
        $metadata['_id'] = $this->doc->toplevelId;
69
        if (empty($metadata)) {
70
            if (TYPO3_DLOG) {
71
                GeneralUtility::devLog(
0 ignored issues
show
Deprecated Code introduced by
The function TYPO3\CMS\Core\Utility\GeneralUtility::devLog() has been deprecated: since TYPO3 v9, will be removed in TYPO3 v10.0. ( Ignorable by Annotation )

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

71
                /** @scrutinizer ignore-deprecated */ GeneralUtility::devLog(

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
72
                    '[tx_dpf_metatags->main(' . $content . ', [data])] No metadata found for document with UID "' . $this->doc->uid . '"',
73
                    'tx_dpf',
74
                    SYSLOG_SEVERITY_WARNING,
0 ignored issues
show
Bug introduced by
The constant EWW\Dpf\Plugins\Coins\SYSLOG_SEVERITY_WARNING was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
75
                    $conf
76
                );
77
            }
78
            return;
79
        }
80
81
        return $this->generateCoins($metadata);
82
    }
83
84
    /**
85
     * Prepares the coins <span> for output
86
     *
87
     * @access    protected
88
     * @param    array        $metadata: The metadata array
89
     *
90
     * @return    string        The coins <span> ready for output
91
     */
92
     protected function generateCoins(array $metadata)
93
    {
94
95
        // The output follows the "Brief guide to Implementing OpenURL 1.0 Context Object for Journal Articles"
96
        // -> https://archive.is/a0Hgs
97
98
        // Formal specification of COinS
99
        $coins = 'url_ver=Z39.88-2004';
100
        $coins .= '&ctx_ver=Z39.88-2004';
101
102
        // TODO: Get the document type to differentiate info:ofi/fmt:kev:mtx:[book/journal] and rft.genre=[…]
103
        $coins .= '&rft_val_fmt='. urlencode('info:ofi/fmt:kev:mtx:journal');
104
        $coins .= '&rft.genre=unknown';
105
106
        foreach ($metadata as $index_name => $values) {
107
            if (preg_match("/^author[[:digit:]]+/", $index_name)) {
108
                if (is_array($values)) {
109
                    foreach ($values as $id => $value) {
110
                        if ($value) {
111
                            $coins .= '&rft.au=' . urlencode($value);
112
                        }
113
                    }
114
                }
115
                continue;
116
            }
117
118
            if (preg_match("/^publisher[[:digit:]]+/", $index_name)) {
119
                if (is_array($values)) {
120
                    foreach ($values as $id => $value) {
121
                        if ($value) {
122
                            $coins .= '&rft.pub=' . urlencode($value);
123
                        }
124
                    }
125
                }
126
                continue;
127
            }
128
129
            switch ($index_name) {
130
                case 'record_id':
131
                    if (is_array($values) && $values[0]) {
132
                        $coins .= '&rfr_id=info:sid/qucosa.de:' . urlencode($values[0]);
133
                    }
134
                    break;
135
136
                case 'urn':
137
                case 'original_urn':
138
                case 'series_urn':
139
                case 'multivolume_urn':
140
                case 'doi':
141
                case 'original_doi':
142
                    if (is_array($values) && $values[0]) {
143
                        $coins .= '&rft_id=' . urlencode($values[0]);
144
                    }
145
                    break;
146
147
                case 'isbn':
148
                case 'original_isbn':
149
                    if (is_array($values) && $values[0]) {
150
                        $coins .= '&rft.isbn=' . urlencode($values[0]);
151
                    }
152
                    break;
153
154
                case 'issn':
155
                case 'original_issn':
156
                    if (is_array($values) && $values[0]) {
157
                        $coins .= '&rft.issn=' . urlencode($values[0]);
158
                    }
159
                    break;
160
161
                case 'title':
162
                    if (is_array($values) && $values[0]) {
163
                        $coins .= '&rft.atitle=' . urlencode($values[0]);
164
                    }
165
                    break;
166
167
                case 'original_subtitle':
168
                    if (is_array($values) && $values[0]) {
169
                        $coins .= '&rft.stitle=' . urlencode($values[0]);
170
                    }
171
                    break;
172
173
                case 'original_title':
174
                    if (is_array($values) && $values[0]) {
175
                        $coins .= '&rft.jtitle=' . urlencode($values[0]);
176
                    }
177
                    break;
178
179
                case 'original_pages':
180
                    if (is_array($values) && $values[0]) {
181
                        $coins .= '&rft.spage=' . urlencode($values[0]);
182
                    }
183
                    break;
184
185
                case 'original_pages2':
186
                    if (is_array($values) && $values[0]) {
187
                        $coins .= '&rft.epage=' . urlencode($values[0]);
188
                    }
189
                    break;
190
191
                case 'issue':
192
                case 'original_issue':
193
                    if (is_array($values) && $values[0]) {
194
                        $coins .= '&rft.issue=' . urlencode($values[0]);
195
                    }
196
                    break;
197
198
                case 'volume':
199
                case 'original_volume':
200
                    if (is_array($values) && $values[0]) {
201
                        $coins .= '&rft.volume=' . urlencode($values[0]);
202
                    }
203
                    break;
204
205
                case 'original_corporation_publisher':
206
                    if (is_array($values) && $values[0]) {
207
                        $coins .= '&rft.pub=' . urlencode($values[0]);
208
                    }
209
                    break;
210
211
                case 'place':
212
                case 'original_place':
213
                    if (is_array($values) && $values[0]) {
214
                        if ($values[0]) {
215
                            $coins .= '&rft.place=' . urlencode($values[0]);
216
                        }
217
                    }
218
                    break;
219
220
                case 'dateissued':
221
                    if (is_array($values) && $values[0]) {
222
                        $coins .= '&rft.date=' . urlencode($this->safelyFormatDate("Y/m/d", $values[0]));
223
                    }
224
                    break;
225
226
                case 'language':
227
                    if (is_array($values) && $values[0]) {
228
                        $coins .= '&rft.language=' . urlencode($values[0]);
229
                    }
230
                    break;
231
232
                default:
233
                    break;
234
            }
235
        }
236
237
        return '<span class="Z3988" title="' . $coins . '"></span>';
238
    }
239
240
    /**
241
     * Format given date with given format, assuming the input date format is
242
     * parseable by strtotime(). If the input string has a length of 4 (like
243
     * "1989") the string is returned as is, without formatting.
244
     *
245
     * @param String $format Target string format
246
     * @param String $date   Date string to format
247
     *
248
     * @return Formatted date
0 ignored issues
show
Bug introduced by
The type EWW\Dpf\Plugins\Coins\Formatted was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
249
     */
250
     protected function safelyFormatDate($format, $date)
251
    {
252
        return (strlen($date) == 4) ? $date : date($format, strtotime($date));
0 ignored issues
show
Bug Best Practice introduced by
The expression return strlen($date) == ...rmat, strtotime($date)) returns the type string which is incompatible with the documented return type EWW\Dpf\Plugins\Coins\Formatted.
Loading history...
253
    }
254
}
255