Passed
Push — master ( 5ccd99...5e275f )
by Martin
05:51
created

Ticket::getRowAndSeat()   A

Complexity

Conditions 6
Paths 7

Size

Total Lines 22
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 14
nc 7
nop 0
dl 0
loc 22
rs 9.2222
c 0
b 0
f 0
1
<?php
2
3
namespace App;
4
5
use Illuminate\Database\Eloquent\Model;
6
7
class Ticket extends Model
8
{
9
    //
10
    protected $fillable = ['random_id', 'seat_number', 'purchase_id', 'event_id', 'price_category_id'];
11
12
    /**
13
     * Get the route key for the model.
14
     *
15
     * @return string
16
     */
17
    public function getRouteKeyName()
18
    {
19
        return 'random_id';
20
    }
21
22
    public function price()
23
    {
24
        return $this->priceCategory->price;
25
    }
26
27
    public function purchase()
28
    {
29
        return $this->belongsTo('App\Purchase');
30
    }
31
32
    public function event()
33
    {
34
        return $this->belongsTo('App\Event');
35
    }
36
37
    public function priceCategory()
38
    {
39
        return $this->belongsTo('App\PriceCategory');
40
    }
41
42
    public function getRowAndSeat()
43
    {
44
        if( !$this->event->seatMap->layout ) {
45
            return null;
46
        }
47
48
        $counter = 0;
49
        $result = [];
50
        $rows = json_decode($this->event->seatMap->layout);
51
        foreach( $rows as $rowId => $row ) {
52
            foreach( str_split($row) as $charId => $char) {
53
                if($char === 'a') {
54
                    $counter++;
55
                }
56
                if($counter === $this->seat_number) {
57
                    $result['row'] = $rowId;
58
                    $result['seat'] = $charId;
59
                break;
60
                }
61
            }
62
        }
63
        return $result;
64
    }
65
}
66