Completed
Push — master ( 401ef4...2832e5 )
by yuuki
02:02
created

MessageDrivenInterceptor::invoke()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
c 0
b 0
f 0
rs 9.4285
cc 2
eloc 9
nc 2
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-2017 Yuuki Takezawa
16
 *
17
 */
18
namespace Ytake\LaravelAspect\Interceptor;
19
20
use Ray\Aop\MethodInvocation;
21
use Ray\Aop\MethodInterceptor;
22
use Illuminate\Contracts\Bus\Dispatcher;
23
use Ytake\LaravelAspect\Queue\LazyMessage;
24
use Ytake\LaravelAspect\Queue\EagerMessage;
25
use Ytake\LaravelAspect\Annotation\LazyQueue;
26
use Ytake\LaravelAspect\Annotation\MessageDriven;
27
use Ytake\LaravelAspect\Annotation\AnnotationReaderTrait;
28
29
/**
30
 * Class MessageDrivenInterceptor
31
 */
32
class MessageDrivenInterceptor implements MethodInterceptor
33
{
34
    use AnnotationReaderTrait;
35
36
    /** @var Dispatcher */
37
    protected static $dispatcher;
38
39
    /**
40
     * @param MethodInvocation $invocation
41
     *
42
     * @return object
43
     * @throws \Exception
44
     */
45
    public function invoke(MethodInvocation $invocation)
46
    {
47
        /** @var MessageDriven $annotation */
48
        $annotation = $invocation->getMethod()->getAnnotation($this->annotation);
49
        $command = new EagerMessage($invocation);
50
        if ($annotation->value instanceof LazyQueue) {
51
            $command = new LazyMessage($invocation);
52
            $command->onQueue($annotation->onQueue)
53
                ->delay($annotation->value->delay())
54
                ->onConnection($annotation->mappedName);
55
        }
56
57
        return static::$dispatcher->dispatch($command);
58
    }
59
60
    /**
61
     * @param Dispatcher $dispatcher
62
     */
63
    public function setBusDispatcher(Dispatcher $dispatcher)
64
    {
65
        static::$dispatcher = $dispatcher;
66
    }
67
}
68