Completed
Push — develop ( 90d434...5a25e9 )
by Michael
02:38
created

AttachmentBuilder::setTitleLink()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Crummy\Phlack\Builder;
4
5
use Crummy\Phlack\Message\Attachment;
6
use Crummy\Phlack\Message\Collection\FieldCollection;
7
use Crummy\Phlack\Message\Field;
8
9
class AttachmentBuilder implements BuilderInterface
10
{
11
    private $data = [];
12
    private $fields;
13
    private $parent;
14
15
    /**
16
     * Constructor.
17
     */
18 11
    public function __construct(MessageBuilder $parent = null)
19
    {
20 11
        $this->parent = $parent;
21 11
        $this->fields = new FieldCollection();
22 11
    }
23
24
    /**
25
     * @throws \LogicException When called before setFallback($fallback)
26
     *
27
     * @return Attachment
28
     */
29 6
    public function create()
30
    {
31 6
        $attachment = new Attachment($this->data + ['fields' => clone $this->fields]);
32 6
        $this->refresh();
33
34 6
        return $attachment;
35
    }
36
37
    /**
38
     * @param $fallback
39
     *
40
     * @return $this
41
     */
42 7
    public function setFallback($fallback)
43
    {
44 7
        return $this->setParameter('fallback', (string) $fallback);
45
    }
46
47
    /**
48
     * @param $text
49
     *
50
     * @return $this
51
     */
52 1
    public function setText($text)
53
    {
54 1
        return $this->setParameter('text', $text);
55
    }
56
57
    /**
58
     * @param $pretext
59
     *
60
     * @return $this
61
     */
62 1
    public function setPretext($pretext)
63
    {
64 1
        return $this->setParameter('pretext', $pretext);
65
    }
66
67
    /**
68
     * @param $color
69
     *
70
     * @return $this
71
     */
72 1
    public function setColor($color)
73
    {
74 1
        return $this->setParameter('color', $color);
75
    }
76
77
    /**
78
     * @param $author_name
79
     *
80
     * @return $this
81
     */
82 1
    public function setAuthorName($author_name)
83
    {
84 1
        return $this->setParameter('author_name', $author_name);
85
    }
86
87
    /**
88
     * @param $author_link
89
     *
90
     * @return $this
91
     */
92 1
    public function setAuthorLink($author_link)
93
    {
94 1
        return $this->setParameter('author_link', $author_link);
95
    }
96
97
    /**
98
     * @param $author_icon
99
     *
100
     * @return $this
101
     */
102 1
    public function setAuthorIcon($author_icon)
103
    {
104 1
        return $this->setParameter('author_icon', $author_icon);
105
    }
106
107
    /**
108
     * @param $title
109
     *
110
     * @return $this
111
     */
112 1
    public function setTitle($title)
113
    {
114 1
        return $this->setParameter('title', $title);
115
    }
116
117
    /**
118
     * @param $title_link
119
     *
120
     * @return $this
121
     */
122 1
    public function setTitleLink($title_link)
123
    {
124 1
        return $this->setParameter('title_link', $title_link);
125
    }
126
127
    /**
128
     * @param $image_url
129
     *
130
     * @return $this
131
     */
132 1
    public function setImageUrl($image_url)
133
    {
134 1
        return $this->setParameter('image_url', $image_url);
135
    }
136
137
    /**
138
     * @param $thumb_url
139
     *
140
     * @return $this
141
     */
142 1
    public function setThumbUrl($thumb_url)
143
    {
144 1
        return $this->setParameter('thumb_url', $thumb_url);
145
    }
146
147
    /**
148
     * @param $mrkdwn_in
149
     *
150
     * @return $this
151
     */
152 1
    public function setMrkdwnIn($mrkdwn_in)
153
    {
154 1
        return $this->setParameter('mrkdwn_in', $mrkdwn_in);
155
    }
156
157
    /**
158
     * Sets values on non-empty parameters.
159
     * Set $value to null to remove the custom value.
160
     *
161
     * @param string $name
162
     * @param $value
163
     *
164
     * @return $this
165
     */
166 7
    private function setParameter($name, $value)
167
    {
168 7
        if (null !== $value && empty($value)) {
169 1
            return $this;
170
        }
171
172 7
        $this->data[$name] = $value;
173
174 7
        return $this;
175
    }
176
177
    /**
178
     * @param string $title
179
     * @param string $value
180
     * @param bool   $isShort
181
     *
182
     * @return $this
183
     */
184 3
    public function addField($title, $value, $isShort)
185
    {
186 3
        $this->fields->add(new Field($title, $value, $isShort));
187
188 3
        return $this;
189
    }
190
191
    /**
192
     * Reset the attachment data and the fields collection.
193
     */
194 6
    protected function refresh()
195
    {
196 6
        $this->data = [];
197 6
        $this->fields->clear();
198 6
    }
199
200
    /**
201
     * @return MessageBuilder
202
     */
203 3
    public function end()
204
    {
205 3
        if ($this->parent) {
206 2
            $this->parent->addAttachment($this->create());
207
208 2
            return $this->parent;
209
        }
210
211 1
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this; (Crummy\Phlack\Builder\AttachmentBuilder) is incompatible with the return type documented by Crummy\Phlack\Builder\AttachmentBuilder::end of type Crummy\Phlack\Builder\MessageBuilder.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
212
    }
213
}
214