1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Blast Project package. |
5
|
|
|
* |
6
|
|
|
* Copyright (C) 2015-2017 Libre Informatique |
7
|
|
|
* |
8
|
|
|
* This file is licenced under the GNU LGPL v3. |
9
|
|
|
* For the full copyright and license information, please view the LICENSE.md |
10
|
|
|
* file that was distributed with this source code. |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace Blast\CoreBundle\Profiler; |
14
|
|
|
|
15
|
|
|
class Collector |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var mixed |
19
|
|
|
*/ |
20
|
|
|
private $data; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var array |
24
|
|
|
*/ |
25
|
|
|
private $collectedKeys; |
26
|
|
|
|
27
|
|
|
public function __construct() |
28
|
|
|
{ |
29
|
|
|
$this->data = []; |
30
|
|
|
$this->collectedKeys = []; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param string $name |
35
|
|
|
* @param mixed $data |
36
|
|
|
* @param string $destination |
37
|
|
|
* @param string $type |
38
|
|
|
* |
39
|
|
|
* @return $this |
40
|
|
|
*/ |
41
|
|
View Code Duplication |
public function collect($name, $data, $destination = DataCollection::DESTINATION_PROFILER, $type = null) |
|
|
|
|
42
|
|
|
{ |
43
|
|
|
$this->collectedKeys[] = $name; |
44
|
|
|
|
45
|
|
|
$dataCollection = new DataCollection($name, $data, $destination, $type); |
46
|
|
|
$this->data[$this->handleDataKey($name)] = $dataCollection; |
47
|
|
|
|
48
|
|
|
return $this; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @param string $name |
53
|
|
|
* @param mixed $data |
54
|
|
|
* @param string $destination |
55
|
|
|
* @param string $type |
56
|
|
|
* |
57
|
|
|
* @return $this |
58
|
|
|
*/ |
59
|
|
View Code Duplication |
public function collectOnce($name, $data, $destination = DataCollection::DESTINATION_PROFILER, $type = null) |
|
|
|
|
60
|
|
|
{ |
61
|
|
|
if (!in_array($name, $this->collectedKeys)) { |
62
|
|
|
$this->collectedKeys[] = $name; |
63
|
|
|
$dataCollection = new DataCollection($name, $data, $destination, $type); |
64
|
|
|
$this->data[$this->handleDataKey($name)] = $dataCollection; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
return $this; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @return mixed |
72
|
|
|
*/ |
73
|
|
|
public function getData() |
74
|
|
|
{ |
75
|
|
|
return $this->data; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
private function handleDataKey($name) |
79
|
|
|
{ |
80
|
|
|
$keyStrucure = '#%d %s'; |
81
|
|
|
|
82
|
|
|
return sprintf($keyStrucure, count($this->data)+1, $name); |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.