Completed
Branch master (23259a)
by Sergey
04:17 queued 48s
created

CanBeShared::shareViaTwitter()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace seregazhuk\PinterestBot\Api\Traits;
4
5
use seregazhuk\PinterestBot\Helpers\UrlBuilder;
6
7
trait CanBeShared
8
{
9
    use HandlesRequest, ResolvesCurrentUser;
10
11
    /**
12
     * @return array
13
     */
14
    protected function requiresLoginForCanBeShared()
15
    {
16
        return [
17
            'share',
18
            'markAsGood',
19
            'markAsBad',
20
        ];
21
    }
22
23
    /**
24
     * @param int $pinId
25
     * @return string
26
     */
27
    public function shareViaTwitter($pinId)
28
    {
29
        return $this->share($pinId, $twitterChannel = 9);
30
    }
31
32
    /**
33
     * @param string $pinId
34
     * @param int $channel By default uses 12 (share via link)
35
     * @return string
36
     */
37
    public function share($pinId, $channel = 12)
38
    {
39
        $request = [
40
            'invite_type' => [
41
                'invite_category' => 3, // magic numbers, but I have
42
                'invite_object'   => 1, // no idea what do they mean
43
                'invite_channel'  => $channel,
44
            ],
45
            'object_id'   => $pinId,
46
        ];
47
48
        $response = $this->post(UrlBuilder::RESOURCE_SHARE_VIA_SOCIAL, $request, true);
49
50
        return isset($response['invite_url']) ? $response['invite_url'] : '';
51
    }
52
53
    /**
54
     * @param string $pinId
55
     * @param $userId
56
     * @return array|bool
57
     */
58
    public function leaveGoodReaction($pinId, $userId)
59
    {
60
        return $this->reactOnPinInConversation($pinId, $userId, "👍");
61
    }
62
63
    /**
64
     * @param string $pinId
65
     * @param $userId
66
     * @return array|bool
67
     */
68
    public function leaveBadReaction($pinId, $userId)
69
    {
70
        return $this->reactOnPinInConversation($pinId, $userId, "👎");
71
    }
72
73
    /**
74
     * @param string $pinId
75
     * @param string $userId
76
     * @param string $reaction
77
     * @return array|bool
78
     */
79
    protected function reactOnPinInConversation($pinId, $userId, $reaction)
80
    {
81
        $request = [
82
            'user_ids' => [$userId],
83
            'pin'      => (string)$pinId,
84
            'text'     => $reaction,
85
        ];
86
87
        return $this->post(UrlBuilder::RESOURCE_SEND_MESSAGE, $request);
88
    }
89
}
90