Completed
Push — master ( 4d374d...af07f3 )
by Tim
04:33
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
     * @param $importId
47
     * @return mixed|null
48
     */
49
    public function findOneByImportId($importId)
50
    {
51
        $query = $this->createQuery();
52
        $query->getQuerySettings()->setRespectStoragePage(false);
53
        $query->getQuerySettings()->setIgnoreEnableFields(true);
54
        $query->matching($query->equals('importId', $importId));
55
        $result = $query->execute()->toArray();
56
57
        return isset($result[0]) ? $result[0] : null;
58
    }
59
60
    /**
61
     * Return the current tablename.
62
     *
63
     * @return string
64
     */
65
    public function getTableName()
66
    {
67
        $query = $this->createQuery();
68
        if ($query instanceof Query) {
69
            $source = $query->getSource();
70
            if (\method_exists($source, 'getSelectorName')) {
71
                return $source->getSelectorName();
72
            }
73
        }
74
    }
75
76
    /**
77
     * Get the right Index ID by the event ID.
78
     *
79
     * @param int $uid
80
     *
81
     * @return Index|null
82
     */
83
    protected function findNextIndex(int $uid)
84
    {
85
        /** @var Event $event */
86
        $event = $this->findByUid($uid);
87
88
        if (!\is_object($event)) {
89
            return;
90
        }
91
92
        $objectManager = new ObjectManager();
93
        /** @var IndexRepository $indexRepository */
94
        $indexRepository = $objectManager->get(IndexRepository::class);
95
96
        try {
97
            $result = $indexRepository->findByEventTraversing($event, true, false, 1, QueryInterface::ORDER_ASCENDING);
98
            if (empty($result)) {
99
                $result = $indexRepository->findByEventTraversing($event, false, true, 1, QueryInterface::ORDER_DESCENDING);
100
            }
101
        } catch (\Exception $ex) {
102
            return;
103
        }
104
105
        if (empty($result)) {
106
            return;
107
        }
108
109
        /** @var Index $index */
110
        $index = $result[0];
111
112
        return $index;
113
    }
114
}
115