Passed
Push — master ( abe9d4...f9c79d )
by Vladimir
04:18
created

Tumblr::getLink()   C

Complexity

Conditions 7
Paths 20

Size

Total Lines 35
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 25
nc 20
nop 0
dl 0
loc 35
rs 6.7272
c 0
b 0
f 0
1
<?php
2
/**
3
 * @link https://github.com/yiimaker/yii2-social-share
4
 * @copyright Copyright (c) 2017 Yii Maker
5
 * @license BSD 3-Clause License
6
 */
7
8
namespace ymaker\social\share\drivers;
9
10
use yii\base\InvalidConfigException;
11
use ymaker\social\share\base\DriverAbstract;
12
13
/**
14
 * DriverAbstract for Tumblr.
15
 * @link https://www.tumblr.com
16
 *
17
 * @author Vladimir Kuprienko <[email protected]>
18
 * @since 1.4.0
19
 */
20
class Tumblr extends DriverAbstract
21
{
22
    const POST_TYPE_LINK    = 'link';
23
    const POST_TYPE_TEXT    = 'text';
24
    const POST_TYPE_QUOTE   = 'quote';
25
    const POST_TYPE_PHOTO   = 'photo';
26
    const POST_TYPE_VIDEO   = 'video';
27
28
    /**
29
     * @var string
30
     */
31
    public $postType = self::POST_TYPE_LINK;
32
    /**
33
     * @var string Share URL for type of post `link`.
34
     */
35
    public $shareUrl;
36
    /**
37
     * @var array|string Share photos list for type of post `photo`.
38
     */
39
    public $sharePhotos = [];
40
    /**
41
     * @var string Share URL or embed code for type of post `video`.
42
     */
43
    public $shareVideoUrl;
44
    /**
45
     * @var array|string
46
     */
47
    public $tags = [];
48
49
50
    /**
51
     * @inheritdoc
52
     */
53
    public function init()
54
    {
55
        $types = [
56
            self::POST_TYPE_LINK,
57
            self::POST_TYPE_TEXT,
58
            self::POST_TYPE_QUOTE,
59
            self::POST_TYPE_PHOTO,
60
            self::POST_TYPE_VIDEO,
61
        ];
62
63
        if (!in_array($this->postType, $types)) {
64
            throw new InvalidConfigException('Invalid post type: "' . $this->postType . '"');
65
        }
66
        if ($this->postType === self::POST_TYPE_PHOTO && empty($this->sharePhotos)) {
67
            throw new InvalidConfigException('Photos list cannot be blank!');
68
        }
69
        if ($this->postType === self::POST_TYPE_VIDEO && $this->shareVideoUrl === null) {
70
            throw new InvalidConfigException('You should to set share video URL or embed code!');
71
        }
72
73
        parent::init();
74
    }
75
76
    /**
77
     * @inheritdoc
78
     */
79
    protected function processShareData()
80
    {
81
        $this->url = static::encodeData($this->url);
82
        $this->title = static::encodeData($this->title);
83
        $this->description = static::encodeData($this->description);
84
85
        switch ($this->postType) {
86
            case self::POST_TYPE_LINK:
87
                $this->appendToData(
88
                    'content',
89
                    null === $this->shareUrl ? $this->url : $this->shareUrl,
90
                    null !== $this->shareUrl
91
                );
92
                break;
93
            case self::POST_TYPE_PHOTO:
94
                $this->appendToData('content', implode(',', $this->sharePhotos));
0 ignored issues
show
Bug introduced by
It seems like $this->sharePhotos can also be of type string; however, parameter $pieces of implode() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

94
                $this->appendToData('content', implode(',', /** @scrutinizer ignore-type */ $this->sharePhotos));
Loading history...
95
                break;
96
            case self::POST_TYPE_VIDEO:
97
                $this->appendToData('content', $this->shareVideoUrl);
98
                break;
99
            default:
100
                $this->appendToData('content', $this->description, false);
101
                break;
102
        }
103
104
        if (!empty($this->tags)) {
105
            $this->appendToData('tags', implode(',' , $this->tags));
106
        }
107
    }
108
109
    /**
110
     * @inheritdoc
111
     */
112
    protected function buildLink()
113
    {
114
        $link = 'https://www.tumblr.com/widgets/share/tool?canonicalUrl={url}';
115
116
        $this->addUrlParam($link, 'posttype', $this->postType);
117
118
        if ($this->postType === self::POST_TYPE_TEXT) {
119
            $this->addUrlParam($link, 'title', '{title}');
120
        } else {
121
            $this->addUrlParam($link, 'caption', '{title}');
122
        }
123
124
        $this->addUrlParam($link, 'content', '{content}');
125
126
        if (!empty($this->tags)) {
127
            $this->addUrlParam($link, 'tags', '{tags}');
128
        }
129
130
        return $link;
131
    }
132
}
133