Completed
Push — master ( fe1366...05ac62 )
by Davide
08:29
created

Country::getCountries()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 1
Metric Value
eloc 4
dl 0
loc 7
ccs 5
cts 5
cp 1
rs 10
c 2
b 0
f 1
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
namespace DavideCasiraghi\LaravelEventsCalendar\Models;
4
5
use Illuminate\Support\Facades\Cache;
6
use Illuminate\Database\Eloquent\Model;
7
8
class Country extends Model
9
{
10
    /***************************************************************************/
11
    /**
12
     * The table associated with the model.
13
     *
14
     * @var string
15
     */
16
    protected $table = 'countries';
17
18
    /***************************************************************************/
19
20
    protected $fillable = [
21
        'name', 'code', 'continent_id',
22
    ];
23
24
    /***************************************************************************/
25
26
    /**
27
     * Return all the countries ordered by name.
28
     *
29
     * @return \DavideCasiraghi\LaravelEventsCalendar\Models\Country
30
     */
31 30
    public static function getCountries()
32
    {
33 30
        $minutes = 15;
34
        $ret = Cache::remember('countries_list', $minutes, function () {
35 30
            return self::orderBy('name')->pluck('name', 'id');
36 30
        });
37 30
        return $ret;
38
    }
39
40
    /***************************************************************************/
41
42
    /**
43
     * Return the all countries with active events.
44
     *
45
     * @return \DavideCasiraghi\LaravelEventsCalendar\Models\Country
46
     */
47
    public static function getActiveCountries()
48
    {
49
        $cacheExpireMinutes = 15; // Set the duration time of the cache
50
        
51
        // All the countries
52
        $ret = Cache::remember('active_countries', $cacheExpireMinutes, function () {
53
            date_default_timezone_set('Europe/Rome');
54
            $searchStartDate = date('Y-m-d', time());
55
            $lastestEventsRepetitionsQuery = EventRepetition::getLastestEventsRepetitionsQuery($searchStartDate, null);
56
57
            return self::
58
            select('countries.*')
59
                ->join('event_venues', 'countries.id', '=', 'event_venues.country_id')
60
                ->join('events', 'event_venues.id', '=', 'events.venue_id')
61
                ->joinSub($lastestEventsRepetitionsQuery, 'event_repetitions', function ($join) {
62
                    $join->on('events.id', '=', 'event_repetitions.event_id');
63
                })
64
                ->orderBy('countries.name')
65
                ->get();
66
        });
67
        
68
        return $ret;
69
    }
70
    
71
    /***************************************************************************/
72
73
    /**
74
     * Return the all active countries by continent 
75
     *
76
     * @return \DavideCasiraghi\LaravelEventsCalendar\Models\Country
77
     */
78
    public static function getActiveCountriesByContinent($continent_id)
79
    {
80
        $activeCountries = $this->getActiveCountries();
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using $this inside a static method is generally not recommended and can lead to errors in newer PHP versions.
Loading history...
81
        $ret = $activeCountries->where('continent_id', $continent_id)->orderBy('name')->get();
82
        
83
        return $ret;
84
    }
85
86
    /***************************************************************************/
87
88
    /**
89
     * Return the all countries with teachers.
90
     *
91
     * @return \DavideCasiraghi\LaravelEventsCalendar\Models\Country
92
     */
93 3
    public static function getCountriesWithTeachers()
94
    {
95 3
        $ret = self::join('teachers', 'countries.id', '=', 'teachers.country_id')
96 3
                      ->orderBy('countries.name')->pluck('countries.name', 'countries.id');
97
98 3
        return $ret;
99
    }
100
101
    /***************************************************************************/
102
103
    /*
104
     * Return active Continent and Countries JSON Tree (for hp select filters, vue component).
105
     *
106
     * @return string
107
     */
108
    /*public static function getActiveCountriesByContinent()
109
    {
110
        $minutes = 15;
111
        $ret = Cache::remember('active_continent_countries_json_tree', $minutes, function () {
112
            return Country::orderBy('name')->pluck('name', 'id');
113
        });
114
115
        return $ret;
116
    }*/
117
}
118