Passed
Push — master ( 33103d...13ffe5 )
by Russell
02:55
created

BackendServiceFactory::create()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 11
rs 10
c 0
b 0
f 0
cc 3
nc 2
nop 0
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 PhpTek\Verifiable\Exception\VerifiableBackendException;
13
14
/**
15
 * Constructs and returns the appropriate backend service implementation,
16
 * according to userland config.
17
 */
18
class BackendServiceFactory
19
{
20
    use Configurable;
21
22
    /**
23
     * @return ServiceProvider
24
     * @throws VerifiableBackendException
25
     */
26
    public function create()
27
    {
28
        $backend = strtolower($this->config()->get('backend'));
29
        $gtwClass = sprintf('PhpTek\Verifiable\Backend\%s\Gateway', ucfirst($backend));
30
        $srvClass = sprintf('PhpTek\Verifiable\Backend\%s\Service', ucfirst($backend));
31
32
        if (!class_exists($gtwClass) || !class_exists($srvClass)) {
33
            throw new VerifiableBackendException(sprintf('No backend named %s was found', $srvClass));
34
        }
35
36
        return Injector::inst()->create($srvClass);
37
    }
38
39
}
40