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

AuthorListNormalizer::denormalize()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 10
cts 10
cp 1
rs 9.2
c 0
b 0
f 0
cc 4
eloc 8
nc 2
nop 1
crap 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