Completed
Pull Request — master (#308)
by Sergey
06:18
created

CanBeShared::leaveGoodReaction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 2
dl 0
loc 4
rs 10
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 string $pinId
25
     * @return string
26
     */
27
    public function share($pinId)
28
    {
29
        $request = [
30
            "invite_type" => [
31
                "invite_category" => 3, // magic numbers, but I have
32
                "invite_object"   => 1, // no idea what do they mean
33
                "invite_channel"  => $linkChannel = 12,
34
            ],
35
            "object_id"   => $pinId,
36
        ];
37
38
        $response = $this->post(UrlBuilder::RESOURCE_SHARE_VIA_SOCIAL, $request, true);
39
40
        return isset($response['invite_url']) ? $response['invite_url'] : '';
41
    }
42
43
    /**
44
     * @param string $pinId
45
     * @param $userId
46
     * @return array|bool
47
     */
48
    public function leaveGoodReaction($pinId, $userId)
49
    {
50
        return $this->reactOnPinInConversation($pinId, $userId, "👍");
51
    }
52
53
    /**
54
     * @param string $pinId
55
     * @param $userId
56
     * @return array|bool
57
     */
58
    public function leaveBadReaction($pinId, $userId)
59
    {
60
        return $this->reactOnPinInConversation($pinId, $userId, "👎");
61
    }
62
63
    /**
64
     * @param string $pinId
65
     * @param string $userId
66
     * @param string $reaction
67
     * @return array|bool
68
     */
69
    protected function reactOnPinInConversation($pinId, $userId, $reaction)
70
    {
71
        $request = [
72
            "user_ids" => [$userId],
73
            "pin"      => (string)$pinId,
74
            "text"     => $reaction,
75
        ];
76
77
        return $this->post(UrlBuilder::RESOURCE_SEND_MESSAGE, $request);
78
    }
79
}
80