Issues (49)

src/Data/DataTransferGateway.php (2 issues)

1
<?php
2
3
namespace Vox\Data;
4
5
use Metadata\MetadataFactoryInterface;
6
use Vox\Metadata\ClassMetadata;
7
8
/**
9
 * transfer data from one object to another
10
 * 
11
 * @author Jhonatan Teixeira <[email protected]>
12
 */
13
class DataTransferGateway implements DataTransferGatewayInterface
14
{
15
    /**
16
     * @var ObjectGraphBuilderInterface
17
     */
18
    private $objectGraphBuilder;
19
    
20
    /**
21
     * @var MetadataFactoryInterface
22
     */
23
    private $metadataFactory;
24
    
25
    /**
26
     * @var PropertyAccessorInterface
27
     */
28
    private $propertyAccessor;
29
    
30 2
    public function __construct(
31
        ObjectGraphBuilderInterface $objectGraphBuilder,
32
        MetadataFactoryInterface $metadataFactory,
33
        PropertyAccessorInterface $propertyAccessor
34
    ) {
35 2
        $this->objectGraphBuilder = $objectGraphBuilder;
36 2
        $this->metadataFactory    = $metadataFactory;
37 2
        $this->propertyAccessor   = $propertyAccessor;
38 2
    }
39
    
40 1
    public function transferDataTo($fromObject, $toObject)
41
    {
42 1
        $this->objectGraphBuilder->clear();
43
        
44 1
        $toObject     = $this->objectGraphBuilder->buildObjectGraph($toObject);
45 1
        $metadataFrom = $this->getObjectMetadata($fromObject);
46 1
        $metadataTo   = $this->getObjectMetadata($toObject);
0 ignored issues
show
The assignment to $metadataTo is dead and can be removed.
Loading history...
47
        
48
        /* @var $propertyMetadata \Vox\Metadata\PropertyMetadata */
49 1
        foreach ($metadataFrom->propertyMetadata as $propertyMetadata) {
50 1
            $bindings = $propertyMetadata->getAnnotation(Mapping\Bindings::class);
51 1
            $target   = $bindings->target ?? $propertyMetadata->name;
52
            
53 1
            $targetValue = $this->propertyAccessor->get($toObject, $target);
54
            
55 1
            if (is_object($targetValue)) {
56 1
                $this->transferDataTo($propertyMetadata->getValue($fromObject), $targetValue);
57 1
                continue;
58
            }
59
            
60 1
            $this->propertyAccessor->set($toObject, $target, $propertyMetadata->getValue($fromObject));
61
        }
62
        
63 1
        return $toObject;
64
    }
65
    
66 1
    public function transferDataFrom($fromObject, $toObject)
67
    {
68 1
        $this->objectGraphBuilder->clear();
69
        
70 1
        $toObject     = $this->objectGraphBuilder->buildObjectGraph($toObject);
71 1
        $metadataTo   = $this->getObjectMetadata($toObject);
72
        
73
        /* @var $propertyMetadata \Vox\Metadata\PropertyMetadata */
74 1
        foreach ($metadataTo->propertyMetadata as $propertyMetadata) {
75 1
            $bindings = $propertyMetadata->getAnnotation(Mapping\Bindings::class);
76 1
            $source   = $bindings->source ?? $propertyMetadata->name;
77 1
            $type     = $propertyMetadata->type;
78
            
79 1
            if (class_exists($type)) {
80 1
                $this->transferDataFrom($fromObject, $propertyMetadata->getValue($toObject));
81 1
                continue;
82
            }
83
84 1
            $sourceValue = $this->propertyAccessor->get($fromObject, $source);
85 1
            $propertyMetadata->setValue($toObject, $sourceValue);
86
        }
87
        
88 1
        return $toObject;
89
    }
90
    
91 2
    private function getObjectMetadata($class): ClassMetadata
92
    {
93 2
        if (is_object($class)) {
94 2
            $class = get_class($class);
95
        }
96
        
97 2
        return $this->metadataFactory->getMetadataForClass($class);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->metadataFa...etadataForClass($class) returns the type null|Metadata\ClassHierarchyMetadata which is incompatible with the type-hinted return Vox\Metadata\ClassMetadata.
Loading history...
98
    }
99
}
100