SessionStorage::initSession()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
1
<?php
2
3
namespace evelikto\di\storage;
4
use evelikto\di\Bean;
5
use evelikto\di\scope\SessionScope;
6
7
/** Activates the session scope. */
8
trait SessionStorage
9
{
10
    /** @var OfflineStorageAdapter $sessionStorage Scope storage implementation */
11
    private $sessionStorage;
12
13
    /** Init the scope storage. Get the underlying storage implementation from factory method */
14
    protected function initSession() {
15
        $impl = $this->fromMethod(self::DI_SESSION_STORAGE_KEY);
0 ignored issues
show
Bug introduced by
The constant evelikto\di\storage\Sess...:DI_SESSION_STORAGE_KEY was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
Bug introduced by
It seems like fromMethod() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

15
        /** @scrutinizer ignore-call */ 
16
        $impl = $this->fromMethod(self::DI_SESSION_STORAGE_KEY);
Loading history...
16
        if ($impl === null)
17
            throw new StorageFactoryNotDefined(self::DI_SESSION_STORAGE_KEY);
18
19
        $this->sessionStorage = new OfflineStorageAdapter($impl->value);
20
    }
21
22
    /**
23
     * Try to read the requested dependency from the storage
24
     *
25
     * @param   string  $name  Dependency name.
26
     * @return  mixed          Retrieved value or null if not previously stored.
27
     */
28
    protected function fromSession(string $name) {
29
        return $this->sessionStorage->fetch($name);
30
    }
31
32
    /**
33
     * Save the dependency to the storage.
34
     *
35
     * @param   Bean    $bean  Dependency to be stored.
36
     * @return  mixed          Stored value from the supplied bean.
37
     */
38
    protected function toSession(Bean $bean) {
39
        if ($bean->scope === Bean::SCOPE_SESSION || $bean->value instanceof SessionScope)
40
            return $this->sessionStorage->store($bean);
41
42
        return null;
43
    }
44
}