Passed
Push — master ( 683c38...4d0316 )
by Timo
22:01
created

Classification   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Test Coverage

Coverage 85.19%

Importance

Changes 0
Metric Value
wmc 8
dl 0
loc 70
ccs 23
cts 27
cp 0.8519
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B cObjGetSingleExt() 0 29 4
A buildClassificationsFromConfiguration() 0 18 4
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 3 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 1
    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. ( Ignorable by Annotation )

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

63
        /** @noinspection PhpUnusedParameterInspection */ /** @scrutinizer ignore-unused */ $name,

This check looks for 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. ( Ignorable by Annotation )

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

65
        /** @noinspection PhpUnusedParameterInspection */ /** @scrutinizer ignore-unused */ $TyposcriptKey,

This check looks for 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 1
        if (!is_array($configuration['classes.'])) {
70
            throw new \InvalidArgumentException('No class configuration configured for SOLR_CLASSIFICATION object. Given configuration: ' . serialize($configuration));
71
        }
72
73 1
        $configuredMappedClasses = $configuration['classes.'];
74 1
        unset($configuration['classes.']);
75
76 1
        $data = '';
77 1
        if (isset($configuration['value'])) {
78
            $data = $configuration['value'];
79
            unset($configuration['value']);
80
        }
81
82 1
        if (!empty($configuration)) {
83 1
            $data = $contentObject->stdWrap($data, $configuration);
84
        }
85 1
        $classifications = $this->buildClassificationsFromConfiguration($configuredMappedClasses);
86
        /** @var $classificationService ClassificationService */
87 1
        $classificationService = GeneralUtility::makeInstance(ClassificationService::class);
88 1
        $classes = serialize($classificationService->getMatchingClassNames((string)$data, $classifications));
89
90 1
        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 1
    protected function buildClassificationsFromConfiguration($configuredMappedClasses) : array
100
    {
101 1
        $classifications = [];
102 1
        foreach ($configuredMappedClasses as $class) {
103 1
            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 1
            $patterns = GeneralUtility::trimExplode(',', $class['patterns']);
108 1
            $className = $class['class'];
109 1
            $classifications[] = GeneralUtility::makeInstance(
110 1
                ClassificationItem::class,
111 1
                /** @scrutinizer ignore-type */ $patterns,
112 1
                /** @scrutinizer ignore-type */ $className
113
            );
114
        }
115
116 1
        return $classifications;
117
    }
118
}
119