Completed
Push — master ( 7c2b21...ca3b7a )
by Tim
01:59
created

EventXmlSitemapDataProvider::generateItems()   C

Complexity

Conditions 11
Paths 160

Size

Total Lines 62

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 62
rs 6.2824
c 0
b 0
f 0
cc 11
nc 160
nop 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace HDNET\Calendarize\Seo;
6
7
use Psr\Http\Message\ServerRequestInterface;
8
use TYPO3\CMS\Core\Context\Context;
9
use TYPO3\CMS\Core\Database\ConnectionPool;
10
use TYPO3\CMS\Core\Utility\GeneralUtility;
11
use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;
12
use TYPO3\CMS\Seo\XmlSitemap\AbstractXmlSitemapDataProvider;
13
use TYPO3\CMS\Seo\XmlSitemap\Exception\MissingConfigurationException;
14
15
/**
16
 * EventXmlSitemapDataProvider.
17
 */
18
class EventXmlSitemapDataProvider extends AbstractXmlSitemapDataProvider
19
{
20
    /**
21
     * @param ServerRequestInterface     $request
22
     * @param string                     $key
23
     * @param array                      $config
24
     * @param ContentObjectRenderer|null $cObj
25
     *
26
     * @throws MissingConfigurationException
27
     */
28
    public function __construct(ServerRequestInterface $request, string $key, array $config = [], ContentObjectRenderer $cObj = null)
29
    {
30
        parent::__construct($request, $key, $config, $cObj);
31
32
        $this->generateItems();
33
    }
34
35
    /**
36
     * @throws MissingConfigurationException
37
     */
38
    public function generateItems(): void
39
    {
40
        $table = 'tx_calendarize_domain_model_index';
41
42
        $pids = !empty($this->config['pid']) ? GeneralUtility::intExplode(',', $this->config['pid']) : [];
43
        $lastModifiedField = $this->config['lastModifiedField'] ?? 'tstamp';
44
        $sortField = $this->config['sortField'] ?? 'uid';
45
46
        $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
47
            ->getQueryBuilderForTable($table);
48
49
        $constraints = [];
50
        if (!empty($GLOBALS['TCA'][$table]['ctrl']['languageField'])) {
51
            $constraints[] = $queryBuilder->expr()->in(
52
                $GLOBALS['TCA'][$table]['ctrl']['languageField'],
53
                [
54
                    -1, // All languages
55
                    $this->getLanguageId(),  // Current language
56
                ]
57
            );
58
        }
59
60
        if (!empty($pids)) {
61
            $recursiveLevel = isset($this->config['recursive']) ? (int) $this->config['recursive'] : 0;
62
            if ($recursiveLevel) {
63
                $newList = [];
64
                foreach ($pids as $pid) {
65
                    $list = $this->cObj->getTreeList($pid, $recursiveLevel);
66
                    if ($list) {
67
                        $newList = \array_merge($newList, \explode(',', $list));
68
                    }
69
                }
70
                $pids = \array_merge($pids, $newList);
71
            }
72
73
            $constraints[] = $queryBuilder->expr()->in('pid', $pids);
74
        }
75
76
        if (!empty($this->config['additionalWhere'])) {
77
            $constraints[] = $this->config['additionalWhere'];
78
        }
79
80
        $queryBuilder->select('*')
81
            ->from($table);
82
83
        if (!empty($constraints)) {
84
            $queryBuilder->where(
85
                ...$constraints
86
            );
87
        }
88
89
        $rows = $queryBuilder->orderBy($sortField)
90
            ->execute()
91
            ->fetchAll();
92
93
        foreach ($rows as $row) {
94
            $this->items[] = [
95
                'data' => $row,
96
                'lastMod' => (int) $row[$lastModifiedField],
97
            ];
98
        }
99
    }
100
101
    /**
102
     * @param array $data
103
     *
104
     * @return array
105
     */
106
    protected function defineUrl(array $data): array
107
    {
108
        $pageId = $this->config['url']['pageId'] ?? $GLOBALS['TSFE']->id;
109
110
        $additionalParams = [
111
            'tx_calendarize_calendar' => [
112
                'index' => $data['data']['uid'],
113
            ],
114
        ];
115
        $additionalParams = $this->getUrlFieldParameterMap($additionalParams, $data['data']);
116
        $additionalParams = $this->getUrlAdditionalParams($additionalParams);
117
118
        $additionalParamsString = \http_build_query(
119
            $additionalParams,
120
            '',
121
            '&',
122
            PHP_QUERY_RFC3986
123
        );
124
125
        $typoLinkConfig = [
126
            'parameter' => $pageId,
127
            'additionalParams' => $additionalParamsString ? '&' . $additionalParamsString : '',
128
            'forceAbsoluteUrl' => 1,
129
            'useCacheHash' => $this->config['url']['useCacheHash'] ?? 0,
130
        ];
131
132
        $data['loc'] = $this->cObj->typoLink_URL($typoLinkConfig);
133
134
        return $data;
135
    }
136
137
    /**
138
     * @param array $additionalParams
139
     * @param array $data
140
     *
141
     * @return array
142
     */
143
    protected function getUrlFieldParameterMap(array $additionalParams, array $data): array
144
    {
145
        if (!empty($this->config['url']['fieldToParameterMap']) &&
146
            \is_array($this->config['url']['fieldToParameterMap'])) {
147
            foreach ($this->config['url']['fieldToParameterMap'] as $field => $urlPart) {
148
                $additionalParams[$urlPart] = $data[$field];
149
            }
150
        }
151
152
        return $additionalParams;
153
    }
154
155
    /**
156
     * @param array $additionalParams
157
     *
158
     * @return array
159
     */
160
    protected function getUrlAdditionalParams(array $additionalParams): array
161
    {
162
        if (!empty($this->config['url']['additionalGetParameters']) &&
163
            \is_array($this->config['url']['additionalGetParameters'])) {
164
            foreach ($this->config['url']['additionalGetParameters'] as $extension => $extensionConfig) {
165
                foreach ($extensionConfig as $key => $value) {
166
                    $additionalParams[$extension . '[' . $key . ']'] = $value;
167
                }
168
            }
169
        }
170
171
        return $additionalParams;
172
    }
173
174
    /**
175
     * @throws \TYPO3\CMS\Core\Context\Exception\AspectNotFoundException
176
     *
177
     * @return int
178
     */
179
    protected function getLanguageId(): int
180
    {
181
        $context = GeneralUtility::makeInstance(Context::class);
182
183
        return (int) $context->getPropertyFromAspect('language', 'id');
184
    }
185
}
186