1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Freshcells\SoapClientBundle\Plugin; |
4
|
|
|
|
5
|
|
|
use Freshcells\SoapClientBundle\Event\Events; |
6
|
|
|
use Freshcells\SoapClientBundle\Event\FaultEvent; |
7
|
|
|
use Freshcells\SoapClientBundle\Event\RequestEvent; |
8
|
|
|
use Freshcells\SoapClientBundle\Event\ResponseEvent; |
9
|
|
|
use Psr\Log\LoggerInterface; |
10
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
11
|
|
|
|
12
|
|
|
class AnonymizerLogPlugin implements EventSubscriberInterface |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var LoggerInterface |
16
|
|
|
*/ |
17
|
|
|
private $logger; |
18
|
|
|
private $elements = []; |
19
|
|
|
private $attributes = []; |
20
|
|
|
private $substitute; |
21
|
|
|
private $headersToSubstitute = []; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* AnonymizerLogPlugin constructor. |
25
|
|
|
* @param LoggerInterface $logger |
26
|
|
|
* @param string[] $elements |
27
|
|
|
* @param string[] $attributes |
28
|
|
|
* @param string $substitute |
29
|
|
|
* @param string[] $headersToSubstitute |
30
|
|
|
*/ |
31
|
|
|
public function __construct( |
32
|
|
|
LoggerInterface $logger, |
33
|
|
|
array $elements, |
34
|
|
|
array $attributes, |
35
|
|
|
$substitute = '*****', |
36
|
|
|
array $headersToSubstitute = [] |
37
|
|
|
) { |
38
|
|
|
$this->logger = $logger; |
39
|
|
|
$this->elements = $elements; |
40
|
|
|
$this->attributes = $attributes; |
41
|
|
|
$this->substitute = $substitute; |
42
|
|
|
$this->headersToSubstitute = $headersToSubstitute; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param RequestEvent $event |
47
|
|
|
*/ |
48
|
|
|
public function onClientRequest(RequestEvent $event) |
49
|
|
|
{ |
50
|
|
|
$content = $this->anonymize( |
51
|
|
|
print_r($event->getRequest(), true) |
52
|
|
|
); |
53
|
|
|
|
54
|
|
|
$this->logger->info( |
55
|
|
|
sprintf( |
56
|
|
|
'[freshcells/soap-client-bundle] pre request: about to call "%s" with params %s', |
57
|
|
|
$event->getResource(), |
58
|
|
|
$content, |
59
|
|
|
true |
60
|
|
|
) |
61
|
|
|
); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @param ResponseEvent $event |
66
|
|
|
*/ |
67
|
|
|
public function onClientResponse(ResponseEvent $event) |
68
|
|
|
{ |
69
|
|
|
$requestContent = $this->anonymize(print_r($event->getRequestContent(), true)); |
70
|
|
|
$this->logger->info(sprintf( |
71
|
|
|
'[freshcells/soap-client-bundle] request: %s %s', |
72
|
|
|
print_r($event->getRequestHeaders(), true), |
73
|
|
|
$requestContent |
74
|
|
|
)); |
75
|
|
|
$responseContent = $this->anonymize(print_r($event->getResponseContent(), true)); |
76
|
|
|
$this->logger->info(sprintf( |
77
|
|
|
'[freshcells/soap-client-bundle] response: %s %s', |
78
|
|
|
print_r($event->getResponseHeaders(), true), |
79
|
|
|
$responseContent |
80
|
|
|
)); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @param FaultEvent $event |
85
|
|
|
*/ |
86
|
|
View Code Duplication |
public function onClientFault(FaultEvent $event) |
|
|
|
|
87
|
|
|
{ |
88
|
|
|
$requestContent = $this->anonymizeElements(print_r($event->getRequestContent(), true)); |
|
|
|
|
89
|
|
|
$this->logger->error(sprintf( |
90
|
|
|
'[freshcells/soap-client-bundle] fault "%s" for request "%s" with params %s', |
91
|
|
|
$event->getException()->getMessage(), |
92
|
|
|
$event->getRequestEvent()->getResource(), |
93
|
|
|
$requestContent |
94
|
|
|
)); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
protected function anonymize(string $content) |
98
|
|
|
{ |
99
|
|
|
//elements |
100
|
|
|
foreach ($this->elements as $field) { |
101
|
|
|
$content = preg_replace( |
102
|
|
|
sprintf('/<%s[^>]*>.*?<\/%s>/i', $field, $field), |
103
|
|
|
sprintf('<%s>%s</%s>', $field, $this->substitute, $field), |
104
|
|
|
$content |
105
|
|
|
); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
//attributes |
109
|
|
|
foreach ($this->attributes as $attribute) { |
110
|
|
|
$re = '/ '.$attribute.'="[^"]*/'; |
111
|
|
|
$subst = ' '.$attribute.'="'.$this->substitute; |
112
|
|
|
|
113
|
|
|
$content = preg_replace($re, $subst, $content); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
return $content; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* {@inheritdoc} |
121
|
|
|
*/ |
122
|
|
|
public static function getSubscribedEvents() |
123
|
|
|
{ |
124
|
|
|
return [ |
125
|
|
|
Events::REQUEST => 'onClientRequest', |
126
|
|
|
Events::RESPONSE => 'onClientResponse', |
127
|
|
|
Events::FAULT => 'onClientFault', |
128
|
|
|
]; |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|
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.