Completed
Push — master ( f69c09...7ceaf2 )
by Tim
14:30
created

IndexResult::initializeIndex()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 26
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 26
rs 8.8571
cc 3
eloc 20
nc 3
nop 0
1
<?php
2
/**
3
 * @todo    General file information
4
 *
5
 * @author  Tim Lochmüller
6
 */
7
8
namespace HDNET\CalendarizeNews\Persistence;
9
10
use HDNET\Calendarize\Service\IndexerService;
11
use HDNET\Calendarize\Utility\HelperUtility;
12
use HDNET\CalendarizeNews\Service\NewsOverwrite;
13
use HDNET\CalendarizeNews\Xclass\NewsRepository;
14
use TYPO3\CMS\Core\Database\DatabaseConnection;
15
use TYPO3\CMS\Extbase\Persistence\Generic\QueryResult;
16
17
/**
18
 * @todo General class information
19
 *
20
 */
21
class IndexResult extends QueryResult
22
{
23
24
    /**
25
     * The result of the index selection
26
     *
27
     * @var array
28
     */
29
    protected $indexResult;
30
31
    /**
32
     * Inject Repository
33
     *
34
     * @var \HDNET\Calendarize\Domain\Repository\IndexRepository
35
     * @inject
36
     */
37
    protected $indexRepository;
38
39
    /**
40
     * Loads the objects this QueryResult is supposed to hold
41
     *
42
     * @return void
43
     */
44
    protected function initializeIndex()
45
    {
46
        if (!is_array($this->indexResult)) {
47
            $newsIds = [];
48
            $query = clone $this->query;
49
            $query->setLimit(99999999);
50
            $query->setOffset(0);
51
            $news = $query->execute(true);
52
            foreach ($news as $row) {
53
                $newsIds[] = (int)$row['uid'];
54
            }
55
            $newsIds[] = -1;
56
57
            $database = $this->getDatabaseConnection();
58
            $this->indexResult = $database->exec_SELECTgetRows(
0 ignored issues
show
Documentation Bug introduced by
It seems like $database->exec_SELECTge..., '', 'start_date ASC') can be null. However, the property $indexResult is declared as array. Maybe change the type of the property to array|null or add a type check?

Our type inference engine has found an assignment of a scalar value (like a string, an integer or null) to a property which is an array.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property.

To type hint that a parameter can be either an array or null, you can set a type hint of array and a default value of null. The PHP interpreter will then accept both an array or null for that parameter.

function aContainsB(array $needle = null, array  $haystack) {
    if (!$needle) {
        return false;
    }

    return array_intersect($haystack, $needle) == $haystack;
}

The function can be called with either null or an array for the parameter $needle but will only accept an array as $haystack.

Loading history...
59
                '*',
60
                IndexerService::TABLE_NAME,
61
                'foreign_table = "tx_news_domain_model_news" AND foreign_uid IN (' . implode(
62
                    ',',
63
                    $newsIds
64
                ) . ') AND start_date > ' . time(),
65
                '',
66
                'start_date ASC'
67
            );
68
        }
69
    }
70
71
    /**
72
     * Loads the objects this QueryResult is supposed to hold
73
     *
74
     * @return void
75
     */
76
    protected function initialize()
77
    {
78
        if (!is_array($this->queryResult)) {
79
            $this->initializeIndex();
80
            /** @var NewsOverwrite $overwriteService */
81
            $overwriteService = HelperUtility::create('HDNET\\CalendarizeNews\\Service\\NewsOverwrite');
82
83
            /** @var NewsRepository $newsRepository */
84
            $newsRepository = HelperUtility::create('GeorgRinger\\News\\Domain\\Repository\\NewsRepository');
85
            $selection = array_slice($this->indexResult, (int)$this->query->getOffset(), (int)$this->query->getLimit());
86
            $this->queryResult = [];
87
            foreach ($selection as $item) {
88
                $news = $newsRepository->findByIdentifier((int)$item['foreign_uid']);
89
                if (is_object($news)) {
90
                    $customNews = clone $news;
91
                    $overwriteService->overWriteNewsPropertiesByIndexArray($customNews, $item);
92
                    $this->queryResult[] = $customNews;
93
                }
94
            }
95
        }
96
    }
97
98
    /**
99
     * Returns the first object in the result set
100
     *
101
     * @return object
102
     * @api
103
     */
104
    public function getFirst()
105
    {
106
        $this->initialize();
107
        $queryResult = $this->queryResult;
108
        reset($queryResult);
109
        $firstResult = current($queryResult);
110
        if ($firstResult === false) {
111
            $firstResult = null;
112
        }
113
        return $firstResult;
114
    }
115
116
    /**
117
     * Returns the number of objects in the result
118
     *
119
     * @return integer The number of matching objects
120
     * @api
121
     */
122
    public function count()
123
    {
124
        if ($this->numberOfResults === null) {
125
            $this->initializeIndex();
126
            $this->numberOfResults = count($this->indexResult);
127
        }
128
        return $this->numberOfResults;
129
    }
130
131
    /**
132
     * @return DatabaseConnection
133
     */
134
    protected function getDatabaseConnection()
135
    {
136
        return $GLOBALS['TYPO3_DB'];
137
    }
138
}
139