StrategyFactory::create()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.7666
c 0
b 0
f 0
ccs 0
cts 11
cp 0
cc 3
nc 3
nop 2
crap 12
1
<?php
2
declare(strict_types=1);
3
4
5
namespace Webhook\Domain\Infrastructure\Strategy;
6
7
8
/**
9
 * Class StrategyFactory
10
 *
11
 * @package Webhook\Domain\Infrastructure\Strategy
12
 */
13
class StrategyFactory
14
{
15
    /**
16
     * @param string $name
17
     * @param array $options
18
     *
19
     * @return StrategyInterface
20
     */
21
    public static function create(string $name, array $options = []): StrategyInterface
22
    {
23
        if (false !== $class = StrategyRegistry::getClassByName($name)) {
24
            /** @var StrategyInterface $instance */
25
            $instance = new $class;
26
            if ($instance instanceof SetOptionsInterface) {
27
                $instance->setOptions($options);
28
            }
29
30
            return $instance;
31
32
        }
33
34
        throw new \RuntimeException('Unsupported strategy name.');
35
    }
36
}