Completed
Push — master ( 2006e3...2912e5 )
by ARCANEDEV
9s
created

Card::types()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 12
cc 1
eloc 9
nc 1
nop 0
ccs 9
cts 9
cp 1
crap 1
rs 9.4285
1
<?php namespace Arcanedev\SeoHelper\Entities\Twitter;
2
3
use Arcanedev\SeoHelper\Contracts\Entities\TwitterCardInterface;
4
use Arcanedev\SeoHelper\Exceptions\InvalidTwitterCardException;
5
use Arcanedev\Support\Traits\Configurable;
6
7
/**
8
 * Class     Card
9
 *
10
 * @package  Arcanedev\SeoHelper\Entities\Twitter
11
 * @author   ARCANEDEV <[email protected]>
12
 */
13
class Card implements TwitterCardInterface
14
{
15
    /* ------------------------------------------------------------------------------------------------
16
     |  Traits
17
     | ------------------------------------------------------------------------------------------------
18
     */
19
    use Configurable;
20
21
    /* ------------------------------------------------------------------------------------------------
22
     |  Properties
23
     | ------------------------------------------------------------------------------------------------
24
     */
25
    /**
26
     * Card type.
27
     *
28
     * @var string
29
     */
30
    protected $type;
31
32
    /**
33
     * Card meta collection.
34
     *
35
     * @var \Arcanedev\SeoHelper\Contracts\Entities\MetaCollectionInterface
36
     */
37
    protected $metas;
38
39
    /**
40
     * Card images.
41
     *
42
     * @var array
43
     */
44
    protected $images  = [];
45
46
    /* ------------------------------------------------------------------------------------------------
47
     |  Constructor
48
     | ------------------------------------------------------------------------------------------------
49
     */
50
    /**
51
     * Make the twitter card instance.
52
     *
53
     * @param  array  $configs
54
     */
55 480
    public function __construct(array $configs = [])
56
    {
57 480
        $this->setConfigs($configs);
58 480
        $this->metas = new MetaCollection;
59
60 480
        $this->init();
61 480
    }
62
63
    /**
64
     * Start the engine.
65
     *
66
     * @return self
67
     */
68 480
    private function init()
69
    {
70 480
        $this->setPrefix($this->getConfig('prefix', 'twitter:'));
71 480
        $this->setType($this->getConfig('card', static::TYPE_SUMMARY));
72 480
        $this->setSite($this->getConfig('site', ''));
73 480
        $this->setTitle($this->getConfig('title', ''));
74 480
        $this->addMetas($this->getConfig('metas', []));
75
76 480
        return $this;
77
    }
78
79
    /* ------------------------------------------------------------------------------------------------
80
     |  Getters & Setters
81
     | ------------------------------------------------------------------------------------------------
82
     */
83
    /**
84
     * Set meta prefix name.
85
     *
86
     * @param  string  $prefix
87
     *
88
     * @return self
89
     */
90 480
    private function setPrefix($prefix)
91
    {
92 480
        $this->metas->setPrefix($prefix);
93
94 480
        return $this;
95
    }
96
97
    /**
98
     * Set the card type.
99
     *
100
     * @param  string  $type
101
     *
102
     * @return self
103
     */
104 480
    public function setType($type)
105
    {
106 480
        if ( ! empty($type)) {
107 480
            $this->checkType($type);
108
109 480
            $this->type = $type;
110 480
            $this->addMeta('card', $type);
111 360
        }
112
113 480
        return $this;
114
    }
115
116
    /**
117
     * Set card site.
118
     *
119
     * @param  string  $site
120
     *
121
     * @return self
122
     */
123 480
    public function setSite($site)
124
    {
125 480
        if ( ! empty($site)) {
126 480
            $this->checkSite($site);
127 480
            $this->addMeta('site', $site);
128 360
        }
129
130 480
        return $this;
131
    }
132
133
    /**
134
     * Set card title.
135
     *
136
     * @param  string  $title
137
     *
138
     * @return self
139
     */
140 480
    public function setTitle($title)
141
    {
142 480
        $this->addMeta('title', $title);
143
144 480
        return $this;
145
    }
146
147
    /**
148
     * Set card description.
149
     *
150
     * @param  string  $description
151
     *
152
     * @return self
153
     */
154 60
    public function setDescription($description)
155
    {
156 60
        $this->addMeta('description', $description);
157
158 60
        return $this;
159
    }
160
161
    /**
162
     * Add image to the card.
163
     *
164
     * @param  string  $url
165
     *
166
     * @return self
167
     */
168 60
    public function addImage($url)
169
    {
170 60
        if (count($this->images) < 4) {
171 60
            $this->images[] = $url;
172 45
        }
173
174 60
        return $this;
175
    }
176
177
    /**
178
     * Add many metas to the card.
179
     *
180
     * @param  array  $metas
181
     *
182
     * @return self
183
     */
184 480
    public function addMetas(array $metas)
185
    {
186 480
        foreach ($metas as $name => $content) {
187 24
            $this->addMeta($name, $content);
188 360
        }
189
190 480
        return $this;
191
    }
192
193
    /**
194
     * Add a meta to the card.
195
     *
196
     * @param  string  $name
197
     * @param  string  $content
198
     *
199
     * @return self
200
     */
201 480
    public function addMeta($name, $content)
202
    {
203 480
        $this->metas->add($name, $content);
204
205 480
        return $this;
206
    }
207
208
    /**
209
     * Get all supported card types.
210
     *
211
     * @return array
212
     */
213 480
    public function types()
214
    {
215
        return [
216 480
            static::TYPE_APP,
217 480
            static::TYPE_GALLERY,
218 480
            static::TYPE_PHOTO,
219 480
            static::TYPE_PLAYER,
220 480
            static::TYPE_PRODUCT,
221 480
            static::TYPE_SUMMARY,
222 480
            static::TYPE_SUMMARY_LARGE_IMAGE,
223 360
        ];
224
    }
225
226
    /* ------------------------------------------------------------------------------------------------
227
     |  Main Functions
228
     | ------------------------------------------------------------------------------------------------
229
     */
230
    /**
231
     * Render card images.
232
     */
233 60
    private function loadImages()
234
    {
235 60
        if (count($this->images) == 1) {
236 48
            $this->addMeta('image', $this->images[0]);
237
238 48
            return;
239
        }
240
241 12
        foreach ($this->images as $number => $url) {
242 12
            $this->addMeta("image{$number}", $url);
243 9
        }
244 12
    }
245
246
    /**
247
     * Reset the card.
248
     *
249
     * @return self
250
     */
251 24
    public function reset()
252
    {
253 24
        $this->metas->reset();
254 24
        $this->images = [];
255
256 24
        return $this->init();
257
    }
258
259
    /**
260
     * Render the twitter card.
261
     *
262
     * @return string
263
     */
264 336
    public function render()
265
    {
266 336
        if ( ! empty($this->images)) {
267 60
            $this->loadImages();
268 45
        }
269
270 336
        return $this->metas->render();
271
    }
272
273
    /**
274
     * Render the tag.
275
     *
276
     * @return string
277
     */
278 96
    public function __toString()
279
    {
280 96
        return $this->render();
281
    }
282
283
    /* ------------------------------------------------------------------------------------------------
284
     |  Check Functions
285
     | ------------------------------------------------------------------------------------------------
286
     */
287
    /**
288
     * Check the card type.
289
     *
290
     * @param  string  $type
291
     *
292
     * @throws \Arcanedev\SeoHelper\Exceptions\InvalidTwitterCardException
293
     */
294 480
    private function checkType(&$type)
295
    {
296 480
        if ( ! is_string($type)) {
297 12
            throw new InvalidTwitterCardException(
298 12
                'The Twitter card type must be a string value, [' . gettype($type) . '] was given.'
299 9
            );
300
        }
301
302 480
        $type = strtolower(trim($type));
303
304 480
        if ( ! in_array($type, $this->types())) {
305 12
            throw new InvalidTwitterCardException(
306 12
                "The Twitter card type [$type] is not supported."
307 9
            );
308
        }
309 480
    }
310
311
    /**
312
     * Check the card site.
313
     *
314
     * @param  string  $site
315
     */
316 480
    private function checkSite(&$site)
317
    {
318 480
        $site = $this->prepareUsername($site);
319 480
    }
320
321
    /* ------------------------------------------------------------------------------------------------
322
     |  Other Functions
323
     | ------------------------------------------------------------------------------------------------
324
     */
325
    /**
326
     * Prepare username.
327
     *
328
     * @param  string  $username
329
     *
330
     * @return string
331
     */
332 480
    private function prepareUsername($username)
333
    {
334 480
        if ( ! starts_with($username, '@')) {
335 480
            $username = '@' . $username;
336 360
        }
337
338 480
        return $username;
339
    }
340
}
341