GlobalStorage   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 6
eloc 10
dl 0
loc 35
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A initGlobal() 0 6 2
A fromGlobal() 0 2 1
A toGlobal() 0 5 3
1
<?php
2
3
namespace evelikto\di\storage;
4
use evelikto\di\Bean;
5
use evelikto\di\scope\GlobalScope;
6
7
/** Activates the global scope. Factory 'diGlobalStorage' must be defined in the config class */
8
trait GlobalStorage
9
{
10
    /** @var OfflineStorageAdapter $globalStorage Scope storage implementation */
11
    private $globalStorage;
12
13
    /** Init the scope storage. Get the underlying storage implementation from factory method */
14
    protected function initGlobal() {
15
        $impl = $this->fromMethod(self::DI_GLOBAL_STORAGE_KEY);
0 ignored issues
show
Bug introduced by
The constant evelikto\di\storage\Glob...::DI_GLOBAL_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_GLOBAL_STORAGE_KEY);
Loading history...
16
        if ($impl === null)
17
            throw new StorageFactoryNotDefined(self::DI_SESSION_STORAGE_KEY);
0 ignored issues
show
Bug introduced by
The constant evelikto\di\storage\Glob...:DI_SESSION_STORAGE_KEY was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
18
19
        $this->globalStorage = 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 fromGlobal(string $name) {
29
        return $this->globalStorage->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 toGlobal(Bean $bean) {
39
        if ($bean->scope === Bean::SCOPE_GLOBAL || $bean->value instanceof GlobalScope)
40
            return $this->globalStorage->store($bean);
41
42
        return null;
43
    }
44
}