LoggableInterceptor   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 34
Duplicated Lines 17.65 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 6
dl 6
loc 34
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A invoke() 6 23 4

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
declare(strict_types=1);
3
4
/**
5
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
6
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
7
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
8
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
9
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
10
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
11
 * THE SOFTWARE.
12
 *
13
 * This software consists of voluntary contributions made by many individuals
14
 * and is licensed under the MIT license.
15
 *
16
 * Copyright (c) 2015-2020 Yuuki Takezawa
17
 *
18
 */
19
20
namespace Ytake\LaravelAspect\Interceptor;
21
22
use Illuminate\Log\LogManager;
23
use Ray\Aop\MethodInterceptor;
24
use Ray\Aop\MethodInvocation;
25
use Ytake\LaravelAspect\Annotation\AnnotationReaderTrait;
26
27
use function is_null;
28
use function microtime;
29
use function number_format;
30
31
/**
32
 * Class LoggableInterceptor
33
 */
34
class LoggableInterceptor extends AbstractLogger implements MethodInterceptor
35
{
36
    use AnnotationReaderTrait;
37
38
    /**
39
     * @param MethodInvocation $invocation
40
     *
41
     * @return object
42
     * @throws \Exception
43
     */
44
    public function invoke(MethodInvocation $invocation)
45
    {
46
        /** @var \Ytake\LaravelAspect\Annotation\Loggable $annotation */
47
        $annotation = $invocation->getMethod()->getAnnotation($this->annotation) ?? new $this->annotation([]);
48
        $start = microtime(true);
49
        $result = $invocation->proceed();
50
        $time = number_format(microtime(true) - $start, 15);
51
        $logFormat = $this->logFormatter($annotation, $invocation);
52
        $logger = static::$logger;
53
        if (!$annotation->skipResult) {
54
            $logFormat['context']['result'] = $result;
55
        }
56
        $logFormat['context']['time'] = $time;
57
        /** Monolog\Logger */
58 View Code Duplication
        if ($logger instanceof LogManager) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
59
            if (!is_null($annotation->driver)) {
60
                $logger = $logger->driver($annotation->driver);
61
            }
62
            $logger->addRecord($logFormat['level'], $logFormat['message'], $logFormat['context']);
63
        }
64
65
        return $result;
66
    }
67
}
68