Completed
Push — master ( 448473...97d669 )
by Sergey
05:45
created

Boards::delete()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 8

Duplication

Lines 15
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 15
loc 15
rs 9.4286
cc 1
eloc 8
nc 1
nop 1
1
<?php
2
3
namespace seregazhuk\PinterestBot\Api\Providers;
4
5
use seregazhuk\PinterestBot\Helpers\Providers\FollowHelper;
6
use seregazhuk\PinterestBot\Helpers\Providers\SearchHelper;
7
use seregazhuk\PinterestBot\Api\Request;
8
use seregazhuk\PinterestBot\Helpers\UrlHelper;
9
10
class Boards extends Provider
11
{
12
    use SearchHelper, FollowHelper;
13
14
    /**
15
     * Get boards for user by username
16
     * @param string $username
17
     * @return array|bool
18
     */
19
    public function forUser($username)
20
    {
21
        $get = Request::createRequestData(
22
            [
23
                'options' => ["username" => $username],
24
            ]
25
        );
26
27
        return $this->boardsGetCall($get, UrlHelper::RESOURCE_GET_BOARDS);
28
    }
29
30
    /**
31
     * Get info about user's board
32
     * @param string $username
33
     * @param string $board
34
     * @return array|bool
35
     */
36
    public function info($username, $board)
37
    {
38
        $get = Request::createRequestData(
39
            [
40
                'options' => [
41
                    'username'      => $username,
42
                    'slug'          => $board,
43
                    'field_set_key' => 'detailed'
44
                ],
45
            ]
46
        );
47
48
        return $this->boardsGetCall($get, UrlHelper::RESOURCE_GET_BOARDS);
49
    }
50
51
    /**
52
     * Get pins form board by boardId
53
     * @param int $boardId
54
     * @param int $batchesLimit
55
     * @return \Iterator
56
     */
57
    public function pins($boardId, $batchesLimit = 0)
58
    {
59
        return $this->getPaginatedData(
60
            [$this, 'getPinsFromBoard'], [
61
            'boardId' => $boardId,
62
        ], $batchesLimit
63
        );
64
65
    }
66
67
    /**
68
     * @param int   $boardId
69
     * @param array $bookmarks
70
     * @return array|bool
71
     */
72
    protected function getPinsFromBoard($boardId, $bookmarks = [])
73
    {
74
        $get = Request::createRequestData(
75
            [
76
                'options' => ['board_id' => $boardId],
77
            ], '', $bookmarks
78
        );
79
80
        return $this->boardsGetCall($get, UrlHelper::RESOURCE_GET_BOARD_FEED, true);
81
    }
82
83
    /**
84
     * Run GET api request to boards resource
85
     * @param array  $query
86
     * @param string $url
87
     * @param bool   $pagination
88
     * @return array|bool
89
     */
90
    protected function boardsGetCall($query, $url, $pagination = false)
91
    {
92
        $getString = UrlHelper::buildRequestString($query);
93
        $response = $this->request->exec($url."?{$getString}");
94
        if ($pagination) {
95
            return $this->response->getPaginationData($response);
96
        }
97
98
        return $this->response->getData($response);
99
    }
100
101
    /**
102
     * Delete your board by ID
103
     *
104
     * @param int $boardId
105
     * @return array|bool
106
     */
107 View Code Duplication
    public function delete($boardId)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
108
    {
109
        $this->request->checkLoggedIn();
110
        $post = Request::createRequestData(
111
            [
112
                'options' => [
113
                    'board_id' => $boardId
114
                ]
115
            ]
116
        );
117
        $postString = UrlHelper::buildRequestString($post);
118
        $response = $this->request->exec(UrlHelper::RESOURCE_DELETE_BOARD, $postString);
119
120
        return $this->response->checkResponse($response);
121
    }
122
123
    /**
124
     * Create a new board
125
     *
126
     * @param string $name
127
     * @param string $description
128
     * @param string $privacy Can be 'public' or 'secret'. 'public by default.
129
     * @return array|bool
130
     */
131 View Code Duplication
    public function create($name, $description, $privacy = 'public')
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
132
    {
133
        $this->request->checkLoggedIn();
134
        $post = Request::createRequestData(
135
            [
136
                'options' => [
137
                    'name'        => $name,
138
                    'description' => $description,
139
                    'privacy'     => $privacy,
140
                ]
141
            ]
142
        );
143
        $postString = UrlHelper::buildRequestString($post);
144
        $response = $this->request->exec(UrlHelper::RESOURCE_CREATE_BOARD, $postString);
145
146
        return $this->response->checkResponse($response);
147
    }
148
149
    /**
150
     * Search scope
151
     *
152
     * @return string
153
     */
154
    protected function getScope()
155
    {
156
        return 'boards';
157
    }
158
159
    protected function getEntityIdName()
160
    {
161
        return Request::BOARD_ENTITY_ID;
162
    }
163
164
    /**
165
     * Follow resource
166
     *
167
     * @return string
168
     */
169
    protected function getFollowUrl()
170
    {
171
        return UrlHelper::RESOURCE_FOLLOW_BOARD;
172
    }
173
174
    /**
175
     * UnFollow resource
176
     * @return string
177
     */
178
    protected function getUnfFollowUrl()
179
    {
180
        return UrlHelper::RESOURCE_UNFOLLOW_BOARD;
181
    }
182
}