Passed
Push — master ( ad9ff4...50b954 )
by Davide
75:09 queued 52:43
created

Country::getCountries()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 3
Bugs 1 Features 1
Metric Value
eloc 4
c 3
b 1
f 1
dl 0
loc 8
ccs 5
cts 5
cp 1
rs 10
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
namespace DavideCasiraghi\LaravelEventsCalendar\Models;
4
5
use Illuminate\Database\Eloquent\Model;
6
use Illuminate\Support\Facades\Cache;
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
38 30
        return $ret;
39
    }
40
41
    /***************************************************************************/
42
43
    /**
44
     * Return the all countries with active events.
45
     *
46
     * @return \DavideCasiraghi\LaravelEventsCalendar\Models\Country
47
     */
48
    public static function getActiveCountries()
49
    {
50
        $cacheExpireMinutes = 15; // Set the duration time of the cache
51
52
        // All the countries
53
        $ret = Cache::remember('active_countries', $cacheExpireMinutes, function () {
54
            date_default_timezone_set('Europe/Rome');
55
            $searchStartDate = date('Y-m-d', time());
56
            $lastestEventsRepetitionsQuery = EventRepetition::getLastestEventsRepetitionsQuery($searchStartDate, null);
57
58
            return self::
59
            select('countries.*')
60
                ->join('event_venues', 'countries.id', '=', 'event_venues.country_id')
61
                ->join('events', 'event_venues.id', '=', 'events.venue_id')
62
                ->joinSub($lastestEventsRepetitionsQuery, 'event_repetitions', function ($join) {
63
                    $join->on('events.id', '=', 'event_repetitions.event_id');
64
                })
65
                ->orderBy('countries.name')
66
                ->get();
67
        });
68
69
        return $ret;
70
    }
71
72
    /***************************************************************************/
73
74
    /**
75
     * Return the all active countries by continent.
76
     *
77
     * @return \DavideCasiraghi\LaravelEventsCalendar\Models\Country
78
     */
79
    public static function getActiveCountriesByContinent($continent_id)
80
    {
81
        $activeCountries = self::getActiveCountries()->unique('name')->sortBy('name');
82
        $ret = $activeCountries->where('continent_id', $continent_id);
83
84
        return $ret;
85
    }
86
87
    /***************************************************************************/
88
89
    /**
90
     * Return the all countries with teachers.
91
     *
92
     * @return \DavideCasiraghi\LaravelEventsCalendar\Models\Country
93
     */
94 3
    public static function getCountriesWithTeachers()
95
    {
96 3
        $ret = self::join('teachers', 'countries.id', '=', 'teachers.country_id')
97 3
                      ->orderBy('countries.name')->pluck('countries.name', 'countries.id');
98
99 3
        return $ret;
100
    }
101
102
    /***************************************************************************/
103
104
    /*
105
     * Return active Continent and Countries JSON Tree (for hp select filters, vue component).
106
     *
107
     * @return string
108
     */
109
    /*public static function getActiveCountriesByContinent()
110
    {
111
        $minutes = 15;
112
        $ret = Cache::remember('active_continent_countries_json_tree', $minutes, function () {
113
            return Country::orderBy('name')->pluck('name', 'id');
114
        });
115
116
        return $ret;
117
    }*/
118
119
    /***************************************************************************/
120
121
    /**
122
     * Return the country name.
123
     * @param int $countryId
124
     * @return string
125
     */
126 3
    public static function getCountryName($countryId)
127
    {
128 3
        $country = self::select('name')->where('id', $countryId)->get();
129 3
        $ret = $country[0]['name'];
130
131 3
        return $ret;
132
    }
133
}
134