Passed
Push — master ( 3c5f34...4a3ec6 )
by Asmir
01:27 queued 50s
created

ClassMetadata::unserializeFromArray()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 1
dl 0
loc 9
ccs 1
cts 1
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Metadata;
6
7
/**
8
 * Base class for class metadata.
9
 *
10
 * This class is intended to be extended to add your own application specific
11
 * properties, and flags.
12
 *
13
 * @author Johannes M. Schmitt <[email protected]>
14
 */
15
class ClassMetadata implements \Serializable
16
{
17
    use SerializationHelper;
18
19
    /**
20
     * @var string
21
     */
22
    public $name;
23
24
    /**
25
     * @var MethodMetadata[]
26
     */
27
    public $methodMetadata = [];
28
29
    /**
30
     * @var PropertyMetadata[]
31
     */
32
    public $propertyMetadata = [];
33
34
    /**
35
     * @var string[]
36
     */
37
    public $fileResources = [];
38
39
    /**
40
     * @var int
41
     */
42 20
    public $createdAt;
43
44 20
    public function __construct(string $name)
45 20
    {
46 20
        $this->name = $name;
47
        $this->createdAt = time();
48
    }
49
50
    public function addMethodMetadata(MethodMetadata $metadata): void
51
    {
52
        $this->methodMetadata[$metadata->name] = $metadata;
53
    }
54
55
    public function addPropertyMetadata(PropertyMetadata $metadata): void
56
    {
57
        $this->propertyMetadata[$metadata->name] = $metadata;
58 1
    }
59
60 1
    public function isFresh(?int $timestamp = null): bool
61 1
    {
62
        if (null === $timestamp) {
63
            $timestamp = $this->createdAt;
64 1
        }
65 1
66
        foreach ($this->fileResources as $filepath) {
67
            if (!file_exists($filepath)) {
68
                return false;
69 1
            }
70 1
71
            if ($timestamp < filemtime($filepath)) {
72
                return false;
73
            }
74 1
        }
75
76
        return true;
77
    }
78
79
    protected function serializeToArray(): array
80
    {
81
        return [
82
            $this->name,
83 5
            $this->methodMetadata,
84
            $this->propertyMetadata,
85 5
            $this->fileResources,
86 5
            $this->createdAt,
87 5
        ];
88 5
    }
89 5
90 5
    protected function unserializeFromArray(array $data): void
91
    {
92
        [
93
            $this->name,
94
            $this->methodMetadata,
95
            $this->propertyMetadata,
96
            $this->fileResources,
97
            $this->createdAt,
98
        ] = $data;
99
    }
100
}
101