Completed
Push — 2.0 ( 28c56a...ffb373 )
by Paweł
18:23 queued 09:44
created

PackageHydrator   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 0
Metric Value
wmc 3
lcom 0
cbo 3
dl 0
loc 29
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A hydrate() 0 26 3
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Superdesk Publisher Core Bundle.
7
 *
8
 * Copyright 2019 Sourcefabric z.ú. and contributors.
9
 *
10
 * For the full copyright and license information, please see the
11
 * AUTHORS and LICENSE files distributed with this source code.
12
 *
13
 * @copyright 2019 Sourcefabric z.ú
14
 * @license http://www.superdesk.org/license
15
 */
16
17
namespace SWP\Bundle\CoreBundle\Hydrator;
18
19
use DateTime;
20
use GeneratedHydrator\Configuration;
21
use SWP\Bundle\CoreBundle\Model\Package;
22
use SWP\Bundle\CoreBundle\Model\PackageInterface;
23
24
final class PackageHydrator implements PackageHydratorInterface
25
{
26
    public function hydrate(PackageInterface $newPackage, PackageInterface $existingPackage): PackageInterface
27
    {
28
        $config = new Configuration(Package::class);
29
        $hydratorClass = $config->createFactory()->getHydratorClass();
30
        $hydrator = new $hydratorClass();
31
32
        $newPackage->setId($existingPackage->getId());
33
        $newPackage->setCreatedAt($existingPackage->getCreatedAt());
34
        $newPackage->setUpdatedAt(new DateTime());
35
        $newPackage->setStatus($existingPackage->getStatus());
36
37
        $groups = $newPackage->getGroups()->toArray();
38
        foreach ($newPackage->getGroups() as $group) {
39
            $newPackage->removeGroup($group);
40
        }
41
42
        $result = $hydrator->extract($newPackage);
43
        $hydrator->hydrate($result, $existingPackage);
44
        $existingPackage->setUpdatedAt(new DateTime());
45
46
        foreach ($groups as $group) {
47
            $existingPackage->addGroup($group);
48
        }
49
50
        return $existingPackage;
51
    }
52
}
53