Sitemap::getRecords()   C
last analyzed

Complexity

Conditions 11
Paths 10

Size

Total Lines 57

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 57
rs 6.7915
c 0
b 0
f 0
cc 11
nc 10
nop 3

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
/**
4
 * Sitemap Provider
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\GeneralUtility;
14
use TYPO3\CMS\Frontend\Page\PageRepository;
15
16
/**
17
 * Sitemap Provider
18
 *
19
 * @author timlochmueller
20
 */
21
class Sitemap implements SitemapProviderInterface
22
{
23
24
    /**
25
     * Get the Records
26
     *
27
     * @param integer $startPage
28
     * @param array $basePages
29
     * @param SitemapController $obj
30
     *
31
     * @return array
32
     */
33
    public function getRecords($startPage, $basePages, SitemapController $obj): array
34
    {
35
        $nodes = [];
36
        $database = $this->getDatabaseConnection();
37
        /** @var PageRepository $pageRepository */
38
        $pageRepository = GeneralUtility::makeInstance(PageRepository::class);
39
        $rows = $database->exec_SELECTgetRows('*', 'tt_content', 'CType=' . $database->fullQuoteStr(
40
            'list',
41
            'tt_content'
42
        ) . ' AND list_type=' . $database->fullQuoteStr(
43
            'googleservices_pisitemap',
44
            'tt_content'
45
        ) . $pageRepository->enableFields('tt_content'));
46
47
        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...
48
            $uid = $row['pid'];
49
            if ($uid == $GLOBALS['TSFE']->id) {
50
                continue;
51
            }
52
53
            // Build URL
54
            $url = $obj->getUriBuilder()
55
                ->setTargetPageUid($uid)
56
                ->build();
57
58
            // can't generate a valid url
59
            if (!strlen($url)) {
60
                continue;
61
            }
62
63
            // Get Record
64
            $record = BackendUtility::getRecord('pages', $uid);
65
66
            // Check FE Access
67
            if ($record['fe_group'] != 0 || $record['no_search'] != 0) {
68
                continue;
69
            }
70
            $rootLineList = $GLOBALS['TSFE']->sys_page->getRootLine($record['uid']);
71
            $addToNode = true;
72
            foreach ($rootLineList as $rootPage) {
73
                if ($rootPage['extendToSubpages'] == 1 && ($rootPage['fe_group'] != 0 || $record['no_search'] != 0)) {
74
                    $addToNode = false;
75
                    break;
76
                }
77
            }
78
            if ($addToNode === false) {
79
                continue;
80
            }
81
82
            // Build Node
83
            $node = new Node();
84
            $node->setLoc($url);
85
            $node->setLastmod($this->getModifiedDate($record));
86
            $nodes[] = $node;
87
        }
88
        return $nodes;
89
    }
90
91
    /**
92
     * get the modifiedDate
93
     *
94
     * @param array $record
95
     *
96
     * @return integer
97
     */
98
    protected function getModifiedDate($record)
99
    {
100
        // Last mod
101
        $lastMod = $record['crdate'];
102
        if ($record['tstamp'] > $lastMod) {
103
            $lastMod = $record['tstamp'];
104
        }
105
        if ($record['SYS_LASTCHANGED'] > $lastMod) {
106
            $lastMod = $record['SYS_LASTCHANGED'];
107
        }
108
        return $lastMod;
109
    }
110
111
    /**
112
     * Get the database connection
113
     *
114
     * @return \TYPO3\CMS\Core\Database\DatabaseConnection
115
     */
116
    protected function getDatabaseConnection()
117
    {
118
        return $GLOBALS['TYPO3_DB'];
119
    }
120
}
121