Completed
Push — master ( 6b97e6...bd2514 )
by Paweł
16:56 queued 08:16
created

PackageHydrator   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 3
dl 0
loc 41
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A hydrate() 0 31 4
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
    private $generatedClassesTargetDir;
27
28
    public function __construct(string $generatedClassesTargetDir)
29
    {
30
        $this->generatedClassesTargetDir = $generatedClassesTargetDir;
31
    }
32
33
    public function hydrate(PackageInterface $newPackage, PackageInterface $existingPackage): PackageInterface
34
    {
35
        $config = new Configuration(Package::class);
36
        $config->setGeneratedClassesTargetDir($this->generatedClassesTargetDir);
37
        $hydratorClass = $config->createFactory()->getHydratorClass();
38
        $hydrator = new $hydratorClass();
39
40
        $newPackage->setCreatedAt($existingPackage->getCreatedAt());
41
        $newPackage->setUpdatedAt(new DateTime());
42
        $newPackage->setStatus($existingPackage->getStatus());
43
44
        if (null !== $newPackage->getId()) {
45
            return $existingPackage;
46
        }
47
48
        $newPackage->setId($existingPackage->getId());
49
50
        // set package when the item or group is newly added
51
        foreach ($newPackage->getGroups() as $group) {
52
            $group->setPackage($existingPackage);
53
        }
54
55
        foreach ($newPackage->getItems() as $item) {
56
            $item->setPackage($existingPackage);
57
        }
58
59
        $result = $hydrator->extract($newPackage);
60
        $hydrator->hydrate($result, $existingPackage);
61
62
        return $existingPackage;
63
    }
64
}
65