Completed
Pull Request — master (#180)
by Gorka
05:29
created

OrganizationDTODataTransformer   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A write() 0 4 1
B read() 0 28 4
1
<?php
2
3
/*
4
 * This file is part of the Kreta package.
5
 *
6
 * (c) Beñat Espiña <[email protected]>
7
 * (c) Gorka Laucirica <[email protected]>
8
 *
9
 * For the full copyright and license information, please view the LICENSE
10
 * file that was distributed with this source code.
11
 */
12
13
declare(strict_types=1);
14
15
namespace Kreta\TaskManager\Application\DataTransformer\Organization;
16
17
use Kreta\TaskManager\Domain\Model\Organization\Organization;
18
19
class OrganizationDTODataTransformer implements OrganizationDataTransformer
20
{
21
    private $organization;
22
    private $memberDataTransformer;
23
24
    public function __construct(MemberDataTransformer $memberDataTransformer)
25
    {
26
        $this->memberDataTransformer = $memberDataTransformer;
27
    }
28
29
    public function write(Organization $organization)
30
    {
31
        $this->organization = $organization;
32
    }
33
34
    public function read()
35
    {
36
        if (!$this->organization instanceof Organization) {
37
            return [];
38
        }
39
40
        $owners = [];
41
        foreach ($this->organization->owners() as $owner) {
42
            $this->memberDataTransformer->write($owner);
43
            $owners[] = $this->memberDataTransformer->read();
44
        }
45
46
        $organizationMembers = [];
47
        foreach ($this->organization->organizationMembers() as $organizationMember) {
48
            $this->memberDataTransformer->write($organizationMember);
49
            $organizationMembers[] = $this->memberDataTransformer->read();
50
        }
51
52
        return [
53
            'id'                  => $this->organization->id()->id(),
54
            'name'                => $this->organization->name()->name(),
55
            'slug'                => $this->organization->slug()->slug(),
56
            'created_on'          => $this->organization->createdOn()->format('Y-m-d'),
57
            'updated_on'          => $this->organization->updatedOn()->format('Y-m-d'),
58
            'owners'              => $owners,
59
            'organizationMembers' => $organizationMembers,
60
        ];
61
    }
62
}
63