Completed
Push — master ( 3d72e1...92457e )
by Tomas Norre
03:26
created

GolfCourseRepository::persistAll()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 7
rs 9.4285
cc 1
eloc 3
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
35
/**
36
 * The repository for GolfCourses
37
 */
38
class GolfCourseRepository extends \TYPO3\CMS\Extbase\Persistence\Repository
39
{
40
41
    /**
42
     * @param $countryUid
43
     *
44
     * @return array|\TYPO3\CMS\Extbase\Persistence\QueryResultInterface
45
     */
46
    public function findAllInCountry($countryUid)
47
    {
48
        /** @var QueryInterface $query */
49
        $query = $this->createQuery();
50
51
        $querySettings = $query->getQuerySettings();
52
        $querySettings->setRespectStoragePage(false);
53
        $querySettings->setIgnoreEnableFields(false);
54
55
        $query->matching($query->equals('country', $countryUid));
56
        $query->setOrderings(['name' => 'ASC']);
57
        $query->setQuerySettings($querySettings);
58
59
        return $query->execute();
60
    }
61
62
    /**
63
     * @return array
64
     */
65
    public function findCountriesUidsInUse()
66
    {
67
        $uids = [];
68
        $courses = $this->getDatabaseConnection()->exec_SELECTquery(
69
            'DISTINCT country',
70
            'tx_golfcourses_domain_model_golfcourse',
71
            ''
72
        );
73
74
        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...
75
            $uids[] = $course['country'];
76
        }
77
78
        return $uids;
79
    }
80
81
    /**
82
     * @return array|\TYPO3\CMS\Extbase\Persistence\QueryResultInterface
83
     */
84
    public function findCountriesWithOutCoordinates()
85
    {
86
        /** @var QueryInterface $query */
87
        $query = $this->createQuery();
88
        $query->setLimit(5);
89
90
        $querySettings = $query->getQuerySettings();
91
        $querySettings->setRespectStoragePage(false);
92
        $querySettings->setIgnoreEnableFields(false);
93
94
        $query->matching($query->equals('longitude', ''));
95
        $query->matching($query->equals('latitude', ''));
96
        $query->setQuerySettings($querySettings);
97
98
        return $query->execute();
99
    }
100
101
    /**
102
     * Persist All
103
     */
104
    public function persistAll()
105
    {
106
        /** @var PersistenceManager $persistenceManager */
107
        $persistenceManager = GeneralUtility::makeInstance(PersistenceManager::class);
108
        $persistenceManager->persistAll();
109
110
    }
111
112
    /**
113
     * @return DatabaseConnection
114
     */
115
    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...
116
    {
117
        return $GLOBALS['TYPO3_DB'];
118
    }
119
}
120