Session::getClassName()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 8
ccs 5
cts 5
cp 1
rs 9.4285
cc 2
eloc 5
nc 2
nop 1
crap 2
1
<?php
2
3
/**
4
 * This file is part of slick/session package
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
namespace Slick\Session;
11
12
use Slick\Common\Base;
13
use Slick\Session\Driver\Server;
14
use Slick\Session\Exception\DriverClassNotFoundException;
15
use Slick\Session\Exception\InvalidSessionDriverClassException;
16
17
/**
18
 * Session session factory
19
 *
20
 * @package Slick\Session
21
 * @author  Filipe Silva <[email protected]>
22
 */
23
final class Session extends Base
24
{
25
26
    /**
27
     * @readwrite
28
     * @var string FQ driver class name or alias
29
     */
30
    protected $driver = 'server';
31
32
    /**
33
     * @readwrite
34
     * @var array Driver initialization options
35
     */
36
    protected $options = [];
37
38
    /**
39
     * @var array List of known session drivers
40
     */
41
    private static $knownDrivers = [
42
        'server' => Server::class
43
    ];
44
45
    /**
46
     * Sets a custom driver class to session factory
47
     * 
48
     * @param string $alias
49
     * @param string $className
50
     */
51 4
    public static function addClass($alias, $className)
52
    {
53 4
        self::$knownDrivers[$alias] = self::checkClass($className);
54 2
    }
55
56
    /**
57
     * Creates a session driver with  provided options
58
     * 
59
     * @param array $options
60
     * @return SessionDriverInterface
61
     */
62 6
    public static function create(array $options = [])
63
    {
64 6
        $factory = new static($options);
65 6
        return $factory->getDriver();
66
    }
67
68
    /**
69
     * Creates the driver for current 
70
     * 
71
     * @return SessionDriverInterface
72
     */
73 6
    public function getDriver()
74
    {
75 6
        $className = $this->getClassName($this->driver);
76 4
        return new $className($this->options);
77
    }
78
79
    /**
80
     * Verifies and returns the FQ session driver class name
81
     * 
82
     * @param string $alias
83
     * @return string
84
     */
85 6
    private function getClassName($alias)
86
    {
87 6
        $className = array_key_exists($alias, self::$knownDrivers)
88 6
            ? self::$knownDrivers[$alias]
89 6
            : self::checkClass($alias);
90
        
91 4
        return $className;
92
    }
93
94
    /**
95
     * Check if provided class exists and implements SessionDriverInterface
96
     * 
97
     * @param string $className
98
     * 
99
     * @return string
100
     */
101 6
    private static function checkClass($className)
102
    {
103 6
        if (! class_exists($className)) {
104 2
            throw new DriverClassNotFoundException(
105 2
                "The session driver class {$className} was not found."
106 2
            );
107
        }
108
109 4
        if (! is_subclass_of($className, SessionDriverInterface::class)) {
110 2
            $itf = SessionDriverInterface::class;
111 2
            throw new InvalidSessionDriverClassException(
112 2
                "The class provided does not implements the '{$itf}' interface"
113 2
            );
114
        }
115
        
116 2
        return $className;
117
    }
118
}