Passed
Push — master ( c2c83c...eb4088 )
by Nathan
03:39
created

EntityNotificationRepository::createQuery()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * Copyright (C) 2018
5
 * Nathan Boiron <[email protected]>
6
 * Romain Canon <[email protected]>
7
 *
8
 * This file is part of the TYPO3 NotiZ project.
9
 * It is free software; you can redistribute it and/or modify it
10
 * under the terms of the GNU General Public License, either
11
 * version 3 of the License, or any later version.
12
 *
13
 * For the full copyright and license information, see:
14
 * http://www.gnu.org/licenses/gpl-3.0.html
15
 */
16
17
namespace CuyZ\Notiz\Domain\Repository;
18
19
use CuyZ\Notiz\Core\Definition\Tree\EventGroup\Event\EventDefinition;
20
use CuyZ\Notiz\Domain\Notification\EntityNotification;
21
use TYPO3\CMS\Extbase\Persistence\Generic\QuerySettingsInterface;
22
use TYPO3\CMS\Extbase\Persistence\QueryInterface;
23
use TYPO3\CMS\Extbase\Persistence\QueryResultInterface;
24
use TYPO3\CMS\Extbase\Persistence\Repository;
25
26
class EntityNotificationRepository extends Repository
27
{
28
    /**
29
     * Initializes the query settings.
30
     */
31
    public function initializeObject()
32
    {
33
        /** @var $querySettings QuerySettingsInterface */
34
        $querySettings = $this->objectManager->get(QuerySettingsInterface::class);
35
36
        $querySettings->setRespectStoragePage(false);
37
38
        $this->setDefaultQuerySettings($querySettings);
39
    }
40
41
    /**
42
     * @return QueryResultInterface
43
     */
44
    public function findAllWithDisabled()
45
    {
46
        return $this->createQueryWithoutEnableStatement()->execute();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->createQuer...eStatement()->execute() also could return the type array which is incompatible with the documented return type TYPO3\CMS\Extbase\Persistence\QueryResultInterface.
Loading history...
47
    }
48
49
    /**
50
     * @param EventDefinition $eventDefinition
51
     * @return QueryResultInterface
52
     */
53
    public function findFromEventDefinition(EventDefinition $eventDefinition)
54
    {
55
        return $this->getQueryForEvent($eventDefinition)->execute();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->getQueryFo...tDefinition)->execute() also could return the type array which is incompatible with the documented return type TYPO3\CMS\Extbase\Persistence\QueryResultInterface.
Loading history...
56
    }
57
58
    /**
59
     * @param EventDefinition $eventDefinition
60
     * @return QueryResultInterface
61
     */
62
    public function findFromEventDefinitionWithDisabled(EventDefinition $eventDefinition)
63
    {
64
        return $this->getQueryForEvent($eventDefinition, true)->execute();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->getQueryFo...ition, true)->execute() also could return the type array which is incompatible with the documented return type TYPO3\CMS\Extbase\Persistence\QueryResultInterface.
Loading history...
65
    }
66
67
    /**
68
     * @param EventDefinition $eventDefinition
69
     * @return int
70
     */
71
    public function countFromEventDefinition(EventDefinition $eventDefinition)
72
    {
73
        return $this->getQueryForEvent($eventDefinition)->count();
74
    }
75
76
    /**
77
     * Returns the wanted notification even if it was disabled.
78
     *
79
     * @param mixed $identifier
80
     * @return EntityNotification|object
81
     */
82
    public function findByIdentifierForce($identifier)
83
    {
84
        $query = $this->createQueryWithoutEnableStatement();
85
86
        $query->matching(
87
            $query->logicalAnd(
88
                $query->equals('uid', $identifier),
89
                $query->equals('deleted', false)
90
            )
91
        );
92
93
        return $query->execute()->getFirst();
94
    }
95
96
    /**
97
     * @return QueryInterface
98
     */
99
    protected function createQueryWithoutEnableStatement()
100
    {
101
        $query = $this->createQuery();
102
        $query->getQuerySettings()
103
            ->setIgnoreEnableFields(true);
104
105
        return $query;
106
    }
107
108
    /**
109
     * @param EventDefinition $eventDefinition
110
     * @param bool $withDisabled
111
     * @return QueryInterface
112
     */
113
    protected function getQueryForEvent(EventDefinition $eventDefinition, $withDisabled = false)
114
    {
115
        $query = $this->createQuery($withDisabled);
116
117
        $query->matching(
118
            $query->equals('event', $eventDefinition->getFullIdentifier())
119
        );
120
121
        return $query;
122
    }
123
124
    /**
125
     * @param bool $withDisabled
126
     * @return QueryInterface
127
     */
128
    public function createQuery($withDisabled = false)
129
    {
130
        if (true === $withDisabled) {
131
            return $this->createQueryWithoutEnableStatement();
132
        }
133
134
        return parent::createQuery();
135
    }
136
}
137