PropertyUpdater   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 4
dl 0
loc 33
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A for() 0 7 1
A updateWith() 0 5 1
A __construct() 0 10 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stratadox\Hydration\Proxying;
6
7
use Closure;
8
use Stratadox\Hydration\UpdatesTheProxyOwner;
9
10
/**
11
 * Updates a property with the newly loaded value.
12
 *
13
 * @package Stratadox\Hydrate
14
 * @author Stratadox
15
 */
16
class PropertyUpdater implements UpdatesTheProxyOwner
17
{
18
    private $owner;
19
    private $propertyShouldReference;
20
    private $setter;
21
22
    public function __construct(
23
        $theOwner,
24
        string $theProperty,
25
        Closure $setter = null
26
    ) {
27
        $this->owner = $theOwner;
28
        $this->propertyShouldReference = $theProperty;
29
        $this->setter = $setter ?: function (string $property, $value) : void
30
        {
31
            $this->$property = $value;
32
        };
33
    }
34
35
    public static function for(
36
        $theOwner,
37
        string $ofTheProperty,
38
        Closure $setter = null
39
    ) : UpdatesTheProxyOwner
40
    {
41
        return new static($theOwner, $ofTheProperty, $setter);
42
    }
43
44
    public function updateWith($theLoadedInstance) : void
45
    {
46
        $this->setter->call($this->owner,
47
            $this->propertyShouldReference,
48
            $theLoadedInstance
49
        );
50
    }
51
}
52