Completed
Push — master ( e5c82c...32f441 )
by Fabien
04:55
created

ext_update::access()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 3
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
namespace Fab\Vidi;
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
/**
18
 * Updater script for Ichtus.
19
 */
20
class ext_update {
21
22
	/**
23
	 * @return bool
24
	 */
25
	public function access() {
26
		return TRUE;
27
	}
28
29
	/**
30
	 * @return string
31
	 */
32
	public function main() {
33
		$output[] = $this->updateSelections();
0 ignored issues
show
Coding Style Comprehensibility introduced by
$output was never initialized. Although not strictly required by PHP, it is generally a good practice to add $output = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
34
35
		return sprintf('<ul><li style="float: none">%s</li></ul>', implode('</li><li style="float: none">', $output));
36
	}
37
38
	/**
39
	 * @return string
40
	 */
41
	protected function updateSelections() {
42
43
		$tableName = 'tx_vidi_selection';
44
		$fields = $this->getDatabaseConnection()->admin_get_fields($tableName);
45
46
		if (!isset($fields['speaking_query'])) {
47
			$sql = 'ALTER TABLE tx_vidi_selection ADD speaking_query text;';
48
			$this->getDatabaseConnection()->sql_query($sql);
49
		}
50
51
		$sql = 'UPDATE tx_vidi_selection SET speaking_query = query WHERE (speaking_query = "" OR speaking_query is NULL) AND query != "";';
52
		$this->getDatabaseConnection()->sql_query($sql);
53
54
		$output = '<br/><strong>Table tx_vidi_selection has been updated</strong><br/>';
55
		return $output;
56
	}
57
58
	/**
59
	 * Return a pointer to the database.
60
	 *
61
	 * @return \TYPO3\CMS\Core\Database\DatabaseConnection
62
	 */
63
	protected function getDatabaseConnection() {
0 ignored issues
show
Coding Style introduced by
getDatabaseConnection uses the super-global variable $GLOBALS which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
64
		return $GLOBALS['TYPO3_DB'];
65
	}
66
67
}
68