Passed
Push — task/2976_TYPO3.11_compatibili... ( 803400...b598e4 )
by Rafael
33:58 queued 15:41
created

getLocalRecordUidFromOverlay()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 6
c 1
b 0
f 0
dl 0
loc 12
ccs 7
cts 7
cp 1
rs 10
cc 4
nc 3
nop 2
crap 4
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 TYPO3\CMS\Core\Context\Exception\AspectNotFoundException;
30
use TYPO3\CMS\Core\Database\ConnectionPool;
31
use TYPO3\CMS\Core\Database\Query\QueryBuilder;
32
use TYPO3\CMS\Core\Utility\GeneralUtility;
33
use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;
34
35
/**
36
 * Class FrontendOverlayService
37
 */
38
class FrontendOverlayService
39
{
40
41
    /**
42
     * @var TCAService
43
     */
44
    protected $tcaService = null;
45
46
    /**
47
     * @var TypoScriptFrontendController|null
48
     */
49
    protected ?TypoScriptFrontendController $tsfe = null;
50
51
    /**
52
     * Relation constructor.
53
     * @param TCAService|null $tcaService
54
     * @param TypoScriptFrontendController|null $tsfe
55
     */
56 13
    public function __construct(TCAService $tcaService = null, TypoScriptFrontendController $tsfe = null)
57
    {
58 13
        $this->tcaService = $tcaService ?? GeneralUtility::makeInstance(TCAService::class);
59 13
        $this->tsfe = $tsfe;
60 13
    }
61
62
    /**
63
     * Return the translated record
64
     *
65
     * @param string $tableName
66
     * @param array $record
67
     * @return array
68
     * @throws AspectNotFoundException
69
     */
70 10
    public function getOverlay(string $tableName, array $record): ?array
71
    {
72 10
        $currentLanguageUid = $this->tsfe->getContext()->getPropertyFromAspect('language', 'id');
0 ignored issues
show
Bug introduced by
The method getContext() does not exist on null. ( Ignorable by Annotation )

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

72
        $currentLanguageUid = $this->tsfe->/** @scrutinizer ignore-call */ getContext()->getPropertyFromAspect('language', 'id');

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
73 10
        if ($tableName === 'pages') {
74
            // @extensionScannerIgnoreLine
75 2
            return $this->tsfe->sys_page->getPageOverlay($record, $currentLanguageUid);
76
        }
77
78
        // @extensionScannerIgnoreLine
79 9
        return $this->tsfe->sys_page->getRecordOverlay($tableName, $record, $currentLanguageUid);
80
    }
81
82
    /**
83
     * When the record has an overlay we retrieve the uid of the translated record,
84
     * to resolve the relations from the translation.
85
     *
86
     * @param string $table
87
     * @param string $field
88
     * @param int $uid
89
     * @return int
90
     * @throws AspectNotFoundException
91
     */
92 13
    public function getUidOfOverlay(string $table, string $field, int $uid): int
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

92
    public function getUidOfOverlay(string $table, /** @scrutinizer ignore-unused */ string $field, int $uid): int

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...
93
    {
94 13
        $contextsLanguageId = $this->tsfe->getContext()->getPropertyFromAspect('language', 'id');
95
        // when no language is set at all we do not need to overlay
96 13
        if ($contextsLanguageId === null) {
97 2
            return $uid;
98
        }
99
        // when no language is set we can return the passed recordUid
100 11
        if (!($contextsLanguageId > 0)) {
101 11
            return $uid;
102
        }
103
104 10
        $record = $this->getRecord($table, $uid);
105
106
        // when the overlay is not an array, we return the localRecordUid
107 10
        if (!is_array($record)) {
108
            return $uid;
109
        }
110
111 10
        $overlayUid = $this->getLocalRecordUidFromOverlay($table, $record);
112 10
        return ($overlayUid !== 0) ? $overlayUid : $uid;
113
    }
114
115
    /**
116
     * This method retrieves the _PAGES_OVERLAY_UID or _LOCALIZED_UID from the localized record.
117
     *
118
     * @param string $localTableName
119
     * @param array $originalRecord
120
     * @return int
121
     * @throws AspectNotFoundException
122
     */
123 10
    protected function getLocalRecordUidFromOverlay(string $localTableName, array $originalRecord): int
124
    {
125 10
        $overlayRecord = $this->getOverlay($localTableName, $originalRecord);
126
127
        // when there is a _PAGES_OVERLAY_UID | _LOCALIZED_UID in the overlay, we return it
128 10
        if ($localTableName === 'pages' && isset($overlayRecord['_PAGES_OVERLAY_UID'])) {
129 1
            return (int)$overlayRecord['_PAGES_OVERLAY_UID'];
130 9
        } elseif (isset($overlayRecord['_LOCALIZED_UID'])) {
131 2
            return (int)$overlayRecord['_LOCALIZED_UID'];
132
        }
133
134 7
        return 0;
135
    }
136
137
    /**
138
     * @param $localTableName
139
     * @param $localRecordUid
140
     * @return mixed
141
     */
142 10
    protected function getRecord($localTableName, $localRecordUid)
143
    {
144
        /* @var QueryBuilder $queryBuilder */
145 10
        $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable($localTableName);
146
147 10
        return $queryBuilder->select('*')->from($localTableName)->where($queryBuilder->expr()->eq('uid', $localRecordUid))->execute()->fetch();
0 ignored issues
show
Deprecated Code introduced by
The function Doctrine\DBAL\ForwardCompatibility\Result::fetch() has been deprecated: Use fetchNumeric(), fetchAssociative() or fetchOne() instead. ( Ignorable by Annotation )

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

147
        return /** @scrutinizer ignore-deprecated */ $queryBuilder->select('*')->from($localTableName)->where($queryBuilder->expr()->eq('uid', $localRecordUid))->execute()->fetch();

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

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

Loading history...
148
    }
149
}
150