AuthorListNormalizer::denormalize()   A
last analyzed

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