BackendServiceFactory   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 21
Duplicated Lines 0 %

Importance

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

1 Method

Rating   Name   Duplication   Size   Complexity  
A create() 0 11 3
1
<?php
2
3
/**
4
 * @author  Russell Michell 2018 <[email protected]>
5
 * @package silverstripe-verifiable
6
 */
7
8
namespace PhpTek\Verifiable\Backend;
9
10
use SilverStripe\Core\Config\Configurable;
11
use SilverStripe\Core\Injector\Injector;
12
use SilverStripe\Core\Injector\Factory;
13
use PhpTek\Verifiable\Backend\ServiceProvider;
14
use PhpTek\Verifiable\Exception\VerifiableBackendException;
15
16
/**
17
 * Constructs and returns the appropriate backend service implementation,
18
 * according to userland config.
19
 */
20
class BackendServiceFactory implements Factory
21
{
22
    use Configurable;
23
24
    /**
25
     * @param  string $service
26
     * @param  array $params
27
     * @return ServiceProvider
28
     * @throws VerifiableBackendException
29
     */
30
    public function create($service, array $params = []) : ServiceProvider
31
    {
32
        $backend = strtolower($this->config()->get('backend'));
33
        $gtwClass = sprintf('PhpTek\Verifiable\Backend\%s\Gateway', ucfirst($backend));
34
        $srvClass = sprintf('PhpTek\Verifiable\Backend\%s\Service', ucfirst($backend));
35
36
        if (!class_exists($gtwClass) || !class_exists($srvClass)) {
37
            throw new VerifiableBackendException(sprintf('No backend named %s was found', $srvClass));
38
        }
39
40
        return Injector::inst()->create($srvClass);
41
    }
42
}
43