Completed
Push — master ( 615456...bf7129 )
by Sergey
31:30 queued 25:25
created

FollowableProvider::getFollowUrl()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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