ServiceCaller   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A call() 0 8 2
A hasHandler() 0 4 1
1
<?php
2
3
namespace BrightComponents\Services;
4
5
use Illuminate\Contracts\Container\Container;
6
use BrightComponents\Services\Exceptions\ServiceHandlerMethodException;
7
8
class ServiceCaller extends AbstractServiceCaller
9
{
10
    /**
11
     * The container implementation.
12
     *
13
     * @var \Illuminate\Contracts\Container\Container
14
     */
15
    protected $container;
16
17
    /**
18
     * The handler method to be called.
19
     *
20
     * @var string
21
     */
22
    public static $handlerMethod;
23
24
    /**
25
     * Create a new service caller instance.
26
     *
27
     * @param  \Illuminate\Contracts\Container\Container  $container
28
     */
29
    public function __construct(Container $container)
30
    {
31
        $this->container = $container;
32
    }
33
34
    /**
35
     * Call a service through its appropriate handler.
36
     *
37
     * @param  string  $service
38
     * @param  mixed  ...$params
39
     *
40
     * @return mixed
41
     */
42
    public function call($service, ...$params)
43
    {
44
        if (! $this->hasHandler($service)) {
45
            throw ServiceHandlerMethodException::notFound($service);
46
        }
47
48
        return $this->container->make($service)->{$this::$handlerMethod}(...$params);
49
    }
50
51
    /**
52
     * Determine if the service handler method exists.
53
     *
54
     * @param  mixed  $service
55
     *
56
     * @return bool
57
     */
58
    public function hasHandler($service)
59
    {
60
        return method_exists($service, $this::$handlerMethod);
61
    }
62
}
63