Result   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 116
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 41
c 1
b 0
f 0
dl 0
loc 116
rs 10
wmc 14

8 Methods

Rating   Name   Duplication   Size   Complexity  
A getProgressiveWebApp() 0 8 2
A getAccessibility() 0 8 2
A __construct() 0 30 2
A getPerformance() 0 8 2
A getBestPractices() 0 8 2
A __toString() 0 3 1
A getSEO() 0 8 2
A getRawData() 0 3 1
1
<?php
2
3
namespace Jackal\Lighthouse\Result;
4
5
use Symfony\Component\OptionsResolver\OptionsResolver;
6
7
class Result
8
{
9
    /** @var array  */
10
    protected $response;
11
12
    public function __construct(array $response)
13
    {
14
        $this->response = $response;
15
16
        $options = new OptionsResolver();
17
        $options->setDefaults([
18
            'categories' => [
19
                'performance' => [
20
                    'score' => null,
21
                ],
22
                'accessibility' => [
23
                    'score' => null,
24
                ],
25
                'best-practices' => [
26
                    'score' => null,
27
                ],
28
                'seo' => [
29
                    'score' => null,
30
                ],
31
                'pwa' => [
32
                    'score' => null,
33
                ],
34
            ],
35
        ]);
36
37
        foreach (array_keys($response) as $key){
38
            $options->setDefault($key, null);
39
        }
40
41
        $this->response = $options->resolve($response);
42
    }
43
44
    /**
45
     * @return string
46
     */
47
    public function __toString() : string
48
    {
49
        return json_encode($this->response);
50
    }
51
52
    /**
53
     * @return array
54
     */
55
    public function getRawData() : array
56
    {
57
        return $this->response;
58
    }
59
60
    /**
61
     * @return int|null
62
     */
63
    public function getPerformance() : ?int
64
    {
65
        $value = $this->response['categories']['performance']['score'];
66
        if(!$value){
67
            return $value;
68
        }
69
70
        return $value * 100;
71
    }
72
73
    /**
74
     * @return int|null
75
     */
76
    public function getAccessibility() : ?int
77
    {
78
        $value = $this->response['categories']['accessibility']['score'];
79
        if(!$value){
80
            return $value;
81
        }
82
83
        return $value * 100;
84
    }
85
86
    /**
87
     * @return int|null
88
     */
89
    public function getBestPractices() : ?int
90
    {
91
        $value = $this->response['categories']['best-practices']['score'];
92
        if(!$value){
93
            return $value;
94
        }
95
96
        return $value * 100;
97
    }
98
99
    /**
100
     * @return int|null
101
     */
102
    public function getSEO() : ?int
103
    {
104
        $value = $this->response['categories']['seo']['score'];
105
        if(!$value){
106
            return $value;
107
        }
108
109
        return $value * 100;
110
    }
111
112
    /**
113
     * @return int|null
114
     */
115
    public function getProgressiveWebApp() : ?int
116
    {
117
        $value = $this->response['categories']['pwa']['score'];
118
        if(!$value){
119
            return $value;
120
        }
121
122
        return $value * 100;
123
    }
124
}
125