Completed
Push — master ( 29472a...0fa5c2 )
by Richan
04:47 queued 03:37
created

CanRetrieveInputValues::getUrlPrefix()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 2
nc 1
nop 0
1
<?php
2
3
namespace Suitmedia\LighthouseAudit\Audit\Concerns;
4
5
use Suitmedia\LighthouseAudit\Command;
6
7
trait CanRetrieveInputValues
8
{
9
    use SanitizeInput;
10
11
    /**
12
     * Console input interface.
13
     *
14
     * @var \Symfony\Component\Console\Input\InputInterface
15
     */
16
    protected $input;
17
18
    /**
19
     * Get analysis emulation mode.
20
     *
21
     * @return string
22
     */
23
    protected function getMode() :string
24
    {
25
        $mode = $this->input->getOption('mode');
26
27
        return $this->trimDoubleQuotes(is_string($mode) ? $mode : Command::DEFAULT_MODE);
28
    }
29
30
    /**
31
     * Get URL prefix from input.
32
     *
33
     * @return string
34
     */
35
    protected function getUrlPrefix() :string
36
    {
37
        $prefix = $this->input->getOption('url-prefix');
38
39
        return $this->trimDoubleQuotes(is_string($prefix) ? $prefix : Command::DEFAULT_URL_PREFIX);
40
    }
41
42
    /**
43
     * Get the chrome flags.
44
     *
45
     * @return array
46
     */
47
    protected function getChromeFlags() :array
48
    {
49
        $flags = $this->input->getOption('chrome-flags');
50
51
        $flags = is_string($flags) ? explode(' ', $this->trimDoubleQuotes($flags)) : [];
52
        $flags = array_filter(array_map('trim', $flags));
53
54
        return array_unique(array_merge($this->getDefaultChromeFlags(), $flags));
55
    }
56
57
    /**
58
     * Get minimal performance score.
59
     *
60
     * @return string
61
     */
62
    protected function getPerformanceScore() :string
63
    {
64
        $score = $this->input->getOption('performance');
65
66
        return $this->trimDoubleQuotes(is_string($score) ? $score : Command::DEFAULT_PERFORMANCE);
67
    }
68
69
    /**
70
     * Get minimal best-practices score.
71
     *
72
     * @return string
73
     */
74
    protected function getBestPracticesScore() :string
75
    {
76
        $score = $this->input->getOption('best-practices');
77
78
        return $this->trimDoubleQuotes(is_string($score) ? $score : Command::DEFAULT_BEST_PRACTICES);
79
    }
80
81
    /**
82
     * Get minimal accessibility score.
83
     *
84
     * @return string
85
     */
86
    protected function getAccessibilityScore() :string
87
    {
88
        $score = $this->input->getOption('accessibility');
89
90
        return $this->trimDoubleQuotes(is_string($score) ? $score : Command::DEFAULT_ACCESSIBILITY);
91
    }
92
93
    /**
94
     * Get minimal SEO score.
95
     *
96
     * @return string
97
     */
98
    protected function getSeoScore() :string
99
    {
100
        $score = $this->input->getOption('seo');
101
102
        return $this->trimDoubleQuotes(is_string($score) ? $score : Command::DEFAULT_SEO);
103
    }
104
105
    /**
106
     * Get minimal PWA score.
107
     *
108
     * @return string
109
     */
110
    protected function getPwaScore() :string
111
    {
112
        $score = $this->input->getOption('pwa');
113
114
        return $this->trimDoubleQuotes(is_string($score) ? $score : Command::DEFAULT_PWA);
115
    }
116
117
    /**
118
     * Trim double quotes from input options.
119
     *
120
     * @param string $text
121
     * @return string
122
     */
123
    protected function trimDoubleQuotes(string $text) :string
124
    {
125
        return ((strpos($text, '"') === 0) && ($text[strlen($text)-1] === '"')) ?
126
            substr($text, 1, -1) :
127
            $text;
128
    }
129
130
131
    /**
132
     * Get the default chrome flags.
133
     *
134
     * @return array
135
     */
136
    abstract protected function getDefaultChromeFlags() :array;
137
}