Completed
Pull Request — master (#154)
by Sergey
02:33
created

Followable::getFollowData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
142
    }
143
144
    /**
145
     * @return string
146
     */
147
    protected function getFollowersFor()
148
    {
149
        return property_exists($this, 'followersFor') ? $this->followersFor : '';
0 ignored issues
show
Bug introduced by
The property followersFor does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
150
    }
151
}
152