Completed
Push — master ( db38bb...3477f2 )
by Krystian
15s queued 10s
created

DatabaseRecordLinkBuilder::build()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 19
rs 9.6333
c 0
b 0
f 0
cc 1
nc 1
nop 4
1
<?php
2
declare(strict_types=1);
3
4
namespace SourceBroker\Hugo\Typolink;
5
6
/*
7
 * This file is part of the TYPO3 CMS project.
8
 *
9
 * It is free software; you can redistribute it and/or modify it under
10
 * the terms of the GNU General Public License, either version 2
11
 * of the License, or any later version.
12
 *
13
 * For the full copyright and license information, please read the
14
 * LICENSE.txt file that was distributed with this source code.
15
 *
16
 * The TYPO3 project - inspiring people to share!
17
 */
18
19
use Cocur\Slugify\Slugify;
20
use TYPO3\CMS\Core\Database\ConnectionPool;
21
use TYPO3\CMS\Core\Utility\GeneralUtility;
22
use TYPO3\CMS\Extbase\Object\ObjectManager;
23
24
/**
25
 * Builds a TypoLink to a database record
26
 */
27
class DatabaseRecordLinkBuilder extends AbstractTypolinkBuilder
28
{
29
    /**
30
     * @inheritdoc
31
     */
32
    public function build(array &$linkDetails, string $linkText, string $target, array $conf): array
33
    {
34
        $linkHandlerConfiguration = $this->txHugoConfigurator->getOption('record.indexer.exporter.' . $linkDetails['identifier']);
35
36
        $pageLinkBuilder = GeneralUtility::makeInstance(ObjectManager::class)->get(PageLinkBuilder::class, $this->txHugoConfigurator);
37
38
        $pageLinkDetails = ['pageuid' => $linkHandlerConfiguration['pageUid']];
39
40
        list($pageUri) = $pageLinkBuilder->build($pageLinkDetails, '', '', []);
41
        $record = $this->getRecordByUid($linkHandlerConfiguration['table'], (int)$linkDetails['uid']);
42
        // @todo make slug configurable and use same approach in \SourceBroker\Hugo\Indexer\RecordIndexer::getDocumentsForPage
43
        $recordUri = $record['uid'] . '_' . (new Slugify())->slugify($record['title']) . '/';
44
45
        return [
46
            $pageUri . $recordUri,
47
            $linkText,
48
            $target
49
        ];
50
    }
51
52
    /**
53
     * @param $table
54
     * @param $recordUid
55
     *
56
     * @return int
57
     */
58
    protected function getRecordByUid($table, $recordUid)
59
    {
60
        return ($qb = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable($table))
61
            ->select('*')
62
            ->from($table)
63
            ->where($qb->expr()->eq('uid', $qb->createNamedParameter($recordUid, \PDO::PARAM_INT)))
64
            ->execute()
65
            ->fetch();
66
    }
67
}
68