Completed
Push — master ( 8a1f93...8358ba )
by Tomas Norre
13:47
created

GolfCourseRepository::getDatabaseConnection()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
namespace TNM\GolfCourses\Domain\Repository;
3
4
5
/***************************************************************
6
 *
7
 *  Copyright notice
8
 *
9
 *  (c) 2016 Tomas Norre Mikkelsen <[email protected]>
10
 *
11
 *  All rights reserved
12
 *
13
 *  This script is part of the TYPO3 project. The TYPO3 project is
14
 *  free software; you can redistribute it and/or modify
15
 *  it under the terms of the GNU General Public License as published by
16
 *  the Free Software Foundation; either version 3 of the License, or
17
 *  (at your option) any later version.
18
 *
19
 *  The GNU General Public License can be found at
20
 *  http://www.gnu.org/copyleft/gpl.html.
21
 *
22
 *  This script is distributed in the hope that it will be useful,
23
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
24
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25
 *  GNU General Public License for more details.
26
 *
27
 *  This copyright notice MUST APPEAR in all copies of the script!
28
 ***************************************************************/
29
30
use TYPO3\CMS\Core\Database\DatabaseConnection;
31
use TYPO3\CMS\Extbase\Persistence\QueryInterface;
32
33
/**
34
 * The repository for GolfCourses
35
 */
36
class GolfCourseRepository extends \TYPO3\CMS\Extbase\Persistence\Repository
37
{
38
39
    /**
40
     * @param $countryUid
41
     *
42
     * @return array|\TYPO3\CMS\Extbase\Persistence\QueryResultInterface
43
     */
44
    public function findAllInCountry($countryUid)
45
    {
46
        /** @var QueryInterface $query */
47
        $query = $this->createQuery();
48
49
        $querySettings = $query->getQuerySettings();
50
        $querySettings->setRespectStoragePage(false);
51
        $querySettings->setIgnoreEnableFields(false);
52
53
        $query->matching($query->equals('country', $countryUid));
54
        $query->setOrderings(['name' => 'ASC']);
55
        $query->setQuerySettings($querySettings);
56
57
        return $query->execute();
58
    }
59
60
    /**
61
     * @return array
62
     */
63
    public function findCountriesUidsInUse()
64
    {
65
        $uids = [];
66
        $courses = $this->getDatabaseConnection()->exec_SELECTquery(
67
            'DISTINCT country',
68
            'tx_golfcourses_domain_model_golfcourse',
69
            ''
70
        );
71
72
        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...
73
            $uids[] = $course['country'];
74
        }
75
76
        return $uids;
77
    }
78
79
    /**
80
     * @return DatabaseConnection
81
     */
82
    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...
83
        return $GLOBALS['TYPO3_DB'];
84
    }
85
}