Completed
Push — master ( a52fe5...b565b6 )
by
unknown
05:36
created

TumblrBlog::request()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 24
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 1 Features 1
Metric Value
c 4
b 1
f 1
dl 0
loc 24
rs 8.5126
cc 5
eloc 12
nc 4
nop 4
1
<?php
2
3
namespace Borfast\Socializr\Connectors;
4
5
use Borfast\Socializr\Blog;
6
use Borfast\Socializr\Exceptions\TumblrPostingException;
7
use Borfast\Socializr\Post;
8
use Borfast\Socializr\Response;
9
10
class TumblrBlog extends Tumblr
11
{
12
    public static $provider = 'Tumblr';
13
14
    protected $base_hostname;
15
16
    public function request($path, $method = 'GET', $params = [], $headers = [])
17
    {
18
        $result = parent::request($path, $method, $params, $headers);
19
20
        $json_result = json_decode($result);
21
22
        $status = $json_result->meta->status;
23
24
        if ($status < 200 || $status > 299) {
25
            $msg = $json_result->meta->msg;
26
27
            if ($status == 400) {
28
                $media_error_message = 'Error uploading photo.';
29
30
                if (array_search($media_error_message, $json_result->response->errors) !== false) {
31
                    $msg .= ': ' . $media_error_message;
32
                }
33
            }
34
35
            throw new TumblrPostingException($msg, $status);
36
        }
37
38
        return $result;
39
    }
40
41
42
    public function post(Post $post)
43
    {
44
        $path = 'blog/'.$this->options['base_hostname'].'/post';
45
        $method = 'POST';
46
47
        $params = [];
48
        if (!empty($post->tags)) {
49
            $params['tags'] = $post->tags;
50
        }
51
52
53
        if (empty($post->media)) {
54
            $body  = '<p>' . $post->body . '</p>';
55
            $body .= '<strong>' . $post->url . '</strong>';
56
57
            $params['type'] = 'text';
58
            $params['title'] = $post->title;
59
            $params['body'] = $body;
60
        } else {
61
            $caption  = '<h2>' . $post->title . '</h2>';
62
            $caption .= '<p>' . $post->body . '</p>';
63
            $caption .= '<strong>' . $post->url . '</strong>';
64
65
            $params['type'] = 'photo';
66
            $params['caption'] = $caption;
67
            $params['source'] = $post->media[0];
68
        }
69
70
        $result = $this->request($path, $method, $params);
71
72
        $response = new Response;
73
        $response->setRawResponse(json_encode($result));
74
        $result_json = json_decode($result);
75
        $response->setProvider('Tumblr');
76
        $response->setPostId($result_json->response->id);
77
78
        return $response;
79
    }
80
81
    public function getBlog()
82
    {
83
        $api_key = $this->config['consumer_key'];
84
        $path = 'blog/'.$this->options['base_hostname'].'/info?api_key='.$api_key;
85
        $result = $this->request($path);
86
        $json_result = json_decode($result, true);
87
88
        $mapping = [
89
            'id' => 'name',
90
            'link' => 'url',
91
            'title' => 'title',
92
            'name' => 'name',
93
            'description' => 'description',
94
            'ask' => 'ask',
95
            'ask_anon' => 'ask_anon',
96
            'followers' => 'followers'
97
        ];
98
99
        $blog = Blog::create($mapping, $json_result['response']['blog']);
100
101
        return $blog;
102
    }
103
104
105
    public function getPermissions()
106
    {
107
        return null;
108
    }
109
110
    public function getStats()
111
    {
112
        $profile = $this->getBlog();
113
114
        return $profile->followers;
115
    }
116
}
117