SiteApiService   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 117
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 87.8%

Importance

Changes 3
Bugs 0 Features 3
Metric Value
wmc 10
c 3
b 0
f 3
lcom 1
cbo 1
dl 0
loc 117
ccs 36
cts 41
cp 0.878
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A injectObjectManager() 0 3 1
A getDatabaseHandler() 0 3 1
A getSiteInfo() 0 12 1
A createSysNews() 0 13 2
A getDiskUsage() 0 7 2
A getDatabaseSize() 0 10 2
A getCountOfExtensions() 0 8 1
1
<?php
2
namespace Etobi\CoreAPI\Service;
3
4
/***************************************************************
5
 *  Copyright notice
6
 *
7
 *  (c) 2012 Georg Ringer <[email protected]>
8
 *  (c) 2014 Stefano Kowalke <[email protected]>
9
 *  (c) 2013 Claus Due <[email protected]>, Wildside A/S
10
 *  All rights reserved
11
 *
12
 *  This script is part of the TYPO3 project. The TYPO3 project is
13
 *  free software; you can redistribute it and/or modify
14
 *  it under the terms of the GNU General Public License as published by
15
 *  the Free Software Foundation; either version 2 of the License, or
16
 *  (at your option) any later version.
17
 *
18
 *  The GNU General Public License can be found at
19
 *  http://www.gnu.org/copyleft/gpl.html.
20
 *
21
 *  This script is distributed in the hope that it will be useful,
22
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
23
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24
 *  GNU General Public License for more details.
25
 *
26
 *  This copyright notice MUST APPEAR in all copies of the script!
27
 ***************************************************************/
28
use InvalidArgumentException;
29
use TYPO3\CMS\Core\Utility\GeneralUtility;
30
31
/**
32
 * Site API service
33
 *
34
 * @author Georg Ringer <[email protected]>
35
 * @author Stefano Kowalke <[email protected]>
36
 * @author Claus Due <[email protected]>, Wildside A/S
37
 * @package Etobi\CoreAPI\Service\SiteApiService
38
 */
39
class SiteApiService {
40
41
	/**
42
	 * @var \TYPO3\CMS\Extbase\Object\ObjectManager
43
	 */
44
	protected $objectManager;
45
46
	/**
47
	 * Inject the ObjectManager
48
	 *
49
	 * @param \TYPO3\CMS\Extbase\Object\ObjectManager $objectManager
50
	 *
51
	 * @return void
52
	 */
53
	public function injectObjectManager(\TYPO3\CMS\Extbase\Object\ObjectManager $objectManager) {
54
		$this->objectManager = $objectManager;
55
	}
56
57
	/**
58
	 * Get some basic site information.
59
	 *
60
	 * @return array
61
	 */
62 1
	public function getSiteInfo() {
63
		$data = array(
64 1
			'TYPO3 version' => TYPO3_version,
65 1
			'Site name' => $GLOBALS['TYPO3_CONF_VARS']['SYS']['sitename'],
66 1
		);
67
68 1
		$data = $this->getDiskUsage($data);
69 1
		$data = $this->getDatabaseSize($data);
70 1
		$data = $this->getCountOfExtensions($data);
71
72 1
		return $data;
73
	}
74
75
	/**
76
	 * Create a sys news record.
77
	 *
78
	 * @param string $header header
79
	 * @param string $text   text
80
	 *
81
	 * @throws InvalidArgumentException
82
	 * @return boolean
83
	 */
84 2
	public function createSysNews($header, $text) {
85 2
		if (strlen($header) === 0) {
86 1
			throw new InvalidArgumentException('No header given');
87
		}
88
89 1
		return $this->getDatabaseHandler()->exec_INSERTquery('sys_news', array(
90 1
			'title' => $header,
91 1
			'content' => $text,
92 1
			'tstamp' => $GLOBALS['EXEC_TIME'],
93 1
			'crdate' => $GLOBALS['EXEC_TIME'],
94 1
			'cruser_id' => $GLOBALS['BE_USER']->user['uid']
95 1
		));
96
	}
97
98
	/**
99
	 * Get disk usage.
100
	 *
101
	 * @param array $data
102
	 *
103
	 * @return array
104
	 */
105 1
	public function getDiskUsage($data) {
106 1
		if (TYPO3_OS !== 'WIN') {
107 1
			$data['Combined disk usage'] = trim(array_shift(explode("\t", shell_exec('du -sh ' . PATH_site))));
0 ignored issues
show
Bug introduced by
explode(' ', shell_exec('du -sh ' . PATH_site)) cannot be passed to array_shift() as the parameter $array expects a reference.
Loading history...
108 1
		}
109
110 1
		return $data;
111
	}
112
113
	/**
114
	 * Get database size.
115
	 *
116
	 * @param array $data
117
	 *
118
	 * @return array
119
	 */
120 1
	public function getDatabaseSize($data) {
121 1
		$databaseHandler = $this->getDatabaseHandler();
122 1
		$databaseSizeResult = $databaseHandler->sql_query("SELECT SUM( data_length + index_length ) / 1024 / 1024 AS size FROM information_schema.TABLES WHERE table_schema = '" . TYPO3_db . "'");
123 1
		$databaseSizeRow = $databaseHandler->sql_fetch_assoc($databaseSizeResult);
124 1
		$databaseSize = array_pop($databaseSizeRow);
125 1
		$value = number_format($databaseSize, ($databaseSize > 10 ? 0 : 1)) . 'M';
126 1
		$data['Database size'] = $value;
127
128 1
		return $data;
129
	}
130
131
	/**
132
	 * Get count of local installed extensions.
133
	 *
134
	 * @param array $data
135
	 *
136
	 * @return array
137
	 */
138 1
	public function getCountOfExtensions($data) {
139
		/** @var \Etobi\CoreAPI\Service\ExtensionApiService $extensionService */
140 1
		$extensionService = $this->objectManager->get('Etobi\\CoreAPI\\Service\\ExtensionApiService');
141 1
		$extensions = $extensionService->listExtensions('Local');
142 1
		$data['Count local installed extensions'] = count($extensions);
143
144 1
		return $data;
145
	}
146
147
	/**
148
	 * Returns the DatabaseConnection
149
	 *
150
	 * @return \TYPO3\CMS\Core\Database\DatabaseConnection
151
	 */
152
	protected function getDatabaseHandler() {
153
		return $GLOBALS['TYPO3_DB'];
154
	}
155
}
156