KeyValueUtility   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 18
Duplicated Lines 0 %

Importance

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

1 Method

Rating   Name   Duplication   Size   Complexity  
A getOrSet() 0 9 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Lit\Nexus\Utilities;
6
7
use Lit\Nexus\Interfaces\SingleValueInterface;
8
9
/**
10
 * Utilities about KeyValueInterface
11
 */
12
class KeyValueUtility
13
{
14
    /**
15
     * Get the value from SingleValueInterface, or call $compute callback and write to it.
16
     *
17
     * @param SingleValueInterface $store   The storage.
18
     * @param callable             $compute The method to create the value.
19
     * @return mixed The value.
20
     */
21
    public static function getOrSet(SingleValueInterface $store, callable $compute)
22
    {
23
        if ($store->exists()) {
24
            return $store->get();
25
        }
26
        $value = call_user_func($compute);
27
        $store->set($value);
28
29
        return $value;
30
    }
31
}
32