Country   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 124
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 14
Bugs 6 Features 2
Metric Value
wmc 5
eloc 31
c 14
b 6
f 2
dl 0
loc 124
ccs 31
cts 31
cp 1
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getCountriesWithTeachers() 0 6 1
A getCountryName() 0 6 1
A getActiveCountries() 0 22 1
A getActiveCountriesByContinent() 0 6 1
A getCountries() 0 8 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 iterable
30
     */
31 17
    public static function getCountries(): iterable
32
    {
33 17
        $seconds = 86400; // One day
34
        $ret = Cache::remember('countries_list', $seconds, function () {
35 17
            return self::orderBy('name')->pluck('name', 'id');
36 17
        });
37
38 17
        return $ret;
39
    }
40
41
    /***************************************************************************/
42
43
    /**
44
     * Return the all countries with active events.
45
     *
46
     * @return iterable
47
     */
48 3
    public static function getActiveCountries(): ?iterable
49
    {
50 3
        $seconds = 900; // 15 minutes
51
52
        // All the countries
53
        $ret = Cache::remember('active_countries', $seconds, function () {
54 3
            date_default_timezone_set('Europe/Rome');
55 3
            $searchStartDate = date('Y-m-d', time());
56 3
            $lastestEventsRepetitionsQuery = EventRepetition::getLastestEventsRepetitionsQuery($searchStartDate, null);
57
58
            return self::
59 3
            select('countries.*')
60 3
                ->join('event_venues', 'countries.id', '=', 'event_venues.country_id')
61 3
                ->join('events', 'event_venues.id', '=', 'events.venue_id')
62
                ->joinSub($lastestEventsRepetitionsQuery, 'event_repetitions', function ($join) {
63 3
                    $join->on('events.id', '=', 'event_repetitions.event_id');
64 3
                })
65 3
                ->orderBy('countries.name')
66 3
                ->get();
67 3
        });
68
69 3
        return $ret;
70
    }
71
72
    /***************************************************************************/
73
74
    /**
75
     * Return the all active countries by continent.
76
     * @param int $continent_id
77
     * @return iterable
78
     */
79 2
    public static function getActiveCountriesByContinent($continent_id): ?iterable
80
    {
81 2
        $activeCountries = self::getActiveCountries()->unique('name')->sortBy('name');
82 2
        $ret = $activeCountries->where('continent_id', $continent_id);
83
84 2
        return $ret;
85
    }
86
87
    /***************************************************************************/
88
89
    /**
90
     * Return the all countries with teachers.
91
     *
92
     * @return \Illuminate\Support\Collection
93
     */
94 3
    public static function getCountriesWithTeachers(): ?iterable
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 5
    public static function getCountryName($countryId): string
127
    {
128 5
        $country = self::select('name')->where('id', $countryId)->get();
129 5
        $ret = $country[0]['name'];
130
131 5
        return $ret;
132
    }
133
}
134