Completed
Push — master ( e12848...97e1ec )
by John
21s queued 12s
created

Pastebin::user()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace App\Models\Eloquent\Tool;
4
5
use Illuminate\Database\Eloquent\Model;
6
use Illuminate\Support\Facades\DB;
7
8
class Pastebin extends Model
9
{
10
    protected $table = 'pastebin';
11
    protected $primaryKey = 'pbid';
12
13
    protected $fillable=[
14
        'lang', 'title', 'user_id', 'expired_at', 'content', 'code'
15
    ];
16
17
    public function user()
18
    {
19
        return $this->belongsTo('App\Models\Eloquent\UserModel', 'user_id');
20
    }
21
22
    public static function generatePbCode($length=6)
23
    {
24
        $chars='abcdefghjkmnpqrstuvwxyzABCDEFGHJKMNPQRSTUVWXYZ23456789';
25
26
        $code='';
27
        for ($i=0; $i<$length; $i++) {
28
            $code.=$chars[mt_rand(0, strlen($chars)-1)];
29
        }
30
        return $code;
31
    }
32
33
    public static function generate($all_data)
34
    {
35
        $lang=$all_data["syntax"];
36
        $expire=intval($all_data["expiration"]);
37
        $content=$all_data["content"];
38
        $title=$all_data["title"];
39
        $user_id=$all_data["uid"];
40
41
        if ($expire==0) {
42
            $expire_time=null;
43
        } elseif ($expire==1) {
44
            $expire_time=date("Y-m-d H:i:s", strtotime('+1 days'));
45
        } elseif ($expire==7) {
46
            $expire_time=date("Y-m-d H:i:s", strtotime('+7 days'));
47
        } elseif ($expire==30) {
48
            $expire_time=date("Y-m-d H:i:s", strtotime('+30 days'));
49
        }
50
51
        $code=self::generatePbCode(6);
52
        $ret=self::where('code', $code)->first();
53
        if (is_null($ret)) {
54
            self::create([
55
                'lang' => $lang,
56
                'expired_at' => $expire_time,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $expire_time does not seem to be defined for all execution paths leading up to this point.
Loading history...
57
                'user_id' => $user_id,
58
                'title' => $title,
59
                'content' => $content,
60
                'code' => $code,
61
                'created_at' => date("Y-m-d H:i:s"),
62
            ])->save();
63
            return $code;
64
        } else {
65
            return null;
66
        }
67
    }
68
}
69