Completed
Push — master ( fe2c9c...c65c1d )
by Vladimir
02:30
created

Tumblr::processShareData()   B

Complexity

Conditions 6
Paths 10

Size

Total Lines 22
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 16
nc 10
nop 0
dl 0
loc 22
rs 8.6737
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\Driver;
12
13
/**
14
 * Driver 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 Driver
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 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
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
    public function getLink()
80
    {
81
        $this->_link = 'https://www.tumblr.com/widgets/share/tool?canonicalUrl={url}';
82
83
        $this->addUrlParam('posttype', $this->postType);
84
85
        if ($this->postType === self::POST_TYPE_TEXT) {
86
            $this->addUrlParam('title', '{title}');
87
        } else {
88
            $this->addUrlParam('caption', '{title}');
89
        }
90
91
        switch ($this->postType) {
92
            case self::POST_TYPE_LINK:
93
                $url = $this->shareUrl === null
94
                    ? '{url}'
95
                    : $this->shareUrl;
96
                $this->addUrlParam('content', $url);
97
                break;
98
            case self::POST_TYPE_PHOTO:
99
                $this->addUrlParam('content', implode(',', $this->sharePhotos));
100
                break;
101
            case self::POST_TYPE_VIDEO:
102
                $this->addUrlParam('content', $this->shareVideoUrl);
103
                break;
104
            default:
105
                $this->addUrlParam('content', '{description}');
106
                break;
107
        }
108
109
        if (!empty($this->tags)) {
110
            $this->addUrlParam('tags', implode(',' , $this->tags));
111
        }
112
113
        return parent::getLink();
114
    }
115
116
    /**
117
     * Method should process the share data for current driver.
118
     */
119
    protected function processShareData()
120
    {
121
        $this->url = static::encodeData($this->url);
122
        $this->title = static::encodeData($this->title);
123
        $this->description = static::encodeData($this->description);
124
125
        switch ($this->postType) {
126
            case self::POST_TYPE_LINK:
127
                if (is_string($this->shareUrl)) {
128
                    $this->shareUrl = static::encodeData($this->shareUrl);
129
                }
130
                break;
131
            case self::POST_TYPE_PHOTO:
132
                $this->sharePhotos = static::encodeData($this->sharePhotos);
0 ignored issues
show
Documentation Bug introduced by
It seems like static::encodeData($this->sharePhotos) can also be of type string. However, the property $sharePhotos is declared as type array. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

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

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
133
                break;
134
            case self::POST_TYPE_VIDEO:
135
                $this->shareVideoUrl = static::encodeData($this->shareVideoUrl);
136
                break;
137
        }
138
139
        if (!empty($this->tags)) {
140
            $this->tags = static::encodeData($this->tags);
0 ignored issues
show
Documentation Bug introduced by
It seems like static::encodeData($this->tags) can also be of type string. However, the property $tags is declared as type array. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

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

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
141
        }
142
    }
143
}
144