KeyValueUtility::getOrSet()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 2
dl 0
loc 9
rs 10
c 0
b 0
f 0
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