Completed
Pull Request — master (#24)
by yuuki
02:14
created

LogExceptionsInterceptor::invoke()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 23
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 23
rs 9.0856
cc 3
eloc 13
nc 3
nop 1
1
<?php
2
3
/**
4
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
5
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
6
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
7
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
8
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
9
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
10
 * THE SOFTWARE.
11
 *
12
 * This software consists of voluntary contributions made by many individuals
13
 * and is licensed under the MIT license.
14
 *
15
 * Copyright (c) 2015-2016 Yuuki Takezawa
16
 *
17
 */
18
namespace Ytake\LaravelAspect\Interceptor;
19
20
use Ray\Aop\MethodInvocation;
21
use Ray\Aop\MethodInterceptor;
22
use Ytake\LaravelAspect\Annotation\AnnotationReaderTrait;
23
24
/**
25
 * Class LogExceptionsInterceptor
26
 */
27
class LogExceptionsInterceptor extends AbstractLogger implements MethodInterceptor
28
{
29
    use AnnotationReaderTrait;
30
31
    /**
32
     * @param MethodInvocation $invocation
33
     *
34
     * @return object
35
     * @throws \Exception
36
     */
37
    public function invoke(MethodInvocation $invocation)
38
    {
39
        try {
40
            $result = $invocation->proceed();
41
        } catch (\Exception $exception) {
42
            /** @var \Ytake\LaravelAspect\Annotation\LogExceptions $annotation */
43
            $annotation = $this->reader->getMethodAnnotation($invocation->getMethod(), $this->annotation);
44
45
            if ($exception instanceof $annotation->expect) {
46
                $logFormat = $this->logFormatter($annotation, $invocation);
47
                /** @var \Monolog\Logger $logger */
48
                $logger = app('log')->getMonoLog();
49
                /** Monolog\Logger */
50
                $logFormat['context']['code'] = $exception->getCode();
51
                $logFormat['context']['error_message'] = $exception->getMessage();
52
                $logger->log($logFormat['level'], $logFormat['message'], $logFormat['context']);
0 ignored issues
show
Documentation introduced by
$logFormat['context'] is of type string, but the function expects a array.

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...
53
            }
54
            throw $exception;
55
        }
56
        // @codeCoverageIgnoreStart
57
        return $result;
58
        // @codeCoverageIgnoreEnd
59
    }
60
}
61