Completed
Pull Request — master (#929)
by Timo
62:10 queued 27:19
created

IdBuilder   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
wmc 7
lcom 0
cbo 2
dl 0
loc 61
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A buildFromTypeAndUid() 0 8 1
A applyHook() 0 15 4
A getSystemHash() 0 10 2
1
<?php
2
3
namespace ApacheSolrForTypo3\Solr\Domain\Variants;
4
5
/***************************************************************
6
 *  Copyright notice
7
 *
8
 *  (c) 2017- Timo Hund <[email protected]>
9
 *  All rights reserved
10
 *
11
 *  This script is part of the TYPO3 project. The TYPO3 project is
12
 *  free software; you can redistribute it and/or modify
13
 *  it under the terms of the GNU General Public License as published by
14
 *  the Free Software Foundation; either version 2 of the License, or
15
 *  (at your option) any later version.
16
 *
17
 *  The GNU General Public License can be found at
18
 *  http://www.gnu.org/copyleft/gpl.html.
19
 *
20
 *  This script is distributed in the hope that it will be useful,
21
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
22
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23
 *  GNU General Public License for more details.
24
 *
25
 *  This copyright notice MUST APPEAR in all copies of the script!
26
 ***************************************************************/
27
28
use TYPO3\CMS\Core\Utility\GeneralUtility;
29
30
/**
31
 * The variantId can be used to group documents by a variantId. This variantId is by default unique per system,
32
 * and has the following syntax:
33
 *
34
 * <SystemHash>/type/uid
35
 *
36
 * A file from one system will get the same variantId, which could be useful for de-duplication.
37
 * @author Timo Hund <[email protected]>
38
 */
39
class IdBuilder
40
{
41
42
    /**
43
     * This method is used to build a variantId.
44
     *
45
     * By default the variantId is used
46
     * @param string $type
47
     * @param integer $uid
48
     * @return string
49
     */
50
    public function buildFromTypeAndUid($type, $uid)
51
    {
52
        $systemHash = $this->getSystemHash();
53
        $variantId = $systemHash . '/' . $type . '/' . $uid;
54
55
        $variantId = $this->applyHook($variantId, $systemHash, $type, $uid);
56
        return $variantId;
57
    }
58
59
    /**
60
     * Applies configured postProcessing hooks to build a custom variantId.
61
     *
62
     * @param string $variantId
63
     * @param string $systemHash
64
     * @param string $type
65
     * @param integer $uid
66
     * @return string
67
     */
68
    protected function applyHook($variantId, $systemHash, $type, $uid)
69
    {
70
        if (!is_array($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['solr']['modifyVariantId'])) {
71
            return $variantId;
72
        }
73
74
        foreach ($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['solr']['modifyVariantId'] as $classReference) {
75
            $variantIdModifier = GeneralUtility::getUserObj($classReference);
76
            if ($variantIdModifier instanceof IdModifier) {
77
                $variantId = $variantIdModifier->modifyVariantId($variantId, $systemHash, $type, $uid);
78
            }
79
        }
80
81
        return $variantId;
82
    }
83
84
    /**
85
     * Returns a system unique hash.
86
     *
87
     * @return string
88
     */
89
    protected function getSystemHash()
90
    {
91
        if (!isset($GLOBALS['TYPO3_CONF_VARS']['SYS']['sitename'])) {
92
            throw new \InvalidArgumentException("No sitename set in TYPO3_CONF_VARS|SYS|sitename");
93
        }
94
95
        $siteName = $GLOBALS['TYPO3_CONF_VARS']['SYS']['sitename'];
96
        $systemKey = 'tx_solr' . $siteName;
97
        return GeneralUtility::hmac($systemKey);
98
    }
99
}
100