Completed
Push — master ( 6db110...3d72e1 )
by Tomas Norre
16:02
created

findCountriesWithOutCoordinates()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 16
rs 9.4285
cc 1
eloc 8
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\Extbase\Persistence\QueryInterface;
31
32
/**
33
 * The repository for GolfCourses
34
 */
35
class GolfCourseRepository extends \TYPO3\CMS\Extbase\Persistence\Repository
36
{
37
38
    /**
39
     * @param $countryUid
40
     *
41
     * @return array|\TYPO3\CMS\Extbase\Persistence\QueryResultInterface
42
     */
43
    public function findAllInCountry($countryUid)
44
    {
45
        /** @var QueryInterface $query */
46
        $query = $this->createQuery();
47
48
        $querySettings = $query->getQuerySettings();
49
        $querySettings->setRespectStoragePage(false);
50
        $querySettings->setIgnoreEnableFields(false);
51
52
        $query->matching($query->equals('country', $countryUid));
53
        $query->setOrderings(['name' => 'ASC']);
54
        $query->setQuerySettings($querySettings);
55
56
        return $query->execute();
57
    }
58
59
    /**
60
     * @return array
61
     */
62
    public function findCountriesUidsInUse()
63
    {
64
        $uids = [];
65
        $courses = $this->getDatabaseConnection()->exec_SELECTquery(
66
            'DISTINCT country',
67
            'tx_golfcourses_domain_model_golfcourse',
68
            ''
69
        );
70
71
        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...
72
            $uids[] = $course['country'];
73
        }
74
75
        return $uids;
76
    }
77
78
    /**
79
     * @return array|\TYPO3\CMS\Extbase\Persistence\QueryResultInterface
80
     */
81
    public function findCountriesWithOutCoordinates()
82
    {
83
        /** @var QueryInterface $query */
84
        $query = $this->createQuery();
85
        $query->setLimit(3);
86
87
        $querySettings = $query->getQuerySettings();
88
        $querySettings->setRespectStoragePage(false);
89
        $querySettings->setIgnoreEnableFields(false);
90
91
        //$query->matching($query->equals('longitude', ''));
0 ignored issues
show
Unused Code Comprehensibility introduced by
80% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
92
        //$query->matching($query->equals('latitude', ''));
0 ignored issues
show
Unused Code Comprehensibility introduced by
80% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
93
        $query->setQuerySettings($querySettings);
94
95
        return $query->execute();
96
    }
97
98
    /**
99
     * @return DatabaseConnection
100
     */
101
    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...
102
    {
103
        return $GLOBALS['TYPO3_DB'];
104
    }
105
}
106