Passed
Push — master ( 3c979b...72c793 )
by Kirill
03:02
created

SessionFactory   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 17
c 1
b 0
f 0
dl 0
loc 46
rs 10
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A initSession() 0 21 4
1
<?php
2
3
/**
4
 * Spiral Framework.
5
 *
6
 * @license   MIT
7
 * @author    Anton Titov (Wolfy-J)
8
 */
9
10
declare(strict_types=1);
11
12
namespace Spiral\Session;
13
14
use Psr\Container\ContainerExceptionInterface;
15
use Spiral\Core\Container\SingletonInterface;
16
use Spiral\Core\FactoryInterface;
17
use Spiral\Session\Config\SessionConfig;
18
use Spiral\Session\Exception\MultipleSessionException;
19
use Spiral\Session\Exception\SessionException;
20
21
/**
22
 * Initiates session instance and configures session handlers.
23
 */
24
final class SessionFactory implements SingletonInterface
25
{
26
    /** @var SessionConfig */
27
    private $config;
28
29
    /** @var FactoryInterface */
30
    private $factory;
31
32
    /**
33
     * @param SessionConfig    $config
34
     * @param FactoryInterface $factory
35
     */
36
    public function __construct(SessionConfig $config, FactoryInterface $factory)
37
    {
38
        $this->config = $config;
39
        $this->factory = $factory;
40
    }
41
42
    /**
43
     * @param string      $clientSignature User specific token, does not provide full security but
44
     *                                     hardens session transfer.
45
     * @param string|null $id              When null - expect php to create session automatically.
46
     * @return SessionInterface
47
     *
48
     */
49
    public function initSession(string $clientSignature, string $id = null): SessionInterface
50
    {
51
        if ((int)session_status() === PHP_SESSION_ACTIVE) {
52
            throw new MultipleSessionException('Unable to initiate session, session already started');
53
        }
54
55
        //Initiating proper session handler
56
        if ($this->config->getHandler() !== null) {
57
            try {
58
                $handler = $this->config->getHandler()->resolve($this->factory);
59
            } catch (\Throwable | ContainerExceptionInterface $e) {
60
                throw new SessionException($e->getMessage(), $e->getCode(), $e);
0 ignored issues
show
Bug introduced by
The method getCode() does not exist on Psr\Container\ContainerExceptionInterface. It seems like you code against a sub-type of said class. However, the method does not exist in Psr\Container\NotFoundExceptionInterface. Are you sure you never get one of those? ( Ignorable by Annotation )

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

60
                throw new SessionException($e->getMessage(), $e->/** @scrutinizer ignore-call */ getCode(), $e);
Loading history...
Bug introduced by
The method getMessage() does not exist on Psr\Container\ContainerExceptionInterface. It seems like you code against a sub-type of said class. However, the method does not exist in Psr\Container\NotFoundExceptionInterface. Are you sure you never get one of those? ( Ignorable by Annotation )

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

60
                throw new SessionException($e->/** @scrutinizer ignore-call */ getMessage(), $e->getCode(), $e);
Loading history...
61
            }
62
63
            session_set_save_handler($handler, true);
0 ignored issues
show
Bug introduced by
It seems like $handler can also be of type null; however, parameter $session_handler of session_set_save_handler() does only seem to accept SessionHandlerInterface, 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

63
            session_set_save_handler(/** @scrutinizer ignore-type */ $handler, true);
Loading history...
64
        }
65
66
        return $this->factory->make(Session::class, [
67
            'clientSignature' => $clientSignature,
68
            'lifetime'        => $this->config->getLifetime(),
69
            'id'              => $id
70
        ]);
71
    }
72
}
73