AbstractReportProcessor::getProcessor()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
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 <https://itstoni.com>
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 27
    public function __construct(array $options = [])
45
    {
46 27
        $options = array_merge($this->defaultOptions, $options);
47 27
        foreach ($options as $name => $value) {
48 27
            $method = 'set'.ucfirst($name);
49 27
            if (method_exists($this, $method)) {
50 27
                unset($options[$name]);
51 27
                \call_user_func_array([$this, $method], [$value]);
52
            }
53
        }
54
55 27
        $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 27
    public function setTarget(string $target)
71
    {
72 27
        $this->target = $target;
73
    }
74
75 27
    public function getTarget(): string
76
    {
77 27
        return $this->target;
78
    }
79
80
    /**
81
     * {@inheritdoc}
82
     */
83 2
    public function getProcessor()
84
    {
85 2
        return $this->processor;
86
    }
87
88 3
    public function process(ProcessorInterface $processor, ConsoleIO $consoleIO)
89
    {
90
        try {
91 3
            $reportProcessor = $this->processor;
92 3
            $reportProcessor->process($processor->getCodeCoverage(), $this->target);
93 2
            $info = sprintf(
94 2
                '<info>generated <comment>%s</comment> to <comment>%s</comment></info>',
95 2
                $this->getType(),
96 2
                $this->getTarget()
97
            );
98 2
            $consoleIO->coverageInfo($info);
99 1
        } catch (\Exception $exception) {
100 1
            $message = sprintf(
101 1
                "Failed to generate report type: <comment>%s</comment>. Error message:\n%s",
102 1
                $this->getType(),
103 1
                $exception->getMessage()
104
            );
105 1
            $consoleIO->coverageInfo($message);
106
        }
107
    }
108
109
    protected function configure(array $options)
110
    {
111
    }
112
113 27
    protected function createProcessor(array $options)
114
    {
115 27
        $r    = new \ReflectionClass($this->getProcessorClass());
116 27
        $args = [];
117
118 27
        $constructor= $r->getConstructor();
119
        if (
120 27
            null !== $constructor
121 27
            && \is_array($constructorParams = $constructor->getParameters())
122
        ) {
123 21
            foreach ($constructorParams as $parameter) {
124 21
                $name    = $parameter->getName();
125 21
                $value   = null;
126 21
                $default = null;
127
                if (
128 21
                    !$parameter->isDefaultValueAvailable()
129 21
                    && !isset($options[$name])
130
                ) {
131
                    break;
132
                }
133
134 21
                if ($parameter->isDefaultValueAvailable()) {
135 18
                    $default = $parameter->getDefaultValue();
136
                }
137
138 21
                if (isset($options[$name])) {
139 7
                    $value = $options[$name];
140
                }
141 21
                $args[] = null !== $value ? $value : $default;
142
            }
143
        }
144
145 27
        $outputType = $this->getOutputType();
146 27
        $dir        = $this->getTarget();
147
148 27
        if (static::OUTPUT_FILE === $outputType) {
149 16
            $dir = \dirname($dir);
150
        }
151
152 27
        if (static::OUTPUT_CONSOLE !== $outputType && !is_dir($dir)) {
153 10
            mkdir($dir, 0775, true);
154
        }
155
156 27
        return $r->newInstanceArgs($args);
157
    }
158
}
159