TagRepository   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 111
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 1

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 11
c 4
b 0
f 0
lcom 2
cbo 1
dl 0
loc 111
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A createQuery() 0 7 1
C findByConfiguration() 0 37 7
A findRandom() 0 11 1
A findByUids() 0 6 1
A getTableName() 0 7 1
1
<?php
2
3
/**
4
 * Tag repository
5
 */
6
7
namespace HDNET\Tagger\Domain\Repository;
8
9
use TYPO3\CMS\Core\Utility\GeneralUtility;
10
use TYPO3\CMS\Extbase\Persistence\Generic\Query;
11
use TYPO3\CMS\Extbase\Persistence\Repository;
12
13
/**
14
 * TagRepository
15
 */
16
class TagRepository extends Repository
17
{
18
19
    /**
20
     * Returns a query for objects of this repository
21
     *
22
     * @return \TYPO3\CMS\Extbase\Persistence\QueryInterface
23
     */
24
    public function createQuery()
25
    {
26
        $query = parent::createQuery();
27
        $query->getQuerySettings()
28
            ->setRespectStoragePage(false);
29
        return $query;
30
    }
31
32
    /**
33
     * Select the Tags by the given configuration
34
     *
35
     * @param array|string $relations
36
     * @param string       $sorting
37
     * @param string       $ordering
38
     * @param integer      $amount
39
     *
40
     * @return \TYPO3\CMS\Extbase\Persistence\QueryResultInterface|array
41
     * @todo move valudation to a pseudo value
42
     */
43
    public function findByConfiguration($relations, $sorting, $ordering, $amount)
44
    {
45
        if (!is_array($relations)) {
46
            $relations = GeneralUtility::trimExplode(',', $relations, true);
47
        }
48
49
        switch ($sorting) {
50
            case 'alphabethicaly':
51
                $sorting = 'tx_tagger_domain_model_tag.title';
52
                break;
53
            case 'random':
54
                $sorting = 'RAND()';
55
                break;
56
            case 'weight':
57
            default:
58
                $sorting = 'valuation';
59
                break;
60
        }
61
62
        // Prepare relations
63
        foreach ($relations as $key => $value) {
64
            $relations[$key] = '"' . $value . '"';
65
        }
66
67
        /** @var Query $query */
68
        $query = $this->createQuery();
69
        $where = (sizeof($relations) ? 'tx_tagger_tag_mm.tablenames IN (' . implode(
70
            ',',
71
            $relations
72
        ) . ') AND ' : '') . "tx_tagger_domain_model_tag.uid = tx_tagger_tag_mm.uid_local";
73
        $plainQuery = "SELECT tx_tagger_domain_model_tag.*, (tx_tagger_domain_model_tag.valuation*COUNT( slug )) as valuation, COUNT( slug ) as content
74
					FROM 
75
						tx_tagger_domain_model_tag, tx_tagger_tag_mm
76
					WHERE " . $where . " GROUP BY slug ORDER BY  " . $sorting . " " . $ordering . " LIMIT " . intval($amount);
77
        return $query->statement($plainQuery)
78
            ->execute();
79
    }
80
81
    /**
82
     * Get Objects by random
83
     *
84
     * @param int $count
85
     *
86
     * @return \TYPO3\CMS\Extbase\Persistence\QueryResultInterface|array
87
     */
88
    public function findRandom($count = 1)
89
    {
90
        $rows = $this->createQuery()
91
            ->execute()
92
            ->count();
93
        $row_number = mt_rand(0, max(0, ($rows - $count)));
94
        return $this->createQuery()
95
            ->setOffset($row_number)
96
            ->setLimit($count)
97
            ->execute();
98
    }
99
100
    /**
101
     * Get Objects by uids
102
     *
103
     * @param array $ids
104
     *
105
     * @return \TYPO3\CMS\Extbase\Persistence\QueryResultInterface|array
106
     */
107
    public function findByUids($ids)
108
    {
109
        $query = $this->createQuery();
110
        $query->matching($query->in('uid', $ids));
111
        return $query->execute();
112
    }
113
114
    /**
115
     * Return the current tablename
116
     *
117
     * @return string
118
     */
119
    protected function getTableName()
120
    {
121
        return $this->persistenceManager->getBackend()
0 ignored issues
show
Bug introduced by
The method getBackend() does not seem to exist on object<TYPO3\CMS\Extbase...stenceManagerInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
122
            ->getDataMapper()
123
            ->getDataMap(get_class($this))
124
            ->getTableName();
125
    }
126
}
127