[TASK] Improve duplicate exception code check
[Packages/TYPO3.CMS.git] / typo3 / sysext / workspaces / Classes / Domain / Record / AbstractRecord.php
1 <?php
2 namespace TYPO3\CMS\Workspaces\Domain\Record;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 use TYPO3\CMS\Core\Database\ConnectionPool;
18 use TYPO3\CMS\Core\Database\Query\Restriction\DeletedRestriction;
19 use TYPO3\CMS\Core\Utility\GeneralUtility;
20 use TYPO3\CMS\Workspaces\Service\StagesService;
21
22 /**
23 * Combined record class
24 */
25 abstract class AbstractRecord
26 {
27 /**
28 * @var array
29 */
30 protected $record;
31
32 protected static function fetch($tableName, $uid)
33 {
34 $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable($tableName);
35 $queryBuilder->getRestrictions()->removeAll()->add(GeneralUtility::makeInstance(DeletedRestriction::class));
36 $record = $queryBuilder->select('*')
37 ->from($tableName)
38 ->where($queryBuilder->expr()->eq('uid', (int)$uid))
39 ->execute()
40 ->fetch();
41 if (empty($record)) {
42 throw new \RuntimeException('Record "' . $tableName . ': ' . $uid . '" not found', 1476122008);
43 }
44 return $record;
45 }
46
47 /**
48 * @return \TYPO3\CMS\Core\Database\DatabaseConnection
49 */
50 protected static function getDatabaseConnection()
51 {
52 return $GLOBALS['TYPO3_DB'];
53 }
54
55 /**
56 * @return \TYPO3\CMS\Core\Authentication\BackendUserAuthentication
57 */
58 protected static function getBackendUser()
59 {
60 return $GLOBALS['BE_USER'];
61 }
62
63 /**
64 * @return \TYPO3\CMS\Lang\LanguageService
65 */
66 protected static function getLanguageService()
67 {
68 return $GLOBALS['LANG'];
69 }
70
71 /**
72 * @param array $record
73 */
74 public function __construct(array $record)
75 {
76 $this->record = $record;
77 }
78
79 /**
80 * @return string
81 */
82 public function __toString()
83 {
84 return (string)$this->getUid();
85 }
86
87 /**
88 * @return int
89 */
90 public function getUid()
91 {
92 return (int)$this->record['uid'];
93 }
94
95 /**
96 * @return string
97 */
98 public function getTitle()
99 {
100 return (string)$this->record['title'];
101 }
102
103 /**
104 * @return StagesService
105 */
106 protected function getStagesService()
107 {
108 return GeneralUtility::makeInstance(StagesService::class);
109 }
110 }