FollowableProvider::resolveEntityId()   A
last analyzed

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