Collector::getRenderer()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2.0185

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 11
ccs 5
cts 6
cp 0.8333
rs 9.4286
cc 2
eloc 6
nc 2
nop 0
crap 2.0185
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: arnaud
5
 * Date: 31/10/15
6
 * Time: 17:40
7
 */
8
9
namespace Ndrx\Profiler\Collectors;
10
11
use Ndrx\Profiler\Collectors\Contracts\CollectorInterface;
12
use Ndrx\Profiler\Collectors\Contracts\StreamCollectorInterface;
13
use Ndrx\Profiler\DataSources\Contracts\DataSourceInterface;
14
use Ndrx\Profiler\JsonPatch;
15
use Ndrx\Profiler\Process;
16
use Ndrx\Profiler\Renderer\RenderableInterface;
17
use Ndrx\Profiler\Renderer\RendererInterface;
18
19
abstract class Collector implements CollectorInterface, RenderableInterface
20
{
21
    /**
22
     * @var mixed
23
     */
24
    protected $data;
25
26
    /** @var  DataSourceInterface */
27
    protected $dataSource;
28
29
    /**
30
     * @var Process
31
     */
32
    protected $process;
33
34
    /**
35
     * @var JsonPatch
36
     */
37
    protected $jsonPatch;
38
39
    /**
40
     * @var Validator
41
     */
42
    protected $validator;
43
44
    /**
45
     * @param Process $process
46
     * @param DataSourceInterface $dataSource
47
     * @param JsonPatch|null $jsonPatch
48
     */
49 180
    public function __construct(Process $process, DataSourceInterface $dataSource, JsonPatch $jsonPatch = null)
50
    {
51 180
        $this->dataSource = $dataSource;
52 180
        $this->process = $process;
53 180
        $this->jsonPatch = $jsonPatch;
54
55 180
        if ($jsonPatch === null) {
56 180
            $this->jsonPatch = new JsonPatch();
57 180
        }
58
59 180
        $this->validator = new Validator($this->getDataFields(), $this->getDefaultValue());
0 ignored issues
show
Unused Code introduced by
The call to Validator::__construct() has too many arguments starting with $this->getDefaultValue().

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
60 180
    }
61
62
    /**
63
     * @return array
64
     */
65 146
    public function getDataFields()
66
    {
67 146
        return [];
68
    }
69
70
    /**
71
     * @return array
72
     */
73 180
    public function getDefaultValue()
74
    {
75 180
        return [];
76
    }
77
78
    /**
79
     * Convert data into jsonpatch query and save it in the data source
80
     * @return mixed
81
     */
82 114
    public function persist()
83
    {
84 114
        $this->validate();
85
86 114
        if (!is_array($this->data)) {
87 102
            $this->data = [$this->data];
88 102
        }
89
90 114
        $append = $this instanceof StreamCollectorInterface;
91 114
        $patches = $this->jsonPatch->generateArrayAdd($this->getPath(), $this->data, $append);
92 114
        $this->dataSource->save($this->process, $patches);
93
94 114
        $this->data = [];
95 114
    }
96
97 14
    public function validate()
98
    {
99 14
        $this->validator->validate($this->data);
100 14
    }
101
102
    /**
103
     * @return mixed
104
     */
105 32
    public function getData()
106
    {
107 32
        return $this->data;
108
    }
109
110
    /**
111
     * @return RendererInterface
112
     *
113
     * @throws \RuntimeException
114
     */
115 16
    public function getRenderer()
116
    {
117 16
        $collectorClass = get_class($this);
118 16
        $rendererClass = '\\' . str_replace('Collectors', 'Renderer\\Html', $collectorClass);
119
120 16
        if (!class_exists($rendererClass)) {
121
            throw new \RuntimeException('Collector ' . $collectorClass . ' is renderable but ' . $rendererClass . ' does not exist');
122
        }
123
124 16
        return new $rendererClass;
125
    }
126
127
    /**
128
     * @return string
129
     */
130 16
    public function getName()
131
    {
132 16
        $reflectionClassName = new \ReflectionClass(get_class($this));
133 16
        $pattern = '/([a-z])([A-Z])/';
134 16
        return strtolower(preg_replace_callback($pattern, function ($a) {
135 16
            return $a[1] . '-' . strtolower($a[2]);
136 16
        }, $reflectionClassName->getShortName()));
137
    }
138
}
139