CommandFactory   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 19
c 1
b 0
f 0
dl 0
loc 42
rs 10
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A getCommand() 0 20 2
1
<?php
2
3
/**
4
 * This file is part of the bugloos/fault-tolerance-bundle project.
5
 * (c) Bugloos <https://bugloos.com/>
6
 * For the full copyright and license information, please view
7
 * the LICENSE file that was distributed with this source code.
8
 */
9
10
namespace Bugloos\FaultToleranceBundle\Factory;
11
12
use Bugloos\FaultToleranceBundle\Contract\Command;
13
use Bugloos\FaultToleranceBundle\RequestLog\RequestLog;
14
use Exception;
15
use ReflectionClass;
16
use ReflectionException;
17
18
/**
19
 * @author Mojtaba Gheytasi <[email protected]>
20
 */
21
class CommandFactory implements CommandFactoryInterface
22
{
23
    private CircuitBreakerFactory $circuitBreakerFactory;
24
25
    private RequestCacheFactory $requestCacheFactory;
26
27
    private RequestLog $requestLog;
28
29
    public function __construct(
30
        CircuitBreakerFactory $circuitBreakerFactory,
31
        RequestCacheFactory $requestCacheFactory,
32
        RequestLog $requestLog
33
    ) {
34
        $this->circuitBreakerFactory = $circuitBreakerFactory;
35
        $this->requestCacheFactory = $requestCacheFactory;
36
        $this->requestLog = $requestLog;
37
    }
38
39
    /**
40
     * @throws ReflectionException
41
     * @throws Exception
42
     */
43
    public function getCommand(...$args): Command
44
    {
45
        $class = func_get_args()[0];
46
47
        $parameters = func_get_args();
48
        array_shift($parameters);
49
50
        $reflection = new ReflectionClass($class);
51
52
        /* @var Command $command */
53
        $command = empty($parameters) ?
54
            $reflection->newInstance() :
55
            $reflection->newInstanceArgs($parameters);
56
57
        $command->initializeConfig();
58
        $command->setCircuitBreakerFactory($this->circuitBreakerFactory);
59
        $command->setRequestCacheFactory($this->requestCacheFactory);
60
        $command->setRequestLog($this->requestLog);
61
62
        return $command;
63
    }
64
}
65