Completed
Push — master ( 358751...00908b )
by Woody
07:59 queued 01:04
created

SessionConfiguration::apply()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1
Metric Value
dl 0
loc 11
ccs 7
cts 7
cp 1
rs 9.4286
cc 1
eloc 6
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Equip\Configuration;
4
5
use Auryn\Injector;
6
use Equip\Configuration\ConfigurationInterface;
7
use Equip\NativeSession;
8
use Equip\SessionInterface;
9
10
class SessionConfiguration implements ConfigurationInterface
11
{
12
    /**
13
     * @inheritDoc
14
     */
15 1
    public function apply(Injector $injector)
16
    {
17 1
        $injector->share(SessionInterface::class);
18
19 1
        $injector->alias(
20 1
            SessionInterface::class,
21 1
            NativeSession::class
22
        );
23
24 1
        $injector->prepare(NativeSession::class, [$this, 'prepareSession']);
25 1
    }
26
27
    /**
28
     * Initialize the session
29
     *
30
     * @param NativeSession $session
31
     * @param Injector $injector
32
     *
33
     * @return void
34
     */
35 1
    public function prepareSession(NativeSession $session, Injector $injector)
0 ignored issues
show
Unused Code introduced by
The parameter $session is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $injector is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
36
    {
37
        // It seems impossible to test this, since it has to be run in a web
38
        // server to really be useful and PHPUnit will prevent the session
39
        // headers working properly. One possible way to test it is by using
40
        // runkit to mock the [native function][1], but that seems extreme and
41
        // requires installation of a PECL extension.
42
        //
43
        // [1]: https://github.com/tcz/phpunit-mockfunction
0 ignored issues
show
Unused Code Comprehensibility introduced by
86% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
44
        //
45
        // @codeCoverageIgnoreStart
46
        if ($this->canSessionStart() && $this->isCli() === false) {
47
            $this->startSession();
48
        }
49
        // @codeCoverageIgnoreEnd
50 1
    }
51
52
    /**
53
     * Start a new session
54
     *
55
     * @return void
56
     *
57
     * @codeCoverageIgnore
58
     */
59
    protected function startSession()
60
    {
61
        session_start();
62
    }
63
64
    /**
65
     * Check if a session can be started
66
     *
67
     * @return boolean
68
     */
69 1
    private function canSessionStart()
70
    {
71 1
        return session_status() === PHP_SESSION_NONE;
72
    }
73
74
    /**
75
     * Check if we are running in a command line context
76
     *
77
     * @return boolean
78
     */
79 1
    private function isCli()
80
    {
81 1
        return php_sapi_name() === 'cli';
82
    }
83
}
84