Completed
Push — development ( c87275...b28f77 )
by Ashutosh
11:48
created

Order::getDomainAttribute()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 10
rs 10
c 0
b 0
f 0
cc 3
nc 4
nop 1
1
<?php
2
3
namespace App\Model\Order;
4
5
use App\BaseModel;
6
use App\Model\Product\Subscription;
7
use Illuminate\Contracts\Encryption\DecryptException;
8
use Spatie\Activitylog\Traits\LogsActivity;
9
10
class Order extends BaseModel
11
{
12
    use LogsActivity;
13
    protected $table = 'orders';
14
    protected static $logName = 'Order';
15
    protected $fillable = ['client', 'order_status', 'invoice_item_id',
16
        'serial_key', 'product', 'domain', 'subscription', 'price_override', 'qty', 'invoice_id', 'number', ];
17
    protected static $logAttributes = ['client', 'order_status', 'invoice_item_id',
18
        'serial_key', 'product', 'domain', 'subscription', 'price_override', 'qty', 'invoice_id', 'number', ];
19
    protected static $logOnlyDirty = true;
20
21
    public function getDescriptionForEvent(string $eventName): string
22
    {
23
        if ($eventName == 'created') {
24
            return 'Order No.  <strong> '.$this->number.' </strong> was created';
25
        }
26
27
        if ($eventName == 'updated') {
28
            return 'Order No. <strong> '.$this->number.'</strong> was updated';
29
        }
30
31
        if ($eventName == 'deleted') {
32
            return 'Order No. <strong> '.$this->number.' </strong> was deleted';
33
        }
34
35
        return '';
36
    }
37
38
    public function invoice()
39
    {
40
        return $this->belongsTo('App\Model\Order\Invoice', 'invoice_id');
41
    }
42
43
    public function user()
44
    {
45
        return $this->belongsTo('App\User', 'client');
46
    }
47
48
    public function subscription()
49
    {
50
        return $this->hasOne('App\Model\Product\Subscription');
51
    }
52
53
    public function productUpload()
54
    {
55
        return $this->hasMany('App\Model\Product\ProductUpload');
56
    }
57
58
    public function product()
59
    {
60
        return $this->belongsTo('App\Model\Product\Product', 'product');
61
    }
62
63
    public function invoiceRelation()
64
    {
65
        return $this->hasMany('App\Model\Order\OrderInvoiceRelation');
66
    }
67
68
    public function invoiceItem()
69
    {
70
        return $this->hasManyThrough('App\Model\Order\InvoiceItem', 'App\Model\Order\Invoice');
71
    }
72
73
    public function item()
74
    {
75
        return $this->belongsTo('App\Model\Order\InvoiceItem');
76
    }
77
78
    public function delete()
79
    {
80
        $this->invoiceRelation()->delete();
81
        $this->subscription()->delete();
82
83
        parent::delete();
84
    }
85
86
    public function getOrderStatusAttribute($value)
87
    {
88
        return ucfirst($value);
89
    }
90
91
    public function getCreatedAtAttribute($value)
92
    {
93
        $tz = \Auth::user()->timezone()->first()->name;
94
        $date = \Carbon\Carbon::createFromFormat('Y-m-d H:i:s', $value);
95
        $d = $date->setTimezone($tz);
96
97
        return $d;
98
    }
99
100
    public function getSerialKeyAttribute($value)
101
    {
102
        try {
103
            $decrypted = \Crypt::decrypt($value);
104
105
            return $decrypted;
106
        } catch (DecryptException $ex) {
107
            return $value;
108
        }
109
    }
110
111
    public function getDomainAttribute($value)
112
    {
113
        try {
114
            if (ends_with($value, '/')) {
115
                $value = substr_replace($value, '', -1, 0);
116
            }
117
118
            return $value;
119
        } catch (DecryptException $ex) {
120
            return $value;
121
        }
122
    }
123
124
    public function setDomainAttribute($value)
125
    {
126
        $this->attributes['domain'] = $this->get_domain($value);
127
    }
128
129
    public function get_domain($url)
130
    {
131
        $pieces = parse_url($url);
132
        $domain = isset($pieces['host']) ? $pieces['host'] : '';
133
        if (preg_match('/(?P<domain>[a-z0-9][a-z0-9\-]{1,63}\.[a-z\.]{2,6})$/i', $domain, $regs)) {
134
            return $regs['domain'];
135
        }
136
        if (!$domain) {
137
            $domain = $pieces['path'];
138
        }
139
140
        return strtolower($domain);
141
    }
142
}
143