Completed
Pull Request — master (#245)
by Sergey
04:32
created

Followable::createFollowRequest()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

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