ScrutinizerCheck::run()   B
last analyzed

Complexity

Conditions 7
Paths 13

Size

Total Lines 33
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 7
eloc 19
c 1
b 1
f 0
nc 13
nop 0
dl 0
loc 33
rs 8.8333
1
<?php
2
3
namespace SilverStripe\ModuleRatings\Check;
4
5
use Exception;
6
use SilverStripe\ModuleRatings\Check;
7
8
class ScrutinizerCheck extends Check
9
{
10
    public const THRESHOLD = 6.5;
11
12
    public function getKey()
13
    {
14
        return 'good_scrutinizer_score';
15
    }
16
17
    public function getDescription()
18
    {
19
        return 'Has Scrutinizer CI configured and a "good" score (greater than '
20
            . self::THRESHOLD . '/10, requires slug)';
21
    }
22
23
    public function run()
24
    {
25
        // This check requires a repository slug to be provided
26
        $slug = $this->getSuite()->getRepositorySlug();
27
        if (!$slug) {
28
            return;
29
        }
30
31
        try {
32
            $result = $this->getRequestClient()
33
                ->get('https://scrutinizer-ci.com/api/repositories/g/' . $slug)
34
                ->getBody();
35
        } catch (Exception $ex) {
36
            if ($logger = $this->getSuite()->getLogger()) {
37
                $logger->debug($ex->getMessage());
38
            }
39
            $result = '';
40
        }
41
        $response = json_decode($result, true);
42
43
        // Fetch failure
44
        if (!$response) {
45
            return;
46
        }
47
48
        // Not set up (404)
49
        if (!isset($response['applications']['master']['index']['_embedded']['project']['metric_values'])) {
50
            return;
51
        }
52
53
        $metrics = $response['applications']['master']['index']['_embedded']['project']['metric_values'];
54
        if ($metrics['scrutinizer.quality'] >= self::THRESHOLD) {
55
            $this->setSuccessful(true);
56
        }
57
    }
58
}
59