Passed
Branch master (9c9e82)
by ANTHONIUS
01:35
created

AbstractReportProcessor::setTarget()   A

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 1
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
namespace Doyo\Bridge\CodeCoverage\Report;
5
6
7
use Doyo\Bridge\CodeCoverage\Console\ConsoleIO;
8
use Doyo\Bridge\CodeCoverage\Exception\ReportException;
9
use Doyo\Bridge\CodeCoverage\ProcessorInterface;
10
11
abstract class AbstractReportProcessor implements ReportProcessorInterface
12
{
13
    const OUTPUT_FILE = 'file';
14
    const OUTPUT_DIR = 'dir';
15
    const OUTPUT_CONSOLE = 'console';
16
17
    protected $processor;
18
19
    /**
20
     * @var string
21
     */
22
    protected $target;
23
24
    /**
25
     * @var string
26
     */
27
    protected $fileSystemType;
28
29
    /**
30
     * A default options for this report processor
31
     *
32
     * @var array
33
     */
34
    protected $defaultOptions = [];
35
36 14
    public function __construct(array $options = array())
37
    {
38 14
        $options = array_merge($this->defaultOptions, $options);
39 14
        foreach($options as $name => $value){
40 14
            $method = 'set'.ucfirst($name);
41 14
            if(method_exists($this,$method)){
42 14
                unset($options[$name]);
43 14
                call_user_func_array([$this,$method],[$value]);
44
            }
45
        }
46
47 14
        $this->processor = $this->createProcessor($options);
48
    }
49
50
    abstract public function getProcessorClass(): string;
51
52
    /**
53
     * Get the output type of this report
54
     *
55
     * @return string
56
     */
57
    abstract public function getOutputType(): string;
58
59
    /**
60
     * @param string $target
61
     */
62 14
    public function setTarget(string $target)
63
    {
64 14
        $this->target = $target;
65
    }
66
67 14
    public function getTarget(): string
68
    {
69 14
        return $this->target;
70
    }
71
72
    /**
73
     * @inheritDoc
74
     */
75 2
    public function getProcessor()
76
    {
77 2
        return $this->processor;
78
    }
79
80 2
    public function process(ProcessorInterface $processor, ConsoleIO $consoleIO)
81
    {
82
        try{
83 2
            $reportProcessor = $this->processor;
84 2
            $reportProcessor->process($processor->getCodeCoverage(), $this->target);
85 1
            $info = sprintf(
86 1
                'generated <comment>%s</comment> to: %s',
87 1
                $this->getType(),
88 1
                $this->getTarget()
89
            );
90 1
            $consoleIO->coverageInfo($info);
91 1
        }catch (\Exception $exception){
92 1
            $message = sprintf(
93 1
                "Failed to generate report type: <comment>%s</comment>. Error message:\n%s",
94 1
                $this->getType(),
95 1
                $exception->getMessage()
96
            );
97 1
            $consoleIO->coverageError($message);
98
        }
99
    }
100
101
    protected function configure(array $options)
102
    {
103
104
    }
105
106 14
    private function createProcessor(array $options)
107
    {
108 14
        $r = new \ReflectionClass($this->getProcessorClass());
109 14
        $args = [];
110
111 14
        $constructor= $r->getConstructor();
112
        if(
113 14
            !is_null($constructor)
114 14
            && is_array($constructorParams = $constructor->getParameters())
115
        ){
116 12
            foreach($constructorParams as $parameter){
117 12
                if(!$parameter->isDefaultValueAvailable()){
118
                    break;
119
                }
120 12
                $name = $parameter->getName();
121 12
                $value = $parameter->getDefaultValue();
122 12
                if(isset($options[$name])){
123 4
                    $value = $options[$name];
124
                }
125 12
                $args[] = $value;
126
            }
127
        }
128
129 14
        $outputType = $this->getOutputType();
130 14
        $dir = $this->getTarget();
131
132 14
        if(static::OUTPUT_FILE === $outputType){
133 8
            $dir = dirname($dir);
134
        }
135
136 14
        if(static::OUTPUT_CONSOLE !== $outputType && !is_dir($dir)){
137 2
            mkdir($dir, 0775, true);
138
        }
139
140 14
        return $r->newInstanceArgs($args);
141
    }
142
}
143