Completed
Push — master ( c66703...668741 )
by Fabien
03:49
created

SelectionRepository::findForEveryone()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 19
rs 9.4285
c 1
b 0
f 0
cc 1
eloc 9
nc 1
nop 1
1
<?php
2
namespace Fab\Vidi\Domain\Repository;
3
4
/*
5
 * This file is part of the Fab/Vidi project under GPLv2 or later.
6
 *
7
 * For the full copyright and license information, please read the
8
 * LICENSE.md file that was distributed with this source code.
9
 */
10
11
use TYPO3\CMS\Extbase\Persistence\Generic\QueryResult;
12
use TYPO3\CMS\Extbase\Persistence\QueryInterface;
13
use TYPO3\CMS\Extbase\Persistence\Repository;
14
use Fab\Vidi\Domain\Model\Selection;
15
16
/**
17
 * Repository for accessing Selections
18
 */
19
class SelectionRepository extends Repository
20
{
21
22
    /**
23
     * @param string $dataType
24
     * @return QueryResult
25
     */
26
    public function findByDataTypeForCurrentBackendUser($dataType)
27
    {
28
        $query = $this->createQuery();
29
30
        // Compute the OR part
31
        if ($this->getBackendUser()->isAdmin()) {
32
            $logicalOr = $query->logicalOr(
33
                $query->equals('visibility', Selection::VISIBILITY_EVERYONE),
34
                $query->equals('visibility', Selection::VISIBILITY_ADMIN_ONLY),
35
                $query->equals('cruser_id', $this->getBackendUser()->user['uid'])
36
            );
37
        } else {
38
            $logicalOr = $query->logicalOr(
39
                $query->equals('visibility', Selection::VISIBILITY_EVERYONE),
40
                $query->equals('cruser_id', $this->getBackendUser()->user['uid'])
41
            );
42
        }
43
44
        // Add matching criteria
45
        $query->matching(
46
            $query->logicalAnd(
47
                $query->equals('dataType', $dataType),
48
                $logicalOr
49
            )
50
        );
51
52
        // Set ordering
53
        $query->setOrderings(
54
            array('name' => QueryInterface::ORDER_ASCENDING)
55
        );
56
57
        return $query->execute();
58
    }
59
60
    /**
61
     * @param string $dataType
62
     * @return QueryResult
63
     */
64
    public function findForEveryone($dataType)
65
    {
66
        $query = $this->createQuery();
67
68
        // Add matching criteria
69
        $query->matching(
70
            $query->logicalAnd(
71
                $query->equals('dataType', $dataType),
72
                $query->equals('visibility', Selection::VISIBILITY_EVERYONE)
73
            )
74
        );
75
76
        // Set ordering
77
        $query->setOrderings(
78
            array('name' => QueryInterface::ORDER_ASCENDING)
79
        );
80
81
        return $query->execute();
82
    }
83
84
    /**
85
     * Returns an instance of the current Backend User.
86
     *
87
     * @return \TYPO3\CMS\Core\Authentication\BackendUserAuthentication
88
     */
89
    protected function getBackendUser()
0 ignored issues
show
Coding Style introduced by
getBackendUser 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...
90
    {
91
        return $GLOBALS['BE_USER'];
92
    }
93
94
}