Completed
Push — master ( 09827c...357bd4 )
by Ivo
01:50
created

AnonymizerLogPlugin::getSubscribedEvents()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
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)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
87
    {
88
        $requestContent = $this->anonymizeElements(print_r($event->getRequestContent(), true));
0 ignored issues
show
Bug introduced by
The method getRequestContent() does not seem to exist on object<Freshcells\SoapCl...undle\Event\FaultEvent>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
Bug introduced by
The method anonymizeElements() does not exist on Freshcells\SoapClientBun...gin\AnonymizerLogPlugin. Did you maybe mean anonymize()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
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