Completed
Push — master ( 77c6d7...bc20b8 )
by Tim
14:23
created

News::getRecordsByField()   B

Complexity

Conditions 3
Paths 2

Size

Total Lines 24
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 24
rs 8.9713
cc 3
eloc 19
nc 2
nop 8

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
/* * *************************************************************
4
 *  Copyright notice
5
 *
6
 *  (c) 2011 Tim Lochmüller
7
 *  			
8
 *  All rights reserved
9
 *
10
 *  This script is part of the TYPO3 project. The TYPO3 project is
11
 *  free software; you can redistribute it and/or modify
12
 *  it under the terms of the GNU General Public License as published by
13
 *  the Free Software Foundation; either version 2 of the License, or
14
 *  (at your option) any later version.
15
 *
16
 *  The GNU General Public License can be found at
17
 *  http://www.gnu.org/copyleft/gpl.html.
18
 *
19
 *  This script is distributed in the hope that it will be useful,
20
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
21
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22
 *  GNU General Public License for more details.
23
 *
24
 *  This copyright notice MUST APPEAR in all copies of the script!
25
 * ************************************************************* */
26
27
namespace FRUIT\GoogleServices\Service\SitemapProvider;
28
29
use FRUIT\GoogleServices\Controller\SitemapController;
30
use FRUIT\GoogleServices\Domain\Model\Node;
31
use FRUIT\GoogleServices\Service\SitemapProviderInterface;
32
use TYPO3\CMS\Backend\Utility\BackendUtility;
33
use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;
34
use TYPO3\CMS\Core\Utility\MathUtility;
35
36
/**
37
 * Description of Pages
38
 *
39
 * @author timlochmueller
40
 */
41
class News implements SitemapProviderInterface
42
{
43
44
    /**
45
     * Get the Records
46
     *
47
     * @param integer           $startPage
48
     * @param array             $basePages
49
     * @param SitemapController $obj
50
     *
51
     * @throws \Exception
52
     * @return array
53
     */
54
    public function getRecords($startPage, $basePages, SitemapController $obj)
55
    {
56
        $nodes = array();
57
        if (!ExtensionManagementUtility::isLoaded('tt_news')) {
58
            return $nodes;
59
        }
60
        if (!MathUtility::canBeInterpretedAsInteger($GLOBALS['TSFE']->tmpl->setup['plugin.']['tt_news.']['singlePid'])) {
61
            throw new \Exception('You have to set tt_news singlePid.');
62
        }
63
        $singlePid = intval($GLOBALS['TSFE']->tmpl->setup['plugin.']['tt_news.']['singlePid']);
64
        $news = $this->getRecordsByField('tt_news', 'pid', implode(',', $basePages));
65
        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...
66
            // Alternative Single PID
67
            $alternativeSinglePid = $this->alternativeSinglePid($record['uid']);
68
            $linkPid = ($alternativeSinglePid) ? $alternativeSinglePid : $singlePid;
69
            // Build URL
70
            $url = $obj->getUriBuilder()
71
                ->setArguments(array('tx_ttnews' => array('tt_news' => $record['uid'])))
72
                ->setTargetPageUid($linkPid)
73
                ->build();
74
            // can't generate a valid url
75
            if (!strlen($url)) {
76
                continue;
77
            }
78
            // Build Node
79
            $node = new Node();
80
            $node->setLoc($url);
81
            $node->setPriority($this->getPriority($record));
82
            $node->setChangefreq('monthly');
83
            $node->setLastmod($this->getModifiedDate($record));
84
            $nodes[] = $node;
85
        }
86
        return $nodes;
87
    }
88
89
    /**
90
     * Get the Categories single page ID
91
     *
92
     * @param $newsId
93
     *
94
     * @return bool|int
95
     */
96
    protected function alternativeSinglePid($newsId)
97
    {
98
        $database = $this->getDatabaseConnection();
99
        $rows = $database->exec_SELECTgetRows(
100
            'tt_news_cat.single_pid',
101
            'tt_news_cat, tt_news_cat_mm',
102
            'tt_news_cat_mm.uid_local=' . intval($newsId) . ' AND tt_news_cat_mm.uid_foreign=tt_news_cat.uid',
103
            '',
104
            'tt_news_cat_mm.sorting'
105
        );
106
        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...
107
            if (intval($row['single_pid']) > 0) {
108
                return intval($row['single_pid']);
109
            }
110
        }
111
        return false;
112
    }
113
114
    /**
115
     * Based on t3lib_Befunc::getRecordsByField
116
     *
117
     * @param string $theTable
118
     * @param string $theField
119
     * @param string $theValue
120
     * @param string $whereClause
121
     * @param string $groupBy
122
     * @param string $orderBy
123
     * @param string $limit
124
     * @param bool   $useDeleteClause
125
     *
126
     * @return array
127
     */
128
    public function getRecordsByField(
129
        $theTable,
130
        $theField,
131
        $theValue,
132
        $whereClause = '',
133
        $groupBy = '',
134
        $orderBy = '',
135
        $limit = '',
136
        $useDeleteClause = true
137
    ) {
138
        if (is_array($GLOBALS['TCA'][$theTable])) {
139
            $database = $this->getDatabaseConnection();
140
            return $database->exec_SELECTgetRows(
141
                '*',
142
                $theTable,
143
                $theField . ' IN (' . $theValue . ')' . ($useDeleteClause ? BackendUtility::deleteClause($theTable) . ' ' : '') . BackendUtility::versioningPlaceholderClause($theTable) . ' ' . $whereClause,
144
                // whereClauseMightContainGroupOrderBy
145
                $groupBy,
146
                $orderBy,
147
                $limit
148
            );
149
        }
150
        return array();
151
    }
152
153
    /**
154
     * Get the priority
155
     *
156
     * @param array $record
157
     *
158
     * @internal param int $startPage
159
     * @return float
160
     */
161
    protected function getPriority($record)
162
    {
163
        $prio = 0.9;
164
        if ($record['archivedate'] > 0 && $record['archivedate'] < time()) {
165
            $prio = 0.8;
166
        }
167
        return $prio;
168
    }
169
170
    /**
171
     * get the modifiedDate
172
     *
173
     * @param array $record
174
     *
175
     * @return integer
176
     */
177
    protected function getModifiedDate($record)
178
    {
179
        // Last mod
180
        $lastMod = $record['crdate'];
181
        if ($record['tstamp'] > $lastMod) {
182
            $lastMod = $record['tstamp'];
183
        }
184
        return $lastMod;
185
    }
186
187
    /**
188
     * Get the database connection
189
     *
190
     * @return \TYPO3\CMS\Core\Database\DatabaseConnection
191
     */
192
    protected function getDatabaseConnection()
193
    {
194
        return $GLOBALS['TYPO3_DB'];
195
    }
196
}
197