Completed
Push — master ( a68251...03240e )
by jelmer
02:10
created

Title::setLink()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 18
ccs 10
cts 10
cp 1
rs 9.4285
cc 3
eloc 9
nc 3
nop 1
crap 3
1
<?php
2
3
namespace Pageon\SlackWebhookMonolog\Slack\Attachment;
4
5
use Pageon\SlackWebhookMonolog\Slack\Exceptions\InvalidUrlException;
6
7
/**
8
 * The title is displayed as larger, bold text near the top of a message attachment.
9
 * By passing a valid URL in the link parameter (optional), the title text will be hyperlinked.
10
 *
11
 * @author Jelmer Prins <[email protected]>
12
 *
13
 * @since 0.3.2
14
 */
15
final class Title
16
{
17
    /**
18
     * @var string
19
     */
20
    private $title;
21
22
    /**
23
     * @var string|null
24
     */
25
    private $link;
26
27
    /**
28
     * @param string $title
29
     * @param string|null $link
30
     */
31 4
    public function __construct($title, $link = null)
32
    {
33 4
        $this->setTitle($title);
34 4
        $this->setLink($link);
35 3
    }
36
37
    /**
38
     * @return string
39
     */
40 1
    public function getTitle()
41
    {
42 1
        return $this->title;
43
    }
44
45
    /**
46
     * @param string $title
47
     *
48
     * @return self
49
     */
50 4
    private function setTitle($title)
51
    {
52 4
        $this->title = $title;
53
54 4
        return $this;
55
    }
56
57
    /**
58
     * @return bool
59
     */
60 1
    public function hasLink()
61
    {
62 1
        return $this->link !== null;
63
    }
64
65
    /**
66
     * @return string|null
67
     */
68 1
    public function getLink()
69
    {
70 1
        return $this->link;
71
    }
72
73
    /**
74
     * @param string|null $link
75
     *
76
     * @return self
77
     */
78 4
    private function setLink($link = null)
79
    {
80 4
        if ($link === null) {
81 2
            return $this;
82
        }
83
84 3
        if (!preg_match(
85 3
            '_^(?:(?:https?|ftp)://)(?:\S+(?::\S*)?@)?(?:(?!10(?:\.\d{1,3}){3})(?!127(?:\.\d{1,3}){3})(?!169\.254(?:\.\d{1,3}){2})(?!192\.168(?:\.\d{1,3}){2})(?!172\.(?:1[6-9]|2\d|3[0-1])(?:\.\d{1,3}){2})(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z\x{00a1}-\x{ffff}0-9]+-?)*[a-z\x{00a1}-\x{ffff}0-9]+)(?:\.(?:[a-z\x{00a1}-\x{ffff}0-9]+-?)*[a-z\x{00a1}-\x{ffff}0-9]+)*(?:\.(?:[a-z\x{00a1}-\x{ffff}]{2,})))(?::\d{2,5})?(?:/[^\s]*)?$_iu',
86
            $link
87 3
        )
88 3
        ) {
89 1
            throw new InvalidUrlException($link . ' is not a valid url');
90
        }
91
92 2
        $this->link = $link;
93
94 2
        return $this;
95
    }
96
}
97