Resource::hydrate()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 10
rs 9.2
cc 4
eloc 5
nc 4
nop 1
1
<?php
2
3
namespace Shrikeh\PagerDuty\Hydrator\ContactMethod;
4
5
use stdClass;
6
use IteratorIterator;
7
use Shrikeh\PagerDuty\Hydrator;
8
use Shrikeh\PagerDuty\Collection;
9
use Shrikeh\Collection\NamedConstructorsTrait;
10
use Shrikeh\Collection\ObjectStorageTrait;
11
use Shrikeh\Collection\ImmutableCollectionTrait;
12
use Shrikeh\Collection\ClosedOuterIteratorTrait;
13
use Shrikeh\Collection\OuterIteratorTrait;
14
use Shrikeh\PagerDuty\Collection\Traits\ThrowImmutable;
15
16
class Resource extends IteratorIterator implements Collection, Hydrator
17
{
18
    use NamedConstructorsTrait;
19
    use ObjectStorageTrait;
20
    use ImmutableCollectionTrait;
21
    use ClosedOuterIteratorTrait;
22
    use OuterIteratorTrait;
23
    use ThrowImmutable {
24
        ThrowImmutable::throwImmutable insteadof ImmutableCollectionTrait;
25
    }
26
27
    public function hydrate(stdClass $dto)
28
    {
29
        if (isset($dto->type)) {
30
            foreach ($this->getStorage() as $hydrator) {
31
                if ($hydrator->supports($dto->type)) {
32
                    return $hydrator->hydrate($dto);
33
                }
34
            }
35
        }
36
    }
37
38
    public function supports($token)
39
    {
40
        return 'type' === $token;
41
    }
42
43
    protected function append(Hydrator $hydrator)
44
    {
45
        $this->getStorage()->attach($hydrator);
46
    }
47
}
48