LazyMessage::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
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\Queue;
21
22
use ReflectionClass;
23
use Ray\Aop\MethodInvocation;
24
use Illuminate\Bus\Queueable;
25
use Illuminate\Container\Container;
26
use Illuminate\Queue\SerializesModels;
27
use Illuminate\Queue\InteractsWithQueue;
28
use Illuminate\Contracts\Queue\ShouldQueue;
29
30
use function get_class;
31
use function array_values;
32
33
/**
34
 * Class LazyMessage
35
 */
36
class LazyMessage implements ShouldQueue
37
{
38
    use InteractsWithQueue, Queueable, SerializesModels;
39
40
    /** @var MethodInvocation */
41
    protected $methodInvocation;
42
43
    /**
44
     * LazyMessage constructor.
45
     *
46
     * @param MethodInvocation $invocation
47
     */
48
    public function __construct(MethodInvocation $invocation)
49
    {
50
        $this->methodInvocation = $invocation;
51
    }
52
53
    /**
54
     * @param Container $container
55
     *
56
     * @throws \ReflectionException
57
     */
58
    public function handle(Container $container)
59
    {
60
        $class = new ReflectionClass(get_class($this->methodInvocation->getThis()));
61
        if ($class->getFileName()) {
62
            $method = $this->methodInvocation->getMethod()->getName();
0 ignored issues
show
Bug introduced by
Consider using $this->methodInvocation->getMethod()->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
63
            $parameters = $this->methodInvocation->getMethod()->getParameters();
64
            $array = array_values($this->methodInvocation->getArguments()->getArrayCopy());
65
            $arguments = [];
66
            foreach ($parameters as $k => $parameter) {
67
                $arguments[$parameter->getName()] = $array[$k];
0 ignored issues
show
Bug introduced by
Consider using $parameter->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
68
            }
69
            $container->call(
70
                [
71
                    $this->methodInvocation->getThis(),
72
                    $method,
73
                ],
74
                $arguments
75
            );
76
        }
77
    }
78
}
79