TransferMetadata::unserialize()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 11
cts 11
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 11
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Vox\Webservice\Metadata;
4
5
use Metadata\PropertyMetadata as BasePropertyMetadata;
6
use ReflectionClass;
7
use Vox\Data\Mapping\Exclude;
8
use Vox\Metadata\ClassMetadata;
9
use Vox\Metadata\PropertyMetadata;
10
use Vox\Webservice\Mapping\BelongsTo;
11
use Vox\Webservice\Mapping\HasMany;
12
use Vox\Webservice\Mapping\HasOne;
13
use Vox\Webservice\Mapping\Id;
14
15
/**
16
 * Holds a single transfer metadata information
17
 * 
18
 * @author Jhonatan Teixeira <[email protected]>
19
 */
20
class TransferMetadata extends ClassMetadata
21
{
22
    /**
23
     * @var IdMetadata
24
     */
25
    public $id = [];
26
    
27
    /**
28
     * @var PropertyMetadata[]
29
     */
30
    public $associations = [];
31
32 51
    public function __construct($name)
33
    {
34 51
        parent::__construct($name);
35
36 51
        $this->id = new IdMetadata();
37 51
    }
38
39 51
    public function addPropertyMetadata(BasePropertyMetadata $metadata)
40
    {
41 51
        if ($metadata instanceof PropertyMetadata) {
42 51
            if ($id = $metadata->getAnnotation(Id::class)) {
0 ignored issues
show
Unused Code introduced by
The assignment to $id is dead and can be removed.
Loading history...
43 38
                $this->id->append($metadata);
44
            }
45
        }
46
        
47 51
        parent::addPropertyMetadata($metadata);
48
        
49 51
        if ($metadata->hasAnnotation(BelongsTo::class) && !$metadata->hasAnnotation(Exclude::class)) {
0 ignored issues
show
introduced by
The method hasAnnotation() does not exist on Metadata\PropertyMetadata. Are you sure you never get this type here, but always one of the subclasses? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

49
        if ($metadata->/** @scrutinizer ignore-call */ hasAnnotation(BelongsTo::class) && !$metadata->hasAnnotation(Exclude::class)) {
Loading history...
50 11
            $metadata->annotations[Exclude::class] = new Exclude();
51 11
            $metadata->annotations[Exclude::class]->output = false;
52
        }
53
        
54 51
        if ($metadata->hasAnnotation(BelongsTo::class)
55 51
            || $metadata->hasAnnotation(HasOne::class)
56 51
            || $metadata->hasAnnotation(HasMany::class)) {
57 16
            $this->associations[$metadata->name] = $metadata;
58
        }
59
        
60 51
        return $this;
61
    }
62
    
63
    /**
64
     * @param string $name
65
     * @return PropertyMetadata
66
     */
67
    public function getAssociation(string $name)
68
    {
69
        return $this->associations[$name] ?? null;
70
    }
71
    
72 5
    public function serialize()
73
    {
74 5
        return serialize(array(
75 5
            $this->name,
76 5
            $this->methodMetadata,
77 5
            $this->propertyMetadata,
78 5
            $this->fileResources,
79 5
            $this->createdAt,
80 5
            $this->annotations,
81 5
            $this->id,
82 5
            $this->associations,
83
        ));
84
    }
85
86 3
    public function unserialize($str)
87
    {
88
        list(
89 3
            $this->name,
90 3
            $this->methodMetadata,
91 3
            $this->propertyMetadata,
92 3
            $this->fileResources,
93 3
            $this->createdAt,
94 3
            $this->annotations,
95 3
            $this->id,
96 3
            $this->associations
97 3
        ) = unserialize($str);
98
99 3
        $this->reflection = new ReflectionClass($this->name);
100 3
    }
101
}
102