AuditManager   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 145
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 2
dl 0
loc 145
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A getDefaultAudit() 0 6 2
A audit() 0 10 3
A createAudit() 0 10 2
A createDesktopAudit() 0 9 1
A createMobileAudit() 0 9 1
A __call() 0 4 1
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
     *
80
     * @return AbstractAudit
81
     */
82
    public function audit(string $audit = null) :AbstractAudit
83
    {
84
        $audit = $audit ?: $this->getDefaultAudit();
85
86
        if (!isset($this->audits[$audit])) {
87
            $this->audits[$audit] = $this->createAudit($audit);
88
        }
89
90
        return $this->audits[$audit];
91
    }
92
93
    /**
94
     * Create a new audit instance based on
95
     * the given audit name.
96
     *
97
     * @param string $audit
98
     *
99
     * @return AbstractAudit
100
     */
101
    protected function createAudit(string $audit) :AbstractAudit
102
    {
103
        $method = 'create'.ucfirst($audit).'Audit';
104
105
        if (method_exists($this, $method)) {
106
            return $this->$method();
107
        }
108
109
        throw new \InvalidArgumentException("Audit [$audit] is not supported.");
110
    }
111
112
    /**
113
     * Create a new desktop audit instance.
114
     *
115
     * @return AbstractAudit
116
     */
117
    protected function createDesktopAudit() :AbstractAudit
118
    {
119
        return new DesktopAudit(
120
            $this->processBuilder,
121
            $this->input,
122
            $this->output,
123
            $this->filename
124
        );
125
    }
126
127
    /**
128
     * Create a new mobile audit instance.
129
     *
130
     * @return AbstractAudit
131
     */
132
    protected function createMobileAudit() :AbstractAudit
133
    {
134
        return new MobileAudit(
135
            $this->processBuilder,
136
            $this->input,
137
            $this->output,
138
            $this->filename
139
        );
140
    }
141
142
    /**
143
     * Dynamically call the default audit instance.
144
     *
145
     * @param string $method
146
     * @param array  $parameters
147
     *
148
     * @return mixed
149
     */
150
    public function __call($method, $parameters)
151
    {
152
        return $this->audit()->$method(...$parameters);
153
    }
154
}
155