ExtractRepoMetaData   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Test Coverage

Coverage 88%

Importance

Changes 4
Bugs 0 Features 2
Metric Value
eloc 21
c 4
b 0
f 2
dl 0
loc 74
ccs 22
cts 25
cp 0.88
rs 10
wmc 12

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getDom() 0 15 3
A getRepo() 0 3 1
A getStars() 0 5 2
A getName() 0 3 2
A getUrl() 0 3 1
A getDescription() 0 5 2
1
<?php
2
3
namespace PiedWeb\Github;
4
5
use rOpenDev\curl\CurlRequest;
6
use simple_html_dom;
7
8
class ExtractRepoMetaData
9
{
10
    private $dom;
11
    private $url;
12
    private $name;
13
14 3
    public function __construct(string $url, $name = 0)
15
    {
16 3
        $this->url = $url;
17 3
        $this->name = $name;
18 3
    }
19
20
    /**
21
     * @throw \Exception if something happened during the github download
22
     */
23 3
    private function getDom()
24
    {
25 3
        if (null === $this->dom) {
26 3
            $request = new CurlRequest($this->url);
27 3
            $request->setDefaultGetOptions()->setReturnHeader()->setDestkopUserAgent()->setEncodingGzip();
28 3
            $output = $request->execute();
29
30 3
            if ($request->hasError()) {
31
                throw new \Exception('An error occured when trying to get '.$this->url.'');
32
            }
33
34 3
            $this->dom = new simple_html_dom($output);
35
        }
36
37 3
        return $this->dom;
38
    }
39
40
    /**
41
     * @return string (empty if no description found)
42
     */
43 3
    public function getDescription(): string
44
    {
45 3
        $findDescription = $this->getDom()->find('p.f4', 0);
46
47 3
        return $findDescription ? $findDescription->plaintext : '';
48
    }
49
50
    /**
51
     * @return string or null
52
     */
53 3
    public function getStars(): ?string
54
    {
55 3
        $findStars = $this->getDom()->find('[href='.$this->getRepo().'/stargazers]', 0);
56
57 3
        return $findStars ? trim($findStars->plaintext) : null;
58
    }
59
60
    /**
61
     * @return string
62
     */
63 3
    public function getName(): string
64
    {
65 3
        return is_int($this->name) ? preg_replace('@https://github.com/[^/]+/@si', '', $this->url) : $this->name;
66
    }
67
68
    /**
69
     * @return string
70
     */
71 3
    public function getRepo(): string
72
    {
73 3
        return str_replace('https://github.com', '', $this->url);
74
    }
75
76
    /**
77
     * @return string
78
     */
79
    public function getUrl(): string
80
    {
81
        return $this->url;
82
    }
83
}
84