AbstractReportProcessor::setTarget()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of the doyo/code-coverage project.
5
 *
6
 * (c) Anthonius Munthi <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace Doyo\Bridge\CodeCoverage\Report;
15
16
use Doyo\Bridge\CodeCoverage\Console\ConsoleIO;
17
use Doyo\Bridge\CodeCoverage\ProcessorInterface;
18
19
abstract class AbstractReportProcessor implements ReportProcessorInterface
20
{
21
    const OUTPUT_FILE    = 'file';
22
    const OUTPUT_DIR     = 'dir';
23
    const OUTPUT_CONSOLE = 'console';
24
25
    protected $processor;
26
27
    /**
28
     * @var string
29
     */
30
    protected $target;
31
32
    /**
33
     * @var string
34
     */
35
    protected $fileSystemType;
36
37
    /**
38
     * A default options for this report processor.
39
     *
40
     * @var array
41
     */
42
    protected $defaultOptions = [];
43
44
    public function __construct(array $options = [])
45
    {
46
        $options = array_merge($this->defaultOptions, $options);
47
        foreach ($options as $name => $value) {
48
            $method = 'set'.ucfirst($name);
49
            if (method_exists($this, $method)) {
50
                unset($options[$name]);
51
                \call_user_func_array([$this, $method], [$value]);
52
            }
53
        }
54
55
        $this->processor = $this->createProcessor($options);
56
    }
57
58
    abstract public function getProcessorClass(): string;
59
60
    /**
61
     * Get the output type of this report.
62
     *
63
     * @return string
64
     */
65
    abstract public function getOutputType(): string;
66
67
    /**
68
     * @param string $target
69
     */
70
    public function setTarget(string $target)
71
    {
72
        $this->target = $target;
73
    }
74
75
    public function getTarget(): string
76
    {
77
        return $this->target;
78
    }
79
80
    /**
81
     * {@inheritdoc}
82
     */
83
    public function getProcessor()
84
    {
85
        return $this->processor;
86
    }
87
88
    public function process(ProcessorInterface $processor, ConsoleIO $consoleIO)
89
    {
90
        try {
91
            $reportProcessor = $this->processor;
92
            $reportProcessor->process($processor->getCodeCoverage(), $this->target);
93
            $info = sprintf(
94
                '<info>generated <comment>%s</comment> to <comment>%s</comment></info>',
95
                $this->getType(),
96
                $this->getTarget()
97
            );
98
            $consoleIO->coverageInfo($info);
99
        } catch (\Exception $exception) {
100
            $message = sprintf(
101
                "Failed to generate report type: <comment>%s</comment>. Error message:\n%s",
102
                $this->getType(),
103
                $exception->getMessage()
104
            );
105
            $consoleIO->coverageInfo($message);
106
        }
107
    }
108
109
    protected function configure(array $options)
110
    {
111
    }
112
113
    protected function createProcessor(array $options)
114
    {
115
        $r    = new \ReflectionClass($this->getProcessorClass());
116
        $args = [];
117
118
        $constructor= $r->getConstructor();
119
        if (
120
            null !== $constructor
121
            && \is_array($constructorParams = $constructor->getParameters())
122
        ) {
123
            foreach ($constructorParams as $parameter) {
124
                $name    = $parameter->getName();
125
                $value   = null;
126
                $default = null;
127
                if (
128
                    !$parameter->isDefaultValueAvailable()
129
                    && !isset($options[$name])
130
                ) {
131
                    break;
132
                }
133
134
                if ($parameter->isDefaultValueAvailable()) {
135
                    $default = $parameter->getDefaultValue();
136
                }
137
138
                if (isset($options[$name])) {
139
                    $value = $options[$name];
140
                }
141
                $args[] = null !== $value ? $value : $default;
142
            }
143
        }
144
145
        $outputType = $this->getOutputType();
146
        $dir        = $this->getTarget();
147
148
        if (static::OUTPUT_FILE === $outputType) {
149
            $dir = \dirname($dir);
150
        }
151
152
        if (static::OUTPUT_CONSOLE !== $outputType && !is_dir($dir)) {
153
            mkdir($dir, 0775, true);
154
        }
155
156
        return $r->newInstanceArgs($args);
157
    }
158
}
159