Passed
Pull Request — master (#190)
by Arman
02:55
created

SessionFactory   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 16
c 1
b 0
f 0
dl 0
loc 65
rs 10
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getAdapterClass() 0 7 2
A get() 0 7 2
A createInstance() 0 11 2
1
<?php
2
3
/**
4
 * Quantum PHP Framework
5
 *
6
 * An open source software development framework for PHP
7
 *
8
 * @package Quantum
9
 * @author Arman Ag. <[email protected]>
10
 * @copyright Copyright (c) 2018 Softberg LLC (https://softberg.org)
11
 * @link http://quantum.softberg.org/
12
 * @since 2.9.5
13
 */
14
15
namespace Quantum\Libraries\Session\Factories;
16
17
use Quantum\Libraries\Session\Adapters\Database\DatabaseSessionAdapter;
18
use Quantum\Libraries\Session\Adapters\Native\NativeSessionAdapter;
19
use Quantum\Libraries\Session\Exceptions\SessionException;
20
use Quantum\Libraries\Config\Exceptions\ConfigException;
21
use Quantum\Libraries\Session\Session;
22
use Quantum\Di\Exceptions\DiException;
23
use Quantum\Exceptions\BaseException;
24
use Quantum\Loader\Setup;
25
use ReflectionException;
26
27
/**
28
 * Class SessionFactory
29
 * @package Quantum\Libraries\Session
30
 */
31
class SessionFactory
32
{
33
34
    /**
35
     * Supported adapters
36
     */
37
    const ADAPTERS = [
38
        Session::NATIVE => NativeSessionAdapter::class,
39
        Session::DATABASE => DatabaseSessionAdapter::class,
40
    ];
41
42
    /**
43
     * @var Session|null
44
     */
45
    private static $instance = null;
46
47
    /**
48
     * @param array|null $storage
49
     * @return Session
50
     * @throws BaseException
51
     * @throws ConfigException
52
     * @throws DiException
53
     * @throws ReflectionException
54
     */
55
    public static function get(?array $storage = null): Session
56
    {
57
        if (self::$instance === null) {
58
            return self::$instance = self::createInstance($storage);
0 ignored issues
show
Unused Code introduced by
The call to Quantum\Libraries\Sessio...ctory::createInstance() has too many arguments starting with $storage. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

58
            return self::$instance = self::/** @scrutinizer ignore-call */ createInstance($storage);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
59
        }
60
61
        return self::$instance;
62
    }
63
64
    /**
65
     * @return Session
66
     * @throws BaseException
67
     * @throws ConfigException
68
     * @throws DiException
69
     * @throws ReflectionException
70
     */
71
    private static function createInstance(): Session
72
    {
73
        if (!config()->has('session')) {
74
            config()->import(new Setup('Config', 'session'));
75
        }
76
77
        $adapter = config()->get('session.current');
78
79
        $adapterClass = self::getAdapterClass($adapter);
0 ignored issues
show
Bug introduced by
It seems like $adapter can also be of type null; however, parameter $adapter of Quantum\Libraries\Sessio...tory::getAdapterClass() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

79
        $adapterClass = self::getAdapterClass(/** @scrutinizer ignore-type */ $adapter);
Loading history...
80
81
        return new Session(new $adapterClass(config()->get('session.' . $adapter)));
82
    }
83
84
    /**
85
     * @param string $adapter
86
     * @return string
87
     * @throws BaseException
88
     */
89
    private static function getAdapterClass(string $adapter): string
90
    {
91
        if (!array_key_exists($adapter, self::ADAPTERS)) {
92
            throw SessionException::adapterNotSupported($adapter);
93
        }
94
95
        return self::ADAPTERS[$adapter];
96
    }
97
}