Observe   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
eloc 8
c 1
b 0
f 0
dl 0
loc 38
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A observe() 0 3 1
A hydrate() 0 3 1
A __construct() 0 6 1
A hydrating() 0 5 1
1
<?php
2
declare(strict_types=1);
3
4
namespace Stratadox\Hydrator;
5
6
/**
7
 * Base class for decorating hydrators with observable functionality.
8
 *
9
 * @author Stratadox
10
 */
11
abstract class Observe implements Hydrator
12
{
13
    /** @var Hydrator */
14
    private $hydrator;
15
    /** @var HydrationObserver */
16
    private $observer;
17
18
    final private function __construct(
19
        Hydrator $hydrator,
20
        HydrationObserver $observer
21
    ) {
22
        $this->hydrator = $hydrator;
23
        $this->observer = $observer;
24
    }
25
26
    /**
27
     * Attaches an observer to a hydrator.
28
     *
29
     * @param Hydrator          $hydrator The hydrator to observe.
30
     * @param HydrationObserver $observer The observer to attach.
31
     * @return Hydrator                   The decorated hydrator.
32
     */
33
    public static function hydrating(
34
        Hydrator $hydrator,
35
        HydrationObserver $observer
36
    ): Hydrator {
37
        return new static($hydrator, $observer);
38
    }
39
40
    /** @throws HydrationFailure */
41
    final protected function hydrate(object $target, array $input): void
42
    {
43
        $this->hydrator->writeTo($target, $input);
44
    }
45
46
    final protected function observe(object $target, array $input): void
47
    {
48
        $this->observer->hydrating($target, $input);
49
    }
50
}
51