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

getLocalRecordUidFromOverlay()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 4.0466

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 12
rs 10
c 0
b 0
f 0
ccs 6
cts 7
cp 0.8571
cc 4
nc 3
nop 2
crap 4.0466
1
<?php
2
3
namespace ApacheSolrForTypo3\Solr\System\Language;
4
5
/***************************************************************
6
 *  Copyright notice
7
 *
8
 *  (c) 2018 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 3 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 ApacheSolrForTypo3\Solr\System\TCA\TCAService;
29
use ApacheSolrForTypo3\Solr\Util;
30
use Doctrine\DBAL\Driver\Statement;
31
use TYPO3\CMS\Core\Database\ConnectionPool;
32
use TYPO3\CMS\Core\Utility\GeneralUtility;
33
use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;
34
35
/**
36
 * Class FrontendOverlayService
37
 * @package ApacheSolrForTypo3\Solr\System\Language
38
 */
39
class FrontendOverlayService {
40
41
    /**
42
     * @var TCAService
43
     */
44
    protected $tcaService = null;
45
46
    /**
47
     * @var TypoScriptFrontendController
48
     */
49
    protected $tsfe = null;
50
51
    /**
52
     * Relation constructor.
53
     * @param TCAService|null $tcaService
54
     * @param TypoScriptFrontendController|null $tsfe
55
     */
56 47
    public function __construct(TCAService $tcaService = null, TypoScriptFrontendController $tsfe = null)
57
    {
58 47
        $this->tcaService = $tcaService ?? GeneralUtility::makeInstance(TCAService::class);
59 47
        $this->tsfe = $tsfe ?? $GLOBALS['TSFE'];
60 47
    }
61
62
    /**
63
     * Return the translated record
64
     *
65
     * @param string $tableName
66
     * @param array $record
67
     * @return array
68
     */
69 5
    public function getOverlay($tableName, $record)
70
    {
71 5
        if ($tableName === 'pages') {
72 2
            return $this->tsfe->sys_page->getPageOverlay($record, $this->tsfe->sys_language_uid);
0 ignored issues
show
Deprecated Code introduced by
The property TYPO3\CMS\Frontend\Contr...ller::$sys_language_uid has been deprecated: since TYPO3 v9.4, will be removed in TYPO3 v10.0 - use LanguageAspect->getId() instead. ( Ignorable by Annotation )

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

72
            return $this->tsfe->sys_page->getPageOverlay($record, /** @scrutinizer ignore-deprecated */ $this->tsfe->sys_language_uid);

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

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

Loading history...
73
        }
74
75 4
        return $this->tsfe->sys_page->getRecordOverlay($tableName, $record, $this->tsfe->sys_language_uid);
0 ignored issues
show
Deprecated Code introduced by
The property TYPO3\CMS\Frontend\Contr...ller::$sys_language_uid has been deprecated: since TYPO3 v9.4, will be removed in TYPO3 v10.0 - use LanguageAspect->getId() instead. ( Ignorable by Annotation )

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

75
        return $this->tsfe->sys_page->getRecordOverlay($tableName, $record, /** @scrutinizer ignore-deprecated */ $this->tsfe->sys_language_uid);

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

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

Loading history...
76
    }
77
78
    /**
79
     * When the record has an overlay we retrieve the uid of the translated record,
80
     * to resolve the relations from the translation.
81
     *
82
     * @param string $table
83
     * @param string $field
84
     * @param int $uid
85 47
     * @return int
86
     */
87
    public function getUidOfOverlay($table, $field, $uid)
0 ignored issues
show
Unused Code introduced by
The parameter $field 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

87
    public function getUidOfOverlay($table, /** @scrutinizer ignore-unused */ $field, $uid)

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...
88 47
    {
89 47
        // when no language is set at all we do not need to overlay
90 47
        if (!isset($this->tsfe->sys_language_uid)) {
0 ignored issues
show
Deprecated Code introduced by
The property TYPO3\CMS\Frontend\Contr...ller::$sys_language_uid has been deprecated: since TYPO3 v9.4, will be removed in TYPO3 v10.0 - use LanguageAspect->getId() instead. ( Ignorable by Annotation )

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

90
        if (!isset(/** @scrutinizer ignore-deprecated */ $this->tsfe->sys_language_uid)) {

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

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

Loading history...
91 47
            return $uid;
92 1
        }
93
        // when no language is set we can return the passed recordUid
94
        if (!$this->tsfe->sys_language_uid > 0) {
95 47
            return $uid;
96
        }
97
98
        $record = $this->getRecord($table, $uid);
99
100
        // when the overlay is not an array, we return the localRecordUid
101
        if (!is_array($record)) {
102
            return $uid;
103
        }
104
105
        $overlayUid = $this->getLocalRecordUidFromOverlay($table, $record);
106
        $uid = ($overlayUid !== 0) ? $overlayUid : $uid;
107 47
        return $uid;
108
    }
109
110 47
    /**
111
     * This method retrieves the _PAGES_OVERLAY_UID or _LOCALIZED_UID from the localized record.
112
     *
113
     * @param string $localTableName
114 47
     * @param array $originalRecord
115 45
     * @return int
116
     */
117
    protected function getLocalRecordUidFromOverlay($localTableName, $originalRecord)
118
    {
119 5
        $overlayRecord = $this->getOverlay($localTableName, $originalRecord);
120 5
121 5
        // when there is a _PAGES_OVERLAY_UID | _LOCALIZED_UID in the overlay, we return it
122 2
        if ($localTableName === 'pages' && isset($overlayRecord['_PAGES_OVERLAY_UID'])) {
123
            return (int)$overlayRecord['_PAGES_OVERLAY_UID'];
124
        } elseif (isset($overlayRecord['_LOCALIZED_UID'])) {
125 3
            return (int)$overlayRecord['_LOCALIZED_UID'];
126
        }
127
128 3
        return 0;
129
    }
130
131
    /**
132 3
     * @param $localTableName
133 3
     * @param $localRecordUid
134 3
     * @return mixed
135
     */
136
    protected function getRecord($localTableName, $localRecordUid)
137
    {
138
        /** @var QueryBuilder $queryBuilder */
139
        $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable($localTableName);
140
141
        $record = $queryBuilder->select('*')->from($localTableName)->where($queryBuilder->expr()->eq('uid', $localRecordUid))->execute()->fetch();
142
        return $record;
143
    }
144
}