Completed
Push — master ( 34f9ca...cb41b9 )
by Tim
02:22
created

EventRepository::findNextIndex()   A

Complexity

Conditions 5
Paths 7

Size

Total Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 31
rs 9.1128
c 0
b 0
f 0
cc 5
nc 7
nop 1
1
<?php
2
3
/**
4
 * Event repository.
5
 */
6
declare(strict_types=1);
7
8
namespace HDNET\Calendarize\Domain\Repository;
9
10
use HDNET\Calendarize\Domain\Model\Event;
11
use HDNET\Calendarize\Domain\Model\Index;
12
use TYPO3\CMS\Extbase\Object\ObjectManager;
13
use TYPO3\CMS\Extbase\Persistence\Generic\Query;
14
use TYPO3\CMS\Extbase\Persistence\QueryInterface;
15
16
/**
17
 * Event repository.
18
 */
19
class EventRepository extends AbstractRepository
20
{
21
    /**
22
     * Get the IDs of the given search term.
23
     *
24
     * @param string $searchTerm
25
     *
26
     * @return array
27
     */
28
    public function getIdsBySearchTerm($searchTerm)
29
    {
30
        $query = $this->createQuery();
31
        $constraint = [];
32
        $constraint[] = $query->like('title', '%' . $searchTerm . '%');
33
        $constraint[] = $query->like('description', '%' . $searchTerm . '%');
34
        $query->matching($query->logicalOr($constraint));
35
        $rows = $query->execute(true);
36
37
        $ids = [];
38
        foreach ($rows as $row) {
39
            $ids[] = (int) $row['uid'];
40
        }
41
42
        return $ids;
43
    }
44
45
    /**
46
     * Return the current tablename.
47
     *
48
     * @return string
49
     */
50
    public function getTableName()
51
    {
52
        $query = $this->createQuery();
53
        if ($query instanceof Query) {
54
            $source = $query->getSource();
55
            if (\method_exists($source, 'getSelectorName')) {
56
                return $source->getSelectorName();
57
            }
58
        }
59
    }
60
61
    /**
62
     * Get the right Index ID by the event ID
63
     *
64
     * @param int $uid
65
     *
66
     * @return Index|null
67
     */
68
    protected function findNextIndex(int $uid)
69
    {
70
        /** @var Event $event */
71
        $event = $this->findByUid($uid);
72
73
        if (!is_object($event)) {
74
            return;
75
        }
76
77
        $objectManager = new ObjectManager();
78
        /** @var IndexRepository $indexRepository */
79
        $indexRepository = $objectManager->get(IndexRepository::class);
80
81
        try {
82
            $result = $indexRepository->findByEventTraversing($event, true, false, 1, QueryInterface::ORDER_ASCENDING);
83
            if (empty($result)) {
84
                $result = $indexRepository->findByEventTraversing($event, false, true, 1, QueryInterface::ORDER_DESCENDING);
85
            }
86
        } catch (\Exception $ex) {
87
            return;
88
89
        }
90
91
        if (empty($result)) {
92
            return;
93
        }
94
95
        /** @var Index $index */
96
        $index = $result[0];
97
        return $index;
98
    }
99
}
100