Test Failed
Push — develop ( 587b5b...6f7954 )
by Andreas
04:55 queued 01:18
created

Content::__construct()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 31
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 5

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 31
ccs 19
cts 19
cp 1
rs 8.439
cc 5
eloc 18
nc 5
nop 3
crap 5
1
<?php
2
namespace Wambo\Catalog\Model;
3
4
use Wambo\Catalog\Exception\ContentException;
5
6
/**
7
 * Class Content contains a product title, summary and description text
8
 *
9
 * @package Wambo\Catalog\Model
10
 */
11
class Content
12
{
13
    const TITLE_MIN_LENGTH = 1;
14
    const TITLE_MAX_LENGTH = 60;
15
16
    const SUMMARY_MIN_LENGTH = 5;
17
    const SUMMARY_MAX_LENGTH = 140;
18
19
    /**
20
     * @var string
21
     */
22
    private $summaryText;
23
    /**
24
     * @var string
25
     */
26
    private $productDescription;
27
    /**
28
     * @var string
29
     */
30
    private $title;
31
32
    /**
33
     * Creates a new instance of a product Content model.
34
     *
35
     * @param string $title              A product title (e.g. "Fancy T-Shirt No. 1")
36
     * @param string $summaryText        A short description text of a product (e.g. "The first edition of our fancy
37
     *                                   T-Shirt with a unicorn pooping ice cream on the front")
38
     * @param string $productDescription A full product description text (optional)
39
     *
40
     * @throws ContentException If the summary text is too long
41
     */
42 20
    public function __construct(string $title, string $summaryText, string $productDescription = "")
43
    {
44
        // validate the title
45 20
        $trimmedTitle = trim($title);
46 20
        if (strlen($trimmedTitle) < self::TITLE_MIN_LENGTH) {
47 2
            throw new ContentException(sprintf("The title should not be shorter than %s characters",
48 2
                self::TITLE_MIN_LENGTH));
49
        }
50
51 18
        if (strlen($trimmedTitle) > self::TITLE_MAX_LENGTH) {
52 1
            throw new ContentException(sprintf("The title should not be longer than %s characters",
53 1
                self::TITLE_MAX_LENGTH));
54
        }
55
56
        // validate the summary
57 17
        $trimmedSummary = trim($summaryText);
58
59 17
        if (strlen($trimmedSummary) < self::SUMMARY_MIN_LENGTH) {
60 2
            throw new ContentException(sprintf("The summary text should not be shorter than %s characters",
61 2
                self::SUMMARY_MIN_LENGTH));
62
        }
63
64 15
        if (strlen($trimmedSummary) > self::SUMMARY_MAX_LENGTH) {
65 1
            throw new ContentException(sprintf("The summary text should not be longer than %s characters",
66 1
                self::SUMMARY_MAX_LENGTH));
67
        }
68
69 14
        $this->title = $trimmedTitle;
70 14
        $this->summaryText = $trimmedSummary;
71 14
        $this->productDescription = $productDescription;
72 14
    }
73
74
    /**
75
     * Get the title of a product (e.g. "Fancy T-Shirt No. 1")
76
     *
77
     * @return string
78
     */
79 3
    public function getTitle()
80
    {
81 3
        return $this->title;
82
    }
83
84
    /**
85
     * Get a short description text of a product (e.g. "The first edition of our fancy T-Shirt with a unicorn pooping
86
     * ice cream on the front")
87
     *
88
     * @return string
89
     */
90 4
    public function getSummaryText()
91
    {
92 4
        return $this->summaryText;
93
    }
94
95
    /**
96
     * Get a full product description text
97
     *
98
     * @return string
99
     */
100 4
    public function getProductDescription()
101
    {
102 4
        return $this->productDescription;
103
    }
104
}