ProcedureAwareLogger::enrichContext()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
3
/**
4
 * Copyright 2014 SURFnet bv
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.
8
 * You may obtain a copy of the License at
9
 *
10
 *     http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * See the License for the specific language governing permissions and
16
 * limitations under the License.
17
 */
18
19
namespace Surfnet\StepupRa\RaBundle\Logger;
20
21
use Psr\Log\LoggerInterface;
22
use Surfnet\StepupRa\RaBundle\Exception\InvalidArgumentException;
23
use Surfnet\StepupRa\RaBundle\Exception\RuntimeException;
24
25
/**
26
 * @SuppressWarnings(PHPMD.TooManyPublicMethods)
27
 */
28
final class ProcedureAwareLogger implements LoggerInterface
29
{
30
    /**
31
     * @var string|null
32
     */
33
    private $procedure;
34
35
    /**
36
     * @var LoggerInterface
37
     */
38
    private $logger;
39
40
    public function __construct(LoggerInterface $logger)
41
    {
42
        $this->logger = $logger;
43
    }
44
45 View Code Duplication
    public function forProcedure($procedure)
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...
46
    {
47
        if (!is_string($procedure)) {
48
            throw InvalidArgumentException::invalidType('string', 'procedure', $procedure);
49
        }
50
51
        $logger            = new self($this->logger);
52
        $logger->procedure = $procedure;
53
54
        return $logger;
55
    }
56
57
    public function emergency($message, array $context = [])
58
    {
59
        $this->logger->emergency($message, $this->enrichContext($context));
60
    }
61
62
    public function alert($message, array $context = [])
63
    {
64
        $this->logger->alert($message, $this->enrichContext($context));
65
    }
66
67
    public function critical($message, array $context = [])
68
    {
69
        $this->logger->critical($message, $this->enrichContext($context));
70
    }
71
72
    public function error($message, array $context = [])
73
    {
74
        $this->logger->error($message, $this->enrichContext($context));
75
    }
76
77
    public function warning($message, array $context = [])
78
    {
79
        $this->logger->warning($message, $this->enrichContext($context));
80
    }
81
82
    public function notice($message, array $context = [])
83
    {
84
        $this->logger->notice($message, $this->enrichContext($context));
85
    }
86
87
    public function info($message, array $context = [])
88
    {
89
        $this->logger->info($message, $this->enrichContext($context));
90
    }
91
92
    public function debug($message, array $context = [])
93
    {
94
        $this->logger->debug($message, $this->enrichContext($context));
95
    }
96
97
    public function log($level, $message, array $context = [])
98
    {
99
        $this->logger->log($message, $this->enrichContext($context));
0 ignored issues
show
Documentation introduced by
$this->enrichContext($context) is of type array<string,string,{"procedure":"string"}>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
100
    }
101
102
103
    /**
104
     * Adds the procedure to the log context.
105
     *
106
     * @param array $context
107
     * @return array
108
     * @throws RuntimeException
109
     */
110
    private function enrichContext(array $context)
111
    {
112
        if (!$this->procedure) {
113
            throw new RuntimeException('Authentication logging context is unknown');
114
        }
115
116
        $context['procedure'] = $this->procedure;
117
118
        return $context;
119
    }
120
}
121