Passed
Branch master (1d4f77)
by Romain
05:21
created

countFromEventDefinition()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
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
     * @param EventDefinition $eventDefinition
43
     * @return QueryResultInterface
44
     */
45
    public function findFromEventDefinition(EventDefinition $eventDefinition)
46
    {
47
        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...
48
    }
49
50
    /**
51
     * @param EventDefinition $eventDefinition
52
     * @return int
53
     */
54
    public function countFromEventDefinition(EventDefinition $eventDefinition)
55
    {
56
        return $this->getQueryForEvent($eventDefinition)->count();
57
    }
58
59
    /**
60
     * Returns the wanted notification even if it was disabled.
61
     *
62
     * @param mixed $identifier
63
     * @return EntityNotification|object
64
     */
65
    public function findByIdentifierForce($identifier)
66
    {
67
        $query = $this->createQueryWithoutEnableStatement();
68
69
        $query->matching(
70
            $query->logicalAnd(
71
                $query->equals('uid', $identifier),
72
                $query->equals('deleted', false)
73
            )
74
        );
75
76
        return $query->execute()->getFirst();
77
    }
78
79
    /**
80
     * @return QueryInterface
81
     */
82
    protected function createQueryWithoutEnableStatement()
83
    {
84
        $query = $this->createQuery();
85
        $query->getQuerySettings()
86
            ->setIgnoreEnableFields(true);
87
88
        return $query;
89
    }
90
91
    /**
92
     * @param EventDefinition $eventDefinition
93
     * @return QueryInterface
94
     */
95
    protected function getQueryForEvent(EventDefinition $eventDefinition)
96
    {
97
        $query = $this->createQuery();
98
99
        $query->matching(
100
            $query->equals('event', $eventDefinition->getFullIdentifier())
101
        );
102
103
        return $query;
104
    }
105
}
106