Completed
Push — middleware-wip ( ffd957...48fd00 )
by Romain
02:53
created

PageExistsValidator::isValid()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 18
rs 9.2
cc 4
eloc 11
nc 2
nop 1
1
<?php
2
/*
3
 * 2017 Romain CANON <[email protected]>
4
 *
5
 * This file is part of the TYPO3 FormZ project.
6
 * It is free software; you can redistribute it and/or modify it
7
 * under the terms of the GNU General Public License, either
8
 * version 3 of the License, or any later version.
9
 *
10
 * For the full copyright and license information, see:
11
 * http://www.gnu.org/licenses/gpl-3.0.html
12
 */
13
14
namespace Romm\Formz\Validation\Validator;
15
16
use Romm\Formz\Core\Core;
17
use TYPO3\CMS\Extbase\Validation\Validator\AbstractValidator;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Romm\Formz\Validation\Validator\AbstractValidator.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
18
19
class PageExistsValidator extends AbstractValidator
20
{
21
    const MESSAGE_DEFAULT = 'default';
22
23
    /**
24
     * @inheritdoc
25
     */
26
    protected $supportedMessages = [
27
        self::MESSAGE_DEFAULT => [
28
            'key'       => 'validator.page_exists.error',
29
            'extension' => null
30
        ]
31
    ];
32
33
    /**
34
     * @inheritdoc
35
     */
36
    public function isValid($uid)
37
    {
38
        $page = Core::get()->getDatabase()->exec_SELECTgetSingleRow(
39
            'uid',
40
            'pages',
41
            'deleted=0 AND uid=' . (int)$uid
42
        );
43
44
        if (false === is_array($page)
45
            || false === isset($page['uid'])
46
            || $uid != $page['uid']
47
        ) {
48
            $this->addError(
49
                $this->translateErrorMessage('validator.page_exists.error', 'formz', [$uid]),
50
                1491829709
51
            );
52
        }
53
    }
54
}
55