Passed
Push — master ( 25c114...6d950c )
by
unknown
10:26 queued 07:40
created

BaseCommand::getStartingPoint()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
c 1
b 0
f 0
dl 0
loc 6
rs 10
cc 2
nc 2
nop 1
1
<?php
2
3
/**
4
 * (c) Kitodo. Key to digital objects e.V. <[email protected]>
5
 *
6
 * This file is part of the Kitodo and TYPO3 projects.
7
 *
8
 * @license GNU General Public License version 3 or later.
9
 * For the full copyright and license information, please read the
10
 * LICENSE.txt file that was distributed with this source code.
11
 */
12
13
namespace Kitodo\Dlf\Command;
14
15
use Symfony\Component\Console\Command\Command;
16
use TYPO3\CMS\Core\Utility\GeneralUtility;
17
use TYPO3\CMS\Core\Utility\MathUtility;
18
use TYPO3\CMS\Core\Database\ConnectionPool;
19
use TYPO3\CMS\Core\Database\Connection;
20
21
/**
22
 * Base class for CLI Command classes.
23
 *
24
 * @author Beatrycze Volk <[email protected]>
25
 * @package TYPO3
26
 * @subpackage dlf
27
 * @access public
28
 */
29
class BaseCommand extends Command
30
{
31
    /**
32
     * Return starting point for indexing command.
33
     *
34
     * @param string|string[]|bool|null $inputPid possible pid
35
     *
36
     * @return int starting point for indexing command
37
     */
38
    protected function getStartingPoint($inputPid): int
39
    {
40
        if (MathUtility::canBeInterpretedAsInteger($inputPid)) {
41
            return MathUtility::forceIntegerInRange((int) $inputPid, 0);
42
        }
43
        return 0;
44
    }
45
46
    /**
47
     * Return matching uid of Solr core depending on the input value.
48
     *
49
     * @param array $solrCores array of the valid Solr cores
50
     * @param string|bool|null $inputSolrId possible uid or name of Solr core
51
     *
52
     * @return int matching uid of Solr core
53
     */
54
    protected function getSolrCoreUid(array $solrCores, $inputSolrId): int
55
    {
56
        if (MathUtility::canBeInterpretedAsInteger($inputSolrId)) {
57
            $solrCoreUid = MathUtility::forceIntegerInRange((int) $inputSolrId, 0);
58
        } else {
59
            $solrCoreUid = $solrCores[$inputSolrId];
60
        }
61
        return $solrCoreUid;
62
    }
63
64
    /**
65
     * Fetches all Solr cores on given page.
66
     *
67
     * @param int $pageId The UID of the Solr core or 0 to disable indexing
68
     *
69
     * @return array Array of valid Solr cores
70
     */
71
    protected function getSolrCores(int $pageId): array
72
    {
73
        $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
74
            ->getQueryBuilderForTable('tx_dlf_solrcores');
75
76
        $solrCores = [];
77
        $result = $queryBuilder
78
            ->select('uid', 'index_name')
79
            ->from('tx_dlf_solrcores')
80
            ->where(
81
                $queryBuilder->expr()->eq(
82
                    'pid',
83
                    $queryBuilder->createNamedParameter((int) $pageId, Connection::PARAM_INT)
84
                )
85
            )
86
            ->execute();
87
88
        while ($record = $result->fetch()) {
89
            $solrCores[$record['index_name']] = $record['uid'];
90
        }
91
92
        return $solrCores;
93
    }
94
}
95