Completed
Push — master ( c48ac2...909c9d )
by Tim
14:12
created

TxNews   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 128
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 4
dl 0
loc 128
c 0
b 0
f 0
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
B getRecords() 0 31 5
B getRecordsByField() 0 24 3
A getPriority() 0 8 3
A getModifiedDate() 0 9 2
A getDatabaseConnection() 0 4 1
1
<?php
2
3
/**
4
 * TxNews
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
 * TxNews
18
 */
19
class TxNews implements SitemapProviderInterface
20
{
21
22
    /**
23
     * Get the Records
24
     *
25
     * @param integer           $startPage
26
     * @param array             $basePages
27
     * @param SitemapController $obj
28
     *
29
     * @throws \Exception
30
     * @return array
31
     */
32
    public function getRecords($startPage, $basePages, SitemapController $obj): array
33
    {
34
        $nodes = [];
35
        if (!ExtensionManagementUtility::isLoaded('news')) {
36
            return $nodes;
37
        }
38
        if (!MathUtility::canBeInterpretedAsInteger($GLOBALS['TSFE']->tmpl->setup['plugin.']['tx_news.']['settings.']['defaultDetailPid'])) {
39
            throw new \Exception('You have to set defaultDetailPid.');
40
        }
41
        $singlePid = intval($GLOBALS['TSFE']->tmpl->setup['plugin.']['tx_news.']['settings.']['defaultDetailPid']);
42
        $news = $this->getRecordsByField('tx_news_domain_model_news', 'pid', implode(',', $basePages));
43
        foreach ($news as $record) {
44
            // Build URL
45
            $url = $obj->getUriBuilder()
46
                ->setArguments(['tx_news_pi1' => ['news' => $record['uid']]])
47
                ->setTargetPageUid($singlePid)
48
                ->build();
49
            // can't generate a valid url
50
            if (!strlen($url)) {
51
                continue;
52
            }
53
            // Build Node
54
            $node = new Node();
55
            $node->setLoc($url);
56
            $node->setPriority($this->getPriority($record));
57
            $node->setChangefreq('monthly');
58
            $node->setLastmod($this->getModifiedDate($record));
59
            $nodes[] = $node;
60
        }
61
        return $nodes;
62
    }
63
64
    /**
65
     * Based on t3lib_Befunc::getRecordsByField
66
     *
67
     * @param string $theTable
68
     * @param string $theField
69
     * @param string $theValue
70
     * @param string $whereClause
71
     * @param string $groupBy
72
     * @param string $orderBy
73
     * @param string $limit
74
     * @param bool   $useDeleteClause
75
     *
76
     * @return array
77
     */
78
    public function getRecordsByField(
79
        $theTable,
80
        $theField,
81
        $theValue,
82
        $whereClause = '',
83
        $groupBy = '',
84
        $orderBy = '',
85
        $limit = '',
86
        $useDeleteClause = true
87
    ) {
88
        if (is_array($GLOBALS['TCA'][$theTable])) {
89
            $database = $this->getDatabaseConnection();
90
            return $database->exec_SELECTgetRows(
91
                '*',
92
                $theTable,
93
                $theField . ' IN (' . $theValue . ')' . ($useDeleteClause ? BackendUtility::deleteClause($theTable) . ' ' : '') . BackendUtility::versioningPlaceholderClause($theTable) . ' ' . $whereClause,
94
                // whereClauseMightContainGroupOrderBy
95
                $groupBy,
96
                $orderBy,
97
                $limit
98
            );
99
        }
100
        return [];
101
    }
102
103
    /**
104
     * Get the priority
105
     *
106
     * @param array $record
107
     *
108
     * @internal param int $startPage
109
     * @return float
110
     */
111
    protected function getPriority($record)
112
    {
113
        $prio = 0.9;
114
        if ($record['archive'] > 0 && $record['archive'] < time()) {
115
            $prio = 0.8;
116
        }
117
        return $prio;
118
    }
119
120
    /**
121
     * get the modifiedDate
122
     *
123
     * @param array $record
124
     *
125
     * @return integer
126
     */
127
    protected function getModifiedDate($record)
128
    {
129
        // Last mod
130
        $lastMod = $record['crdate'];
131
        if ($record['tstamp'] > $lastMod) {
132
            $lastMod = $record['tstamp'];
133
        }
134
        return $lastMod;
135
    }
136
137
    /**
138
     * Get the database connection
139
     *
140
     * @return \TYPO3\CMS\Core\Database\DatabaseConnection
141
     */
142
    protected function getDatabaseConnection()
143
    {
144
        return $GLOBALS['TYPO3_DB'];
145
    }
146
}
147