Completed
Push — master ( 1597c5...d65544 )
by Tomas Norre
03:02
created

GolfCourseRepository::findAllActive()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 11
rs 9.4285
cc 1
eloc 7
nc 1
nop 0
1
<?php
2
namespace TNM\GolfCourses\Domain\Repository;
3
4
/***************************************************************
5
 *
6
 *  Copyright notice
7
 *
8
 *  (c) 2016 Tomas Norre Mikkelsen <[email protected]>
9
 *
10
 *  All rights reserved
11
 *
12
 *  This script is part of the TYPO3 project. The TYPO3 project is
13
 *  free software; you can redistribute it and/or modify
14
 *  it under the terms of the GNU General Public License as published by
15
 *  the Free Software Foundation; either version 3 of the License, or
16
 *  (at your option) any later version.
17
 *
18
 *  The GNU General Public License can be found at
19
 *  http://www.gnu.org/copyleft/gpl.html.
20
 *
21
 *  This script is distributed in the hope that it will be useful,
22
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
23
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24
 *  GNU General Public License for more details.
25
 *
26
 *  This copyright notice MUST APPEAR in all copies of the script!
27
 ***************************************************************/
28
29
use TYPO3\CMS\Core\Database\DatabaseConnection;
30
use TYPO3\CMS\Core\Utility\GeneralUtility;
31
use TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager;
32
use TYPO3\CMS\Extbase\Persistence\PersistenceManagerInterface;
33
use TYPO3\CMS\Extbase\Persistence\QueryInterface;
34
use TYPO3\CMS\Extbase\Persistence\Repository;
35
36
/**
37
 * The repository for GolfCourses
38
 */
39
class GolfCourseRepository extends Repository
40
{
41
42
    /**
43
     * @param $countryUid
44
     *
45
     * @return array|\TYPO3\CMS\Extbase\Persistence\QueryResultInterface
46
     */
47
    public function findAllInCountry($countryUid)
48
    {
49
        /** @var QueryInterface $query */
50
        $query = $this->createQuery();
51
52
        $querySettings = $query->getQuerySettings();
53
        $querySettings->setRespectStoragePage(false);
54
        $querySettings->setIgnoreEnableFields(false);
55
56
        $query->matching($query->equals('country', $countryUid));
57
        $query->setOrderings(['name' => 'ASC']);
58
        $query->setQuerySettings($querySettings);
59
60
        return $query->execute();
61
    }
62
63
    /**
64
     * @return array
65
     */
66
    public function findCountriesUidsInUse()
67
    {
68
        $uids = [];
69
        $courses = $this->getDatabaseConnection()->exec_SELECTquery(
70
            'DISTINCT country',
71
            'tx_golfcourses_domain_model_golfcourse',
72
            ''
73
        );
74
75
        foreach ($courses as $course) {
0 ignored issues
show
Bug introduced by
The expression $courses of type boolean|object<mysqli_result> 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...
76
            $uids[] = $course['country'];
77
        }
78
79
        return $uids;
80
    }
81
82
    /**
83
     * @return array|\TYPO3\CMS\Extbase\Persistence\QueryResultInterface
84
     */
85
    public function findCountriesWithOutCoordinates()
86
    {
87
        /** @var QueryInterface $query */
88
        $query = $this->createQuery();
89
        $query->setLimit(5);
90
91
        $querySettings = $query->getQuerySettings();
92
        $querySettings->setRespectStoragePage(false);
93
        $querySettings->setIgnoreEnableFields(false);
94
95
        $query->matching($query->equals('longitude', ''));
96
        $query->matching($query->equals('latitude', ''));
97
        $query->setQuerySettings($querySettings);
98
99
        return $query->execute();
100
    }
101
102
    /**
103
     * @return array|\TYPO3\CMS\Extbase\Persistence\QueryResultInterface
104
     */
105
    public function findAllActive()
106
    {
107
        $query = $this->createQuery();
108
109
        $querySettings = $query->getQuerySettings();
110
        $querySettings->setRespectStoragePage(false);
111
        $querySettings->setIgnoreEnableFields(true);
112
        $query->setQuerySettings($querySettings);
113
114
        return $query->execute();
115
    }
116
117
    /**
118
     * Persist All
119
     */
120
    public function persistAll()
121
    {
122
        /** @var PersistenceManager $persistenceManager */
123
        $persistenceManager = GeneralUtility::makeInstance(PersistenceManager::class);
124
        $persistenceManager->persistAll();
125
126
    }
127
128
    /**
129
     * @return DatabaseConnection
130
     */
131
    protected function getDatabaseConnection()
0 ignored issues
show
Coding Style introduced by
getDatabaseConnection uses the super-global variable $GLOBALS which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
132
    {
133
        return $GLOBALS['TYPO3_DB'];
134
    }
135
}
136