News::getRecords()   B
last analyzed

Complexity

Conditions 6
Paths 7

Size

Total Lines 34

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 34
rs 8.7537
c 0
b 0
f 0
cc 6
nc 7
nop 3
1
<?php
2
3
/**
4
 * Description of Pages
5
 */
6
7
namespace FRUIT\GoogleServices\Service\SitemapProvider;
8
9
use FRUIT\GoogleServices\Controller\SitemapController;
10
use FRUIT\GoogleServices\Domain\Model\Node;
11
use FRUIT\GoogleServices\Service\SitemapProviderInterface;
12
use TYPO3\CMS\Backend\Utility\BackendUtility;
13
use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;
14
use TYPO3\CMS\Core\Utility\MathUtility;
15
16
/**
17
 * Description of Pages
18
 *
19
 * @author timlochmueller
20
 */
21
class News implements SitemapProviderInterface
22
{
23
24
    /**
25
     * Get the Records
26
     *
27
     * @param integer           $startPage
28
     * @param array             $basePages
29
     * @param SitemapController $obj
30
     *
31
     * @throws \Exception
32
     * @return array
33
     */
34
    public function getRecords($startPage, $basePages, SitemapController $obj): array
35
    {
36
        $nodes = [];
37
        if (!ExtensionManagementUtility::isLoaded('tt_news')) {
38
            return $nodes;
39
        }
40
        if (!MathUtility::canBeInterpretedAsInteger($GLOBALS['TSFE']->tmpl->setup['plugin.']['tt_news.']['singlePid'])) {
41
            throw new \Exception('You have to set tt_news singlePid.');
42
        }
43
        $singlePid = intval($GLOBALS['TSFE']->tmpl->setup['plugin.']['tt_news.']['singlePid']);
44
        $news = $this->getRecordsByField('tt_news', 'pid', implode(',', $basePages));
45
        foreach ($news as $record) {
0 ignored issues
show
Bug introduced by
The expression $news of type null|array is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
46
            // Alternative Single PID
47
            $alternativeSinglePid = $this->alternativeSinglePid($record['uid']);
48
            $linkPid = ($alternativeSinglePid) ? $alternativeSinglePid : $singlePid;
49
            // Build URL
50
            $url = $obj->getUriBuilder()
51
                ->setArguments(['tx_ttnews' => ['tt_news' => $record['uid']]])
52
                ->setTargetPageUid($linkPid)
53
                ->build();
54
            // can't generate a valid url
55
            if (!strlen($url)) {
56
                continue;
57
            }
58
            // Build Node
59
            $node = new Node();
60
            $node->setLoc($url);
61
            $node->setPriority($this->getPriority($record));
62
            $node->setChangefreq('monthly');
63
            $node->setLastmod($this->getModifiedDate($record));
64
            $nodes[] = $node;
65
        }
66
        return $nodes;
67
    }
68
69
    /**
70
     * Get the Categories single page ID
71
     *
72
     * @param $newsId
73
     *
74
     * @return bool|int
75
     */
76
    protected function alternativeSinglePid($newsId)
77
    {
78
        $database = $this->getDatabaseConnection();
79
        $rows = $database->exec_SELECTgetRows(
80
            'tt_news_cat.single_pid',
81
            'tt_news_cat, tt_news_cat_mm',
82
            'tt_news_cat_mm.uid_local=' . intval($newsId) . ' AND tt_news_cat_mm.uid_foreign=tt_news_cat.uid',
83
            '',
84
            'tt_news_cat_mm.sorting'
85
        );
86
        foreach ($rows as $row) {
0 ignored issues
show
Bug introduced by
The expression $rows of type null|array is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
87
            if (intval($row['single_pid']) > 0) {
88
                return intval($row['single_pid']);
89
            }
90
        }
91
        return false;
92
    }
93
94
    /**
95
     * Based on t3lib_Befunc::getRecordsByField
96
     *
97
     * @param string $theTable
98
     * @param string $theField
99
     * @param string $theValue
100
     * @param string $whereClause
101
     * @param string $groupBy
102
     * @param string $orderBy
103
     * @param string $limit
104
     * @param bool   $useDeleteClause
105
     *
106
     * @return array
107
     */
108
    public function getRecordsByField(
109
        $theTable,
110
        $theField,
111
        $theValue,
112
        $whereClause = '',
113
        $groupBy = '',
114
        $orderBy = '',
115
        $limit = '',
116
        $useDeleteClause = true
117
    ) {
118
        if (is_array($GLOBALS['TCA'][$theTable])) {
119
            $database = $this->getDatabaseConnection();
120
            return $database->exec_SELECTgetRows(
121
                '*',
122
                $theTable,
123
                $theField . ' IN (' . $theValue . ')' . ($useDeleteClause ? BackendUtility::deleteClause($theTable) . ' ' : '') . BackendUtility::versioningPlaceholderClause($theTable) . ' ' . $whereClause,
124
                // whereClauseMightContainGroupOrderBy
125
                $groupBy,
126
                $orderBy,
127
                $limit
128
            );
129
        }
130
        return [];
131
    }
132
133
    /**
134
     * Get the priority
135
     *
136
     * @param array $record
137
     *
138
     * @internal param int $startPage
139
     * @return float
140
     */
141
    protected function getPriority($record)
142
    {
143
        $prio = 0.9;
144
        if ($record['archivedate'] > 0 && $record['archivedate'] < time()) {
145
            $prio = 0.8;
146
        }
147
        return $prio;
148
    }
149
150
    /**
151
     * get the modifiedDate
152
     *
153
     * @param array $record
154
     *
155
     * @return integer
156
     */
157
    protected function getModifiedDate($record)
158
    {
159
        // Last mod
160
        $lastMod = $record['crdate'];
161
        if ($record['tstamp'] > $lastMod) {
162
            $lastMod = $record['tstamp'];
163
        }
164
        return $lastMod;
165
    }
166
167
    /**
168
     * Get the database connection
169
     *
170
     * @return \TYPO3\CMS\Core\Database\DatabaseConnection
171
     */
172
    protected function getDatabaseConnection()
173
    {
174
        return $GLOBALS['TYPO3_DB'];
175
    }
176
}
177