ScrutinizerCheck   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getDescription() 0 4 1
A getKey() 0 3 1
B run() 0 33 7
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