Passed
Push — master ( ed38f6...30aafa )
by Timo
11:15
created

buildClassificationsFromConfiguration()   B

Complexity

Conditions 8
Paths 10

Size

Total Lines 24
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 8.0368

Importance

Changes 0
Metric Value
eloc 15
dl 0
loc 24
rs 8.4444
c 0
b 0
f 0
ccs 11
cts 12
cp 0.9167
cc 8
nc 10
nop 1
crap 8.0368
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,
64
        array $configuration,
65
        /** @noinspection PhpUnusedParameterInspection */ $TyposcriptKey,
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['matchPatterns'])) || 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
                // @todo deprecate patterns configuration
108 1
            $patterns = empty($class['patterns']) ? [] : GeneralUtility::trimExplode(',', $class['patterns']);
109 1
            $matchPatterns = empty($class['matchPatterns']) ? [] : GeneralUtility::trimExplode(',', $class['matchPatterns']);
110 1
            $matchPatterns = $matchPatterns + $patterns;
111 1
            $unMatchPatters = empty($class['unmatchPatterns']) ? [] : GeneralUtility::trimExplode(',', $class['unmatchPatterns']);
112 1
113
            $className = $class['class'];
114
            $classifications[] = GeneralUtility::makeInstance(
115
                ClassificationItem::class,
116 1
                /** @scrutinizer ignore-type */ $matchPatterns,
117
                /** @scrutinizer ignore-type */ $unMatchPatters,
118
                /** @scrutinizer ignore-type */ $className
119
            );
120
        }
121
122
        return $classifications;
123
    }
124
}
125