Completed
Push — master ( ee9fc9...5a65c0 )
by Fabien
09:19
created

PageFacet::getDatabaseConnection()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace Fab\Vidi\Facet;
4
5
/*
6
 * This file is part of the Fab/Vidi project under GPLv2 or later.
7
 *
8
 * For the full copyright and license information, please read the
9
 * LICENSE.md file that was distributed with this source code.
10
 */
11
12
use Fab\Vidi\Module\ModuleLoader;
13
use Fab\Vidi\Persistence\Matcher;
14
use TYPO3\CMS\Backend\Utility\BackendUtility;
15
use TYPO3\CMS\Extbase\Utility\LocalizationUtility;
16
use TYPO3\CMS\Core\Utility\GeneralUtility;
17
use TYPO3\CMS\Lang\LanguageService;
18
19
/**
20
 * Class for configuring a custom Facet item.
21
 */
22
class PageFacet implements FacetInterface
23
{
24
25
    /**
26
     * @var string
27
     */
28
    protected $name;
29
30
    /**
31
     * @var string
32
     */
33
    protected $label;
34
35
    /**
36
     * @var bool
37
     */
38
    protected $canModifyMatcher = false;
39
40
    /**
41
     * Constructor of a Generic Facet in Vidi.
42
     *
43
     * @param string $label
44
     */
45
    public function __construct($label = '')
46
    {
47
        $this->name = 'pid';
48
        $this->label = $label;
49
    }
50
51
    /**
52
     * @return string
53
     */
54
    public function getName()
55
    {
56
        return $this->name;
57
    }
58
59
    /**
60
     * @return string
61
     */
62
    public function getLabel()
63
    {
64
        $label = '';
65
        try {
66
            $label = LocalizationUtility::translate($this->label, '');
67
        } catch (\InvalidArgumentException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
68
        }
69
70
        return $label;
71
    }
72
73
    /**
74
     * @return array
75
     */
76
    public function getSuggestions()
77
    {
78
        $values = [];
79
        foreach ($this->getStoragePages() as $page) {
80
            $values[] = [
81
                $page['uid'] => sprintf('%s (%s)', $page['title'], $page['uid'])
82
            ];
83
        }
84
        return $values;
85
    }
86
87
    /**
88
     * @return array
89
     */
90
    protected function getStoragePages()
91
    {
92
        $tableName = 'pages';
93
        $clause = sprintf(
94
            'uid IN (SELECT DISTINCT(pid) FROM %s WHERE 1=1 %s)',
95
            $this->getModuleLoader()->getDataType(),
96
            BackendUtility::deleteClause($this->getModuleLoader()->getDataType())
97
        );
98
        $clause .= BackendUtility::deleteClause('pages');
99
100
        $pages = $this->getDatabaseConnection()->exec_SELECTgetRows('*', $tableName, $clause);
101
102
        return is_array($pages)
103
            ? $pages
104
            : [];
105
    }
106
107
    /**
108
     * Returns a pointer to the database.
109
     *
110
     * @return \TYPO3\CMS\Core\Database\DatabaseConnection
111
     */
112
    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...
113
    {
114
        return $GLOBALS['TYPO3_DB'];
115
    }
116
117
    /**
118
     * @return LanguageService
119
     */
120 View Code Duplication
    protected function getLanguageService()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
Coding Style introduced by
getLanguageService 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...
121
    {
122
123
        /** @var LanguageService $langService */
124
        $langService = $GLOBALS['LANG'];
125
        if (!$langService) {
126
            $langService = GeneralUtility::makeInstance(LanguageService::class);
127
            $langService->init('en');
128
        }
129
130
        return $langService;
131
    }
132
133
    /**
134
     * @return bool
135
     */
136
    public function hasSuggestions()
137
    {
138
        return true;
139
    }
140
141
    /**
142
     * @param string $dataType
143
     * @return $this
144
     */
145
    public function setDataType($dataType)
146
    {
147
        $this->dataType = $dataType;
0 ignored issues
show
Bug introduced by
The property dataType does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
148
        return $this;
149
    }
150
151
    /**
152
     * @return bool
153
     */
154
    public function canModifyMatcher()
155
    {
156
        return $this->canModifyMatcher;
157
    }
158
159
    /**
160
     * @param Matcher $matcher
161
     * @param $value
162
     * @return Matcher
163
     */
164
    public function modifyMatcher(Matcher $matcher, $value)
165
    {
166
        return $matcher;
167
    }
168
169
    /**
170
     * Get the Vidi Module Loader.
171
     *
172
     * @return ModuleLoader|object
173
     * @throws \InvalidArgumentException
174
     */
175
    protected function getModuleLoader()
176
    {
177
        return GeneralUtility::makeInstance(ModuleLoader::class);
178
    }
179
180
    /**
181
     * Magic method implementation for retrieving state.
182
     *
183
     * @param array $states
184
     * @return PageFacet
185
     */
186
    static public function __set_state($states)
0 ignored issues
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
187
    {
188
        return new self($states['name']);
189
    }
190
191
}
192