Completed
Push — master ( 2c98d6...660a35 )
by max
02:57
created

TableFactory   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 6
c 1
b 0
f 1
lcom 0
cbo 3
dl 0
loc 31
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B createService() 0 28 6
1
<?php
2
3
namespace T4web\Authentication\Adapter;
4
5
use Zend\ServiceManager\FactoryInterface;
6
use Zend\ServiceManager\ServiceLocatorInterface;
7
use Zend\Authentication\Adapter\DbTable\CallbackCheckAdapter as AuthenticationAdapter;
8
use Zend\Db\Adapter\Adapter as DbAdapter;
9
use T4web\Authentication\Exception\RuntimeException;
10
use T4webInfrastructure\Config;
11
12
class TableFactory implements FactoryInterface
13
{
14
    public function createService(ServiceLocatorInterface $serviceLocator)
15
    {
16
        /** @var DbAdapter $dbAdapter */
17
        $dbAdapter = $serviceLocator->get(DbAdapter::class);
18
19
        /** @var Config $userInfrastructureConfig */
20
        $config = $serviceLocator->get('config');
21
22
        if (!isset($config['auth']) || !isset($config['auth']['table-adapter'])) {
23
            throw new RuntimeException('For authentication with table adapter, you must describe config[auth][table-adapter]');
24
        }
25
        if (!isset($config['auth']['table-adapter']['table-name'])
26
            || !isset($config['auth']['table-adapter']['identity-column'])
27
            || !isset($config['auth']['table-adapter']['credential-column'])) {
28
            throw new RuntimeException('For authentication with table adapter, you must describe table-name, '
29
                . 'identity-column and credential-column keys in config[auth][table-adapter]');
30
        }
31
32
        $tableName = $config['auth']['table-adapter']['table-name'];
33
        $identityColumn = $config['auth']['table-adapter']['identity-column'];
34
        $credentialColumn = $config['auth']['table-adapter']['credential-column'];
35
36
        $credentialCallback = function ($passwordInDatabase, $passwordProvided) {
37
            return password_verify($passwordProvided, $passwordInDatabase);
38
        };
39
40
        return new AuthenticationAdapter($dbAdapter, $tableName, $identityColumn, $credentialColumn, $credentialCallback);
41
    }
42
}
43