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

AuditManager::createDesktopAudit()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.9666
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Suitmedia\LighthouseAudit\Audit;
4
5
use Suitmedia\LighthouseAudit\Command;
6
use Suitmedia\LighthouseAudit\ProcessBuilder;
7
use Symfony\Component\Console\Input\InputInterface;
8
use Symfony\Component\Console\Output\OutputInterface;
9
10
class AuditManager
11
{
12
    /**
13
     * Audit objects.
14
     *
15
     * @var array
16
     */
17
    protected $audits = [];
18
19
    /**
20
     * Process builder object.
21
     *
22
     * @var ProcessBuilder
23
     */
24
    protected $processBuilder;
25
26
    /**
27
     * Console input interface.
28
     *
29
     * @var InputInterface
30
     */
31
    protected $input;
32
33
    /**
34
     * Console output interface.
35
     *
36
     * @var OutputInterface
37
     */
38
    protected $output;
39
40
    /**
41
     * Current filename.
42
     *
43
     * @var string
44
     */
45
    protected $filename;
46
47
    /**
48
     * AuditManager constructor.
49
     *
50
     * @param ProcessBuilder $processBuilder
51
     * @param InputInterface $input
52
     * @param OutputInterface $output
53
     * @param string $filename
54
     */
55
    public function __construct(ProcessBuilder $processBuilder, InputInterface $input, OutputInterface $output, string $filename)
56
    {
57
        $this->processBuilder = $processBuilder;
58
        $this->input = $input;
59
        $this->output = $output;
60
        $this->filename = $filename;
61
    }
62
63
    /**
64
     * Get the default audit name.
65
     *
66
     * @return string
67
     */
68
    public function getDefaultAudit() :string
69
    {
70
        $mode = $this->input->getOption('mode');
71
72
        return is_string($mode) ? $mode : Command::DEFAULT_MODE;
73
    }
74
75
    /**
76
     * Get an audit instance.
77
     *
78
     * @param string|null $audit
79
     * @return AbstractAudit
80
     */
81
    public function audit(string $audit = null) :AbstractAudit
82
    {
83
        $audit = $audit ?: $this->getDefaultAudit();
84
85
        if (! isset($this->audits[$audit])) {
86
            $this->audits[$audit] = $this->createAudit($audit);
87
        }
88
89
        return $this->audits[$audit];
90
    }
91
92
    /**
93
     * Create a new audit instance based on
94
     * the given audit name.
95
     *
96
     * @param string $audit
97
     * @return AbstractAudit
98
     */
99
    protected function createAudit(string $audit) :AbstractAudit
100
    {
101
        $method = 'create'.ucfirst($audit).'Audit';
102
103
        if (method_exists($this, $method)) {
104
            return $this->$method();
105
        }
106
107
        throw new \InvalidArgumentException("Audit [$audit] is not supported.");
108
    }
109
110
    /**
111
     * Create a new desktop audit instance.
112
     *
113
     * @return AbstractAudit
114
     */
115
    protected function createDesktopAudit() :AbstractAudit
116
    {
117
        return new DesktopAudit(
118
            $this->processBuilder,
119
            $this->input,
120
            $this->output,
121
            $this->filename
122
        );
123
    }
124
125
    /**
126
     * Create a new mobile audit instance.
127
     *
128
     * @return AbstractAudit
129
     */
130
    protected function createMobileAudit() :AbstractAudit
131
    {
132
        return new MobileAudit(
133
            $this->processBuilder,
134
            $this->input,
135
            $this->output,
136
            $this->filename
137
        );
138
    }
139
140
    /**
141
     * Dynamically call the default audit instance.
142
     *
143
     * @param  string  $method
144
     * @param  array   $parameters
145
     * @return mixed
146
     */
147
    public function __call($method, $parameters)
148
    {
149
        return $this->audit()->$method(...$parameters);
150
    }
151
}
152