LogExceptionsInterceptor::invoke()   A
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 27

Duplication

Lines 6
Ratio 22.22 %

Importance

Changes 0
Metric Value
dl 6
loc 27
rs 9.1768
c 0
b 0
f 0
cc 5
nc 5
nop 1
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
29
/**
30
 * Class LogExceptionsInterceptor
31
 */
32
class LogExceptionsInterceptor extends AbstractLogger implements MethodInterceptor
33
{
34
    use AnnotationReaderTrait;
35
36
    /**
37
     * @param MethodInvocation $invocation
38
     *
39
     * @return object
40
     * @throws \Exception
41
     */
42
    public function invoke(MethodInvocation $invocation)
43
    {
44
        /** @var \Ytake\LaravelAspect\Annotation\LogExceptions $annotation */
45
        $annotation = $invocation->getMethod()->getAnnotation($this->annotation) ?? new $this->annotation([]);
46
        try {
47
            $result = $invocation->proceed();
48
        } catch (\Exception $exception) {
49
            if ($exception instanceof $annotation->expect) {
50
                $logFormat = $this->logFormatter($annotation, $invocation);
51
                $logger = static::$logger;
52
                /** Monolog\Logger */
53
                $logFormat['context']['code'] = $exception->getCode();
54
                $logFormat['context']['error_message'] = $exception->getMessage();
55 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...
56
                    if (!is_null($annotation->driver)) {
57
                        $logger = $logger->driver($annotation->driver);
58
                    }
59
                    $logger->addRecord($logFormat['level'], $logFormat['message'], $logFormat['context']);
60
                }
61
            }
62
            throw $exception;
63
        }
64
65
        // @codeCoverageIgnoreStart
66
        return $result;
67
        // @codeCoverageIgnoreEnd
68
    }
69
}
70