Passed
Push — master ( 49e434...6c0612 )
by
unknown
04:29 queued 10s
created

UVaLive::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 3
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace App\Http\Controllers\VirtualCrawler\UVaLive;
4
5
use App\Http\Controllers\VirtualCrawler\CrawlerBase;
6
use App\Models\ProblemModel;
7
use KubAT\PhpSimple\HtmlDomParser;
8
use Auth;
9
use Requests;
10
use Exception;
11
12
class UVaLive extends CrawlerBase
13
{
14
    public $oid=9;
15
    /**
16
     * Initial
17
     *
18
     * @return Response
0 ignored issues
show
Bug introduced by
The type App\Http\Controllers\Vir...rawler\UVaLive\Response was not found. Did you mean Response? If so, make sure to prefix the type with \.
Loading history...
19
     */
20
    public function __construct($action='crawl_problem', $con='all', $cached=false)
0 ignored issues
show
Unused Code introduced by
The parameter $cached is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

20
    public function __construct($action='crawl_problem', $con='all', /** @scrutinizer ignore-unused */ $cached=false)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
21
    {
22
        set_time_limit(0); // Pandora's box, engage!
23
        if ($action=='judge_level') {
24
            $this->judge_level();
25
        } else {
26
            $this->crawler($con);
27
        }
28
    }
29
30
    public function judge_level()
31
    {
32
        // TODO
33
    }
34
35
36
    public function crawler($con)
37
    {
38
        $problemModel=new ProblemModel();
39
        if ($con=='all') {
40
            $res=Requests::get("https://icpcarchive.ecs.baylor.edu/uhunt/api/p", [], ['timeout'=>600]);
41
            $result=json_decode($res->body, true);
42
            $info=[];
43
            for ($i=0; $i<count($result); ++$i) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
44
                $info[$result[$i][1]]=[$result[$i][0], $result[$i][2], $result[$i][3], $result[$i][19]];
45
            }
46
            ksort($info);
47
            foreach ($info as $key=>$value) {
48
                $this->pro['pcode']='UvaL'.$key;
49
                $this->pro['OJ']=$this->oid;
50
                $this->pro['contest_id']=null;
51
                $this->pro['index_id']=$value[0];
52
                $this->pro['origin']="https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=".$value[0];
53
                $this->pro['title']=$value[1];
54
                $this->pro['time_limit']=$value[3];
55
                $this->pro['memory_limit']=131072; // Given in elder codes
56
                $this->pro['solved_count']=$value[2];
57
                $this->pro['input_type']='standard input';
58
                $this->pro['output_type']='standard output';
59
                $this->pro['description']="<a href=\"/external/gym/UvaL{$key}.pdf\">[Attachment Link]</a>";
60
                $this->pro['input']='';
61
                $this->pro['output']='';
62
                $this->pro['note']='';
63
                $this->pro['sample']=[];
64
                $this->pro['source']='Here';
65
                $this->pro['file']=1;
66
67
                $problem=$problemModel->pid($this->pro['pcode']);
68
69
                if ($problem) {
70
                    $problemModel->clearTags($problem);
71
                    $new_pid=$this->update_problem($this->oid);
0 ignored issues
show
Unused Code introduced by
The assignment to $new_pid is dead and can be removed.
Loading history...
72
                } else {
73
                    $new_pid=$this->insert_problem($this->oid);
74
                }
75
76
                // $problemModel->addTags($new_pid, $tag); // not present
77
            }
78
            $this->data=array_keys($info);
79
        } else {
80
            $pf=substr($con, 0, strlen($con)-2);
81
            $res=Requests::get("https://icpcarchive.ecs.baylor.edu/external/$pf/$con.pdf");
82
            file_put_contents(base_path("public/external/gym/UvaL$con.pdf"), $res->body);
83
        }
84
    }
85
}
86