Passed
Push — release-11.5.x ( 2858e4...857414 )
by Rafael
39:14 queued 21:21
created

FrontendOverlayService   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 123
Duplicated Lines 0 %

Test Coverage

Coverage 96.97%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 13
eloc 32
c 2
b 0
f 0
dl 0
loc 123
ccs 32
cts 33
cp 0.9697
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getLocalRecordUidFromOverlay() 0 13 4
A __construct() 0 4 1
A getUidOfOverlay() 0 24 5
A getOverlay() 0 10 2
A getRecord() 0 11 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the TYPO3 CMS project.
7
 *
8
 * It is free software; you can redistribute it and/or modify it under
9
 * the terms of the GNU General Public License, either version 2
10
 * of the License, or any later version.
11
 *
12
 * For the full copyright and license information, please read the
13
 * LICENSE.txt file that was distributed with this source code.
14
 *
15
 * The TYPO3 project - inspiring people to share!
16
 */
17
18
namespace ApacheSolrForTypo3\Solr\System\Language;
19
20
use ApacheSolrForTypo3\Solr\System\TCA\TCAService;
21
use Doctrine\DBAL\Driver\Exception as DBALDriverException;
22
use Doctrine\DBAL\Exception as DBALException;
23
use TYPO3\CMS\Core\Context\Exception\AspectNotFoundException;
24
use TYPO3\CMS\Core\Database\ConnectionPool;
25
use TYPO3\CMS\Core\Database\Query\QueryBuilder;
26
use TYPO3\CMS\Core\Utility\GeneralUtility;
27
use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;
28
29
/**
30
 * Class FrontendOverlayService
31
 */
32
class FrontendOverlayService
33
{
34
    /**
35
     * @var TCAService
36
     */
37
    protected $tcaService;
38
39
    /**
40
     * @var TypoScriptFrontendController|null
41
     */
42
    protected ?TypoScriptFrontendController $tsfe = null;
43
44
    /**
45
     * Relation constructor.
46
     * @param TCAService|null $tcaService
47
     * @param TypoScriptFrontendController|null $tsfe
48
     */
49 13
    public function __construct(TCAService $tcaService = null, TypoScriptFrontendController $tsfe = null)
50
    {
51 13
        $this->tcaService = $tcaService ?? GeneralUtility::makeInstance(TCAService::class);
52 13
        $this->tsfe = $tsfe;
53
    }
54
55
    /**
56
     * Return the translated record
57
     *
58
     * @param string $tableName
59
     * @param array $record
60
     * @return array
61
     * @throws AspectNotFoundException
62
     */
63 10
    public function getOverlay(string $tableName, array $record): ?array
64
    {
65 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

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

90
        /** @scrutinizer ignore-unused */ string $field,

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