Passed
Push — master ( 396e75...9009cf )
by Benjamin
07:10 queued 02:57
created

TwitterVariable::getUserProfileImageResourceUrl()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 2
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * @link      https://dukt.net/twitter/
4
 * @copyright Copyright (c) 2018, Dukt
5
 * @license   https://github.com/dukt/twitter/blob/master/LICENSE.md
6
 */
7
8
namespace dukt\twitter\web\twig\variables;
9
10
use Craft;
11
use craft\helpers\Json;
12
use dukt\twitter\Plugin;
13
use dukt\twitter\helpers\TwitterHelper;
14
use dukt\twitter\models\Tweet;
15
use GuzzleHttp\Exception\ClientException;
16
use GuzzleHttp\Exception\GuzzleException;
17
/**
18
 * Twitter Variable
19
 *
20
 * @author Dukt <[email protected]>
21
 * @since  3.0
22
 */
23
class TwitterVariable
24
{
25
    // Public Methods
26
    // =========================================================================
27
28
    /**
29
     * Perform a GET request on the Twitter API.
30
     *
31
     * @param string     $uri
32
     * @param array|null $query
33
     * @param array|null $headers
34
     * @param array      $options
35
     * @param null|bool  $enableCache
36
     * @param null|int   $cacheExpire
37
     *
38
     * @return array
39
     * @throws GuzzleException
40
     */
41
    public function get($uri, array $query = null, array $headers = null, $options = [], $enableCache = null, $cacheExpire = null): array
42
    {
43
        try {
44
            return [
45
                'success' => true,
46
                'data' => Plugin::getInstance()->getApi()->get($uri, $query, $headers, $options, $enableCache, $cacheExpire)
47
            ];
48
        } catch (ClientException $e) {
49
            Craft::error('Error requesting Twitter’s API: '.$e->getTraceAsString(), __METHOD__);
50
            return [
51
                'success' => false,
52
                'data' => Json::decodeIfJson($e->getResponse()->getBody()->getContents()),
53
            ];
54
        }
55
    }
56
57
    /**
58
     * Returns a tweet by its URL. Add query parameters to the API request with `query`.
59
     *
60
     * @param string|int $urlOrId
61
     * @param array|null $query
62
     *
63
     * @return Tweet|null
64
     * @throws \yii\base\Exception
65
     */
66
    public function getTweet($urlOrId, array $query = null)
67
    {
68
        try {
69
            return Plugin::getInstance()->getApi()->getTweet($urlOrId, $query);
70
        } catch (GuzzleException $e) {
71
            Craft::error('Couldn’t get tweet by URL: '.$e->getTraceAsString(), __METHOD__);
72
            return null;
73
        }
74
    }
75
76
    /**
77
     * Returns a Twitter user by its ID. Add query parameters to the API request with `query`.
78
     *
79
     * @param int   $remoteUserId
80
     * @param array $query
81
     *
82
     * @return array|null
83
     */
84
    public function getUserById($remoteUserId, $query = [])
85
    {
86
        try {
87
            return Plugin::getInstance()->getApi()->getUserById($remoteUserId, $query);
88
        } catch (GuzzleException $e) {
89
            Craft::error('Couldn’t get user by ID: '.$e->getTraceAsString(), __METHOD__);
90
            return null;
91
        }
92
    }
93
94
    /**
95
     * Returns a user image from a twitter user ID for given size. Default size is 48.
96
     *
97
     * @param int $remoteUserId
98
     * @param int $size
99
     *
100
     * @return string|null
101
     * @throws \craft\errors\ImageException
102
     * @throws \yii\base\Exception
103
     */
104
    public function getUserProfileImageResourceUrl($remoteUserId, $size = 48)
105
    {
106
        try {
107
            return TwitterHelper::getUserProfileImageResourceUrl($remoteUserId, $size);
108
        } catch (GuzzleException $e) {
109
            Craft::error('Couldn’t get user profile image resource URL: '.$e->getTraceAsString(), __METHOD__);
110
            return null;
111
        }
112
    }
113
114
    /**
115
     * Parses tweet data and returns a Tweet model.
116
     *
117
     * @param array $data Tweet’s API data as an array.
118
     * @return Tweet
119
     */
120
    public function parseTweetData(array $data): Tweet
121
    {
122
        return Plugin::getInstance()->getApi()->parseTweetData($data);
123
    }
124
}