Passed
Pull Request — develop (#1)
by Andreas
02:53
created

ContentMapper   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 7
c 2
b 0
f 0
lcom 1
cbo 2
dl 0
loc 60
ccs 17
cts 17
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
B getContent() 0 32 6
1
<?php
2
namespace Wambo\Catalog\Mapper;
3
4
use Wambo\Catalog\Error\ContentException;
5
use Wambo\Catalog\Model\Content;
6
7
/**
8
 * Class ContentMapper creates \Wambo\Model\Content models from data bags with product content data.
9
 *
10
 * @package Wambo\Mapper
11
 */
12
class ContentMapper
13
{
14
    const FIELD_SUMMARY = "summary";
15
    const FIELD_DESCRIPTION = "description";
16
17
    /**
18
     * @var array $mandatoryFields A list of all mandatory fields of a Content
19
     */
20
    private $mandatoryFields = [self::FIELD_SUMMARY];
21
22
    /**
23
     * Creates a new ContentMapper instance
24
     */
25 5
    public function __construct()
26
    {
27 5
    }
28
29
    /**
30
     * Get a Content model from an array of unstructured product content data
31
     *
32
     * @param array $contentData An array containing content attributes
33
     *
34
     * @return Content
35
     *
36
     * @throws ContentException If a mandatory field is missing
37
     * @throws ContentException If the no Content could be created from the given product data
38
     */
39 5
    public function getContent(array $contentData)
40
    {
41
        // check if all mandatory fields are present
42 5
        foreach ($this->mandatoryFields as $mandatoryField) {
43 5
            if (!array_key_exists($mandatoryField, $contentData)) {
44 5
                throw new ContentException("The field '$mandatoryField' is missing in the given content data");
45
            }
46
        }
47
48
        // try to create a content model from the available data
49
        try {
50
51
            // summary
52 4
            $summary = "";
53 4
            if (isset($contentData[self::FIELD_SUMMARY])) {
54 4
                $summary = $contentData[self::FIELD_SUMMARY];
55
            }
56
57
            // description
58 4
            $description = "";
59 4
            if (isset($contentData[self::FIELD_DESCRIPTION])) {
60 3
                $description = $contentData[self::FIELD_DESCRIPTION];
61
            }
62
63 4
            $content = new Content($summary, $description);
64 3
            return $content;
65
66 1
        } catch (\Exception $contentException) {
67 1
            throw new ContentException(sprintf("Failed to create a content model from the given data: %s",
68 1
                $contentException->getMessage()), $contentException);
69
        }
70
    }
71
}