Completed
Push — master ( b6358f...8c41f2 )
by Rafael
07:13
created

buildClassificationsFromConfiguration()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.2
c 0
b 0
f 0
cc 4
eloc 9
nc 3
nop 1
1
<?php
2
namespace ApacheSolrForTypo3\Solr\ContentObject;
3
4
/***************************************************************
5
 *  Copyright notice
6
 *
7
 *  (c) 2017 Timo Hund <[email protected]>
8
 *  All rights reserved
9
 *
10
 *  This script is part of the TYPO3 project. The TYPO3 project is
11
 *  free software; you can redistribute it and/or modify
12
 *  it under the terms of the GNU General Public License as published by
13
 *  the Free Software Foundation; either version 2 of the License, or
14
 *  (at your option) any later version.
15
 *
16
 *  The GNU General Public License can be found at
17
 *  http://www.gnu.org/copyleft/gpl.html.
18
 *
19
 *  This script is distributed in the hope that it will be useful,
20
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
21
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22
 *  GNU General Public License for more details.
23
 *
24
 *  This copyright notice MUST APPEAR in all copies of the script!
25
 ***************************************************************/
26
27
use ApacheSolrForTypo3\Solr\Domain\Index\Classification\Classification as ClassificationItem;
28
use ApacheSolrForTypo3\Solr\Domain\Index\Classification\ClassificationService;
29
use TYPO3\CMS\Core\Utility\GeneralUtility;
30
31
/**
32
 * A content object (cObj) to classify content based on a configuration.
33
 *
34
 * Example usage:
35
 *
36
 * keywords = SOLR_CLASSIFICATION # supports stdWrap
37
 * keywords {
38
 *   field = __solr_content # a comma separated field. instead of field you can also use "value"
39
 *   classes {
40
 *     1 {
41
 *        patterns = smartphone, mobile, mobilephone # list of patterns that need to match to assign that class
42
 *        class = mobilephone # class that should be assigned when a pattern matches
43
 *     }
44
 *   }
45
 * }
46
 */
47
class Classification
48
{
49
    const CONTENT_OBJECT_NAME = 'SOLR_CLASSIFICATION';
50
51
    /**
52
     * Executes the SOLR_CLASSIFICATION content object.
53
     *
54
     * Returns mapped classes when the field matches on of the configured patterns ...
55
     *
56
     * @param string $name content object name 'SOLR_CONTENT'
57
     * @param array $configuration for the content object
58
     * @param string $TyposcriptKey not used
59
     * @param \TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer $contentObject parent cObj
60
     * @return string serialized array representation of the given list
61
     */
62
    public function cObjGetSingleExt(
63
        /** @noinspection PhpUnusedParameterInspection */ $name,
0 ignored issues
show
Unused Code introduced by
The parameter $name is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
64
        array $configuration,
65
        /** @noinspection PhpUnusedParameterInspection */ $TyposcriptKey,
0 ignored issues
show
Unused Code introduced by
The parameter $TyposcriptKey is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
66
        $contentObject
67
    ) {
68
69
        if (!is_array($configuration['classes.'])) {
70
            throw new \InvalidArgumentException('No class configuration configured for SOLR_CLASSIFICATION object. Given configuration: ' . serialize($configuration));
71
        }
72
73
        $configuredMappedClasses = $configuration['classes.'];
74
        unset($configuration['classes.']);
75
76
        $data = '';
77
        if (isset($configuration['value'])) {
78
            $data = $configuration['value'];
79
            unset($configuration['value']);
80
        }
81
82
        if (!empty($configuration)) {
83
            $data = $contentObject->stdWrap($data, $configuration);
0 ignored issues
show
Bug introduced by
It seems like $data can also be of type array; however, TYPO3\CMS\Frontend\Conte...jectRenderer::stdWrap() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
84
        }
85
        $classifications = $this->buildClassificationsFromConfiguration($configuredMappedClasses);
86
        /** @var $classificationService ClassificationService */
87
        $classificationService = GeneralUtility::makeInstance(ClassificationService::class);
88
        $classes = serialize($classificationService->getMatchingClassNames((string)$data, $classifications));
89
90
        return $classes;
91
    }
92
93
    /**
94
     * Builds an array of Classification objects from the passed classification configuration.
95
     *
96
     * @param array $configuredMappedClasses
97
     * @return ClassificationItem[]
98
     */
99
    protected function buildClassificationsFromConfiguration($configuredMappedClasses) : array
100
    {
101
        $classifications = [];
102
        foreach ($configuredMappedClasses as $class) {
103
            if (empty($class['patterns']) || empty($class['class'])) {
104
                throw new \InvalidArgumentException('A class configuration in SOLR_CLASSIFCATION needs to have a pattern and a class configured. Given configuration: ' . serialize($class));
105
            }
106
107
            $patterns = GeneralUtility::trimExplode(',', $class['patterns']);
108
            $className = $class['class'];
109
            $classifications[] = GeneralUtility::makeInstance(ClassificationItem::class, $patterns, $className);
110
        }
111
112
        return $classifications;
113
    }
114
}
115