Completed
Push — master ( c40fa9...515296 )
by Sergey
51s
created

Followable::getFollowUrl()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 2
eloc 2
nc 2
nop 0
1
<?php
2
3
namespace seregazhuk\PinterestBot\Api\Traits;
4
5
use seregazhuk\PinterestBot\Api\Request;
6
use seregazhuk\PinterestBot\Api\Response;
7
use seregazhuk\PinterestBot\Helpers\Pagination;
8
use seregazhuk\PinterestBot\Helpers\UrlBuilder;
9
10
/**
11
 * Trait Followable
12
 *
13
 * @property string $followUrl
14
 * @property string $unFollowUrl
15
 * @property string $followersUrl
16
 * @property string $followersFor
17
 * @property Response $response
18
 * @property Request $request
19
 */
20
trait Followable
21
{
22
    use HandlesRequest, HasEntityIdName;
23
24
    /**
25
     * Follow user by user_id.
26
     *
27
     * @param $entityId
28
     *
29
     * @return bool
30
     */
31
    public function follow($entityId)
32
    {
33
        return $this->followCall($entityId, $this->getFollowUrl());
34
    }
35
36
    /**
37
     * UnFollow user by user_id.
38
     *
39
     * @param $entityId
40
     *
41
     * @return bool
42
     */
43
    public function unFollow($entityId)
44
    {
45
        return $this->followCall($entityId, $this->getUnFollowUrl());
46
    }
47
48
    /**
49
     * Make api call for follow/unFollow a entity (user/board).
50
     *
51
     * @param int    $entityId
52
     * @param string $resourceUrl
53
     *
54
     * @return bool
55
     */
56
    protected function followCall($entityId, $resourceUrl)
57
    {
58
        $result = $this->request
59
            ->exec($resourceUrl, $this->createFollowRequestQuery($entityId));
60
61
        $this->processResult($result);
62
63
        return $this->response->isOk();
64
    }
65
66
    /**
67
     * @param integer $entityId
68
     * @return string
69
     */
70
    public function createFollowRequestQuery($entityId)
71
    {
72
        $entityName = $this->getEntityIdName();
73
74
        $dataJson = [
75
            'options' => [
76
                $entityName => (string)$entityId,
77
            ],
78
            'context' => [],
79
        ];
80
81
        if ($entityName == 'interest_id') {
82
            $dataJson['options']['interest_list'] = 'favorited';
83
        }
84
85
        $post = ['data' => json_encode($dataJson, JSON_FORCE_OBJECT)];
86
87
        return UrlBuilder::buildRequestString($post);
88
    }
89
90
    /**
91
     * Get followers.
92
     *
93
     * @param string $for
94
     * @param int $limit
95
     *
96
     * @return Pagination
97
     */
98
    public function followers($for, $limit = Pagination::DEFAULT_LIMIT)
99
    {
100
        return $this->paginate(
101
            [$this->getFollowersFor() => $for], $this->getFollowersUrl(), $limit
102
        );
103
    }
104
105
    /**
106
     * @return string
107
     */
108
    protected function getFollowUrl()
109
    {
110
        return property_exists($this, 'followUrl') ? $this->followUrl : '';
111
    }
112
113
    /**
114
     * @return string
115
     */
116
    protected function getUnFollowUrl()
117
    {
118
        return property_exists($this, 'unFollowUrl') ? $this->unFollowUrl : '';
119
    }
120
121
    /**
122
     * @return string
123
     */
124
    protected function getFollowersUrl()
125
    {
126
        return property_exists($this, 'followersUrl') ? $this->followersUrl : '';
127
    }
128
129
    /**
130
     * @return string
131
     */
132
    protected function getFollowersFor()
133
    {
134
        return property_exists($this, 'followersFor') ? $this->followersFor : '';
135
    }
136
137
    /**
138
     * @param string $res
139
     * @return Response
140
     */
141
    abstract protected function processResult($res);
142
143
    /**
144
     * @param array  $data
145
     * @param string $resourceUrl
146
     * @param int $limit
147
     *
148
     * @return Pagination
149
     */
150
    abstract protected function paginate($data, $resourceUrl, $limit = Pagination::DEFAULT_LIMIT);
151
}
152