Passed
Push — master ( 2aad05...4fe473 )
by Yo
02:45
created

AuthorListNormalizer   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 8
lcom 0
cbo 1
dl 0
loc 47
ccs 23
cts 23
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A normalize() 0 16 4
A denormalize() 0 13 4
1
<?php
2
namespace Yoanm\ComposerConfigManager\Application\Serializer\Normalizer;
3
4
use Yoanm\ComposerConfigManager\Domain\Model\Author;
5
6
class AuthorListNormalizer implements DenormalizerInterface
7
{
8
    const KEY_NAME = 'name';
9
    const KEY_EMAIL = 'email';
10
    const KEY_ROLE = 'role';
11
12
    /**
13
     * @param Author[] $authorList
14
     *
15
     * @return array
16
     */
17 1
    public function normalize(array $authorList)
18
    {
19 1
        $normalizedList = [];
20 1
        foreach ($authorList as $author) {
21 1
            $normalizedAuthor = [self::KEY_NAME => $author->getName()];
22 1
            if ($author->getEmail()) {
23 1
                $normalizedAuthor[self::KEY_EMAIL] = $author->getEmail();
24 1
            }
25 1
            if ($author->getRole()) {
26 1
                $normalizedAuthor[self::KEY_ROLE] = $author->getRole();
27 1
            }
28 1
            $normalizedList[] = $normalizedAuthor;
29 1
        }
30
31 1
        return $normalizedList;
32
    }
33
34
    /**
35
     * @param array $authorList
36
     *
37
     * @return Author[]
38
     */
39 2
    public function denormalize(array $authorList)
40
    {
41 2
        $normalizedList = [];
42 2
        foreach ($authorList as $authorData) {
43 2
            $normalizedList[] = new Author(
44 2
                $authorData[self::KEY_NAME],
45 2
                isset($authorData[self::KEY_EMAIL]) ? $authorData[self::KEY_EMAIL] : null,
46 2
                isset($authorData[self::KEY_ROLE]) ? $authorData[self::KEY_ROLE] : null
47 2
            );
48 2
        }
49
50 2
        return $normalizedList;
51
    }
52
}
53