Completed
Push — master ( da8bf6...ffff83 )
by Tim
27s queued 17s
created

EventRepository::getTableName()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
cc 3
nc 3
nop 0
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\Core\Utility\GeneralUtility;
13
use TYPO3\CMS\Extbase\Object\ObjectManager;
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
     *
48
     * @return mixed|null
49
     */
50
    public function findOneByImportId($importId)
51
    {
52
        $query = $this->createQuery();
53
        $query->getQuerySettings()->setRespectStoragePage(false);
54
        $query->getQuerySettings()->setIgnoreEnableFields(true);
55
        $query->matching($query->equals('importId', $importId));
56
        $result = $query->execute()->toArray();
57
58
        return $result[0] ?? null;
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
        /** @var IndexRepository $indexRepository */
78
        $indexRepository = GeneralUtility::makeInstance(ObjectManager::class)->get(IndexRepository::class);
79
80
        try {
81
            $result = $indexRepository->findByEventTraversing($event, true, false, 1, QueryInterface::ORDER_ASCENDING);
82
            if (empty($result)) {
83
                $result = $indexRepository->findByEventTraversing($event, false, true, 1, QueryInterface::ORDER_DESCENDING);
84
            }
85
        } catch (\Exception $ex) {
86
            return;
87
        }
88
89
        if (empty($result)) {
90
            return;
91
        }
92
93
        /** @var Index $index */
94
        $index = $result[0];
95
96
        return $index;
97
    }
98
}
99