Completed
Push — master ( ba992d...dc755b )
by Davide
07:03
created

EventRepetition   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
wmc 2
lcom 0
cbo 1
dl 0
loc 46
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A user() 0 4 1
A getLastestEventsRepetitionsQuery() 0 14 1
1
<?php
2
3
namespace App;
4
5
use Illuminate\Database\Eloquent\Model;
6
7
class EventRepetition extends Model
8
{
9
    /***************************************************************************/
10
    /**
11
     * The table associated with the model.
12
     *
13
     * @var string
14
     */
15
    protected $table = 'event_repetitions';
16
17
    /***************************************************************************/
18
19
    protected $fillable = [
20
        'event_id', 'start_repeat', 'end_repeat',
21
    ];
22
23
    public function user()
24
    {
25
        return $this->belongsTo('App\Event', 'event_id', 'id');
26
    }
27
28
    /***************************************************************************/
29
30
    /**
31
     * Get for each event the first event repetition in the near future (JUST THE QUERY to use as SUBQUERY).
32
     * Parameters are Start date and End date of the interval
33
     * Return the query string,.
34
     * @param  string $searchStartDate
35
     * @param  string $searchEndDate
36
     * @return string
37
     */
38
    public static function getLastestEventsRepetitionsQuery($searchStartDate, $searchEndDate)
39
    {
40
        $ret = self::
41
                     selectRaw('event_id, MIN(id) AS rp_id, start_repeat, end_repeat')
42
                     ->when($searchStartDate, function ($query, $searchStartDate) {
43
                         return $query->where('event_repetitions.start_repeat', '>=', $searchStartDate);
44
                     })
45
                     ->when($searchEndDate, function ($query, $searchEndDate) {
46
                         return $query->where('event_repetitions.end_repeat', '<=', $searchEndDate);
47
                     })
48
                     ->groupBy('event_id');
49
50
        return $ret;
51
    }
52
}
53