Completed
Push — master ( 56ce82...e6d31a )
by Davide
08:13 queued 11s
created

Country::getCountriesWithTeachers()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 3
c 2
b 0
f 0
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 10
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 25
    public static function getCountries()
32
    {
33 25
        $minutes = 15;
34
        $ret = Cache::remember('countries_list', $minutes, function () {
35 25
            return self::orderBy('name')->pluck('name', 'id');
36 25
        });
37
38 25
        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
        $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
                ->get();
65
        });
66
67
        return $ret;
68
    }
69
70
    /***************************************************************************/
71
72
    /**
73
     * Return the all countries with teachers.
74
     *
75
     * @return \DavideCasiraghi\LaravelEventsCalendar\Models\Country
76
     */
77 3
     public static function getCountriesWithTeachers()
78
     {
79 3
         $ret = self::join('teachers', 'countries.id', '=', 'teachers.country_id')
80 3
                      ->orderBy('countries.name')->pluck('countries.name', 'countries.id');
81
82 3
         return $ret;
83
     }
84
85
    /***************************************************************************/
86
87
88
    /*
89
     * Return active Continent and Countries JSON Tree (for hp select filters, vue component).
90
     *
91
     * @return string
92
     */
93
    /*public static function getActiveCountriesByContinent()
94
    {
95
        $minutes = 15;
96
        $ret = Cache::remember('active_continent_countries_json_tree', $minutes, function () {
97
            return Country::orderBy('name')->pluck('name', 'id');
98
        });
99
100
        return $ret;
101
    }*/
102
}
103