Passed
Branch dev (ead432)
by John
04:24
created

ShareModel   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 29
c 1
b 0
f 0
dl 0
loc 48
rs 10
wmc 8

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A share() 0 11 4
A sharePB() 0 22 3
1
<?php
2
3
namespace App\Models\Submission;
4
5
use Illuminate\Database\Eloquent\Model;
6
use Illuminate\Support\Facades\DB;
7
use App\Models\Tool\PastebinModel;
8
9
class ShareModel extends Model
10
{
11
    protected $tableName="submission";
12
    protected $extractModels=[
13
        "SubmissionModel"=>null
14
    ];
15
16
    public function __construct($submissionModel)
17
    {
18
        $this->extractModels["SubmissionModel"]=$submissionModel;
19
    }
20
21
    public function share($sid, $uid)
22
    {
23
        $basic=DB::table($this->tableName)->where(['sid'=>$sid, 'uid'=>$uid])->first();
24
        if (empty($basic)) {
25
            return [];
26
        }
27
        DB::table($this->tableName)->where(['sid'=>$sid])->update([
28
            "share"=>$basic["share"] ? 0 : 1
29
        ]);
30
        return [
31
            "share"=>$basic["share"] ? 0 : 1
32
        ];
33
    }
34
35
    public function sharePB($sid, $uid)
36
    {
37
        $basic=DB::table($this->tableName)->where(['sid'=>$sid, 'uid'=>$uid])->first();
38
        $problem=DB::table("problem")->where(['pid'=>$basic["pid"]])->first();
39
        $compiler=DB::table("compiler")->where(['coid'=>$basic["coid"]])->first();
40
        if (empty($basic)) {
41
            return [];
42
        }
43
        $pastebinModel=new PastebinModel();
44
        $ret=$pastebinModel->generate([
45
            "syntax"=>$compiler["lang"],
46
            "expiration"=>0,
47
            "content"=>$basic["solution"],
48
            "title"=>$problem["pcode"]." - ".$basic["verdict"],
49
            "uid"=>$uid
50
        ]);
51
52
        if (is_null($ret)) {
53
            return [];
54
        } else {
55
            return [
56
                "code" => $ret
57
            ];
58
        }
59
    }
60
}
61