Session   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 22
dl 0
loc 89
ccs 20
cts 20
cp 1
rs 10
c 2
b 0
f 0
wmc 7

5 Methods

Rating   Name   Duplication   Size   Complexity  
A checkClassType() 0 7 2
A create() 0 6 1
A checkClassExistence() 0 5 2
A initialize() 0 10 1
A __construct() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * This file is part of slick/http
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Slick\Http;
13
14
use Slick\Http\Session\Driver\NullDriver;
15
use Slick\Http\Session\Driver\ServerDriver;
16
use Slick\Http\Session\Exception\ClassNotFoundException;
17
use Slick\Http\Session\Exception\InvalidDriverClassException;
18
use Slick\Http\Session\SessionDriverInterface;
19
20
/**
21
 * Session factory class
22
 *
23
 * @package Slick\Http
24
*/
25
final class Session
26
{
27
    public const DRIVER_NULL   = NullDriver::class;
28
    public const DRIVER_SERVER = ServerDriver::class;
29
30
    /**
31
     * @var string
32
     */
33
    private string $driverClass;
34
35
    /**
36
     * @var array<string, mixed>
37
     */
38
    private array $options;
39
40
    /**
41
     * Creates a Session factory
42
     *
43
     * @param string $driverClass
44
     * @param array<string, mixed>  $options
45
     */
46
    public function __construct(string $driverClass, array $options = [])
47
    {
48
        $this->driverClass = $driverClass;
49
        $this->options = $options;
50
    }
51
52
    /**
53
     * Creates the session driver with provided options
54 6
     *
55
     * @param string $driverClass
56 6
     * @param array<string, mixed>  $options
57 6
     *
58 6
     * @return SessionDriverInterface
59
     *
60
     * @throws ClassNotFoundException if class does not exists
61
     * @throws InvalidDriverClassException if the class does not implement the SessionDriverInterface
62
     */
63
    public static function create(
64
        string $driverClass = self::DRIVER_SERVER,
65
        array $options = []
66
    ): SessionDriverInterface {
67
        $session = new Session($driverClass, $options);
68
        return $session->initialize();
69 8
    }
70
71 8
    /**
72 6
     * Initializes a new session driver
73 4
     *
74
     * @return SessionDriverInterface
75
     *
76
     * @throws ClassNotFoundException if class does not exists
77
     * @throws InvalidDriverClassException if class does not implement the SessionDriverInterface
78
     */
79
    public function initialize(): SessionDriverInterface
80
    {
81
        $this->checkClassExistence();
82
83
        $this->checkClassType();
84 8
85
        $className = $this->driverClass;
86 8
        /** @var SessionDriverInterface $sessionDriver */
87 2
        $sessionDriver = new $className($this->options);
88 2
        return $sessionDriver;
89 2
    }
90
91
    /**
92 6
     * Checks if the current driver class implements SessionDriverInterface
93
     */
94
    private function checkClassType(): void
95
    {
96
        if (! is_subclass_of($this->driverClass, SessionDriverInterface::class)) {
97
            throw new InvalidDriverClassException(
98
                \sprintf(
99
                    "Session driver classes must implement %s interface.",
100
                    SessionDriverInterface::class
101
                )
102 6
            );
103
        }
104 6
    }
105 2
106 2
    /**
107
     * Check if driver class exists
108 2
     */
109
    private function checkClassExistence(): void
110 4
    {
111
        if (!class_exists($this->driverClass)) {
112
            throw new ClassNotFoundException(
113
                "Session driver class '$this->driverClass'' does not exists."
114
            );
115
        }
116
    }
117
}
118