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

TableFactory::createService()   B

Complexity

Conditions 6
Paths 3

Size

Total Lines 28
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 28
rs 8.439
cc 6
eloc 16
nc 3
nop 1
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