Completed
Push — master ( 2aa650...c3039e )
by Jesse
01:58
created

PropertyUpdater   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Importance

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

3 Methods

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