Completed
Branch TYPO3V8 (1e9d52)
by Tomas Norre
12:04
created

Typo3Service::setDbLink()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
namespace Aoe\AoeDbSequenzer\Service;
3
4
/***************************************************************
5
 *  Copyright notice
6
 *
7
 *  (c) 2017 AOE GmbH ([email protected])
8
 *  All rights reserved
9
 *
10
 *  This script is part of the TYPO3 project. The TYPO3 project is
11
 *  free software; you can redistribute it and/or modify
12
 *  it under the terms of the GNU General Public License as published by
13
 *  the Free Software Foundation; either version 2 of the License, or
14
 *  (at your option) any later version.
15
 *
16
 *  The GNU General Public License can be found at
17
 *  http://www.gnu.org/copyleft/gpl.html.
18
 *
19
 *  This script is distributed in the hope that it will be useful,
20
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
21
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22
 *  GNU General Public License for more details.
23
 *
24
 *  This copyright notice MUST APPEAR in all copies of the script!
25
 ***************************************************************/
26
27
use Aoe\AoeDbSequenzer\Sequenzer;
28
use TYPO3\CMS\Core\Utility\GeneralUtility;
29
30
/**
31
 * @package Aoe\AoeDbSequenzer
32
 */
33
class Typo3Service
34
{
35
    /**
36
     * @var Sequenzer
37
     */
38
    private $sequenzer;
39
40
    /**
41
     * @var array
42
     */
43
    private $conf;
44
45
    /**
46
     * array of configured tables that should call the sequenzer
47
     *
48
     * @var array
49
     */
50
    private $supportedTables;
51
52
    /**
53
     * @param Sequenzer $sequenzer
54
     */
55
    public function __construct(Sequenzer $sequenzer)
56
    {
57
        $this->conf = unserialize($GLOBALS['TYPO3_CONF_VARS']['EXT']['extConf']['aoe_dbsequenzer']);
58
59
        $this->sequenzer = $sequenzer;
60
        $this->sequenzer->setDefaultOffset(intval($this->conf['offset']));
61
        $this->sequenzer->setDefaultStart(intval($this->conf['system']));
62
63
        $explodedValues = explode(',', $this->conf['tables']);
64
        $this->supportedTables = array_map('trim', $explodedValues);
65
    }
66
67
    /**
68
     * Modify a TYPO3 insert array (key -> value) , and adds the uid that should be forced during INSERT
69
     *
70
     * @param string $tableName
71
     * @param array  $fields_values
72
     *
73
     * @return array
74
     */
75
    public function modifyInsertFields($tableName, array $fields_values)
76
    {
77
        if (false === $this->needsSequenzer($tableName)) {
78
            return $fields_values;
79
        }
80
81
        if (isset($fields_values['uid'])) {
82
            $e = new \Exception('', 1512378232);
83
            GeneralUtility::devLog(
84
                'UID ' . $fields_values['uid'] . ' is already set for table "' . $tableName . '"',
85
                'aoe_dbsequenzer',
86
                2,
87
                $e->getTraceAsString()
88
            );
89
        } else {
90
            $fields_values['uid'] = $this->sequenzer->getNextIdForTable($tableName);
91
        }
92
93
        return $fields_values;
94
    }
95
96
    /**
97
     * If a table is configured to use the sequenzer
98
     *
99
     * @param string $tableName
100
     * @return boolean
101
     */
102
    public function needsSequenzer($tableName)
103
    {
104
        return in_array($tableName, $this->supportedTables);
105
    }
106
}
107