Passed
Branch master (9232c0)
by Johannes
02:26
created

ClassMetadata::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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