Completed
Pull Request — master (#17)
by Sergey
04:53
created

PinnerHelper   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 7
c 1
b 0
f 1
lcom 1
cbo 2
dl 0
loc 86
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A createUserDataRequest() 0 6 1
A parseAccountNameResponse() 0 8 2
A createLoginRequest() 0 11 1
A parseLoginResponse() 0 8 2
A createPinnerRequestData() 0 9 1
1
<?php
2
3
namespace seregazhuk\PinterestBot\Helpers\Providers;
4
5
use seregazhuk\PinterestBot\Exceptions\AuthException;
6
7
class PinnerHelper extends RequestHelper
8
{
9
10
    /**
11
     * Creates Pinterest API request to get user info according to
12
     * username, API url and bookmarks for pagination
13
     *
14
     * @param string $username
15
     * @param string $sourceUrl
16
     * @param array  $bookmarks
17
     * @return array
18
     */
19
    public static function createUserDataRequest($username, $sourceUrl, $bookmarks)
20
    {
21
        $dataJson = self::createPinnerRequestData($username);
22
23
        return self::createRequestData($dataJson, $sourceUrl, $bookmarks);
24
    }
25
26
    /**
27
     * Parses Pinterest API response with pinner name
28
     *
29
     * @param $res
30
     * @return null
31
     */
32
    public static function parseAccountNameResponse($res)
33
    {
34
        if (isset($res['resource_data_cache'][1]['resource']['options']['username'])) {
35
            return $res['resource_data_cache'][1]['resource']['options']['username'];
36
        }
37
38
        return null;
39
    }
40
41
    /**
42
     * Creates Pinterest API request to login
43
     *
44
     * @param string $username
45
     * @param string $password
46
     * @return array
47
     */
48
    public static function createLoginRequest($username, $password)
49
    {
50
        $dataJson = [
51
            "options" => [
52
                "username_or_email" => $username,
53
                "password"          => $password
54
            ],
55
            "context" => new \stdClass(),
56
        ];
57
        return self::createRequestData($dataJson, "/login/");
58
    }
59
60
    /**
61
     * Parses Pintrest Api response after login
62
     *
63
     * @param array $res
64
     * @return bool
65
     * @throws AuthException
66
     */
67
    public static function parseLoginResponse($res)
68
    {
69
        if (self::checkMethodCallResult($res)) {
70
            throw new AuthException($res['resource_response']['error']['message']);
71
        }
72
73
        return true;
74
    }
75
76
    /**
77
     * Creates common Pinner request data by username
78
     *
79
     * @param string $username
80
     * @return array
81
     */
82
    protected static function createPinnerRequestData($username)
83
    {
84
        return [
85
            "options" => [
86
                "username" => $username,
87
            ],
88
            "context" => new \stdClass(),
89
        ];
90
    }
91
92
}
93