Passed
Push — develop ( bb4de3...f6c43e )
by Benjamin
06:52
created

FieldsController::actionTweetFieldPreview()   B

Complexity

Conditions 5
Paths 13

Size

Total Lines 32
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 18
nc 13
nop 0
dl 0
loc 32
rs 8.439
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\controllers;
9
10
use Craft;
11
use craft\helpers\Json;
12
use craft\web\Controller;
13
use dukt\twitter\errors\InvalidTweetException;
14
use dukt\twitter\Plugin;
15
use GuzzleHttp\Exception\GuzzleException;
16
use yii\web\Response;
17
18
/**
19
 * API controller
20
 *
21
 * @author Dukt <[email protected]>
22
 * @since  3.0
23
 */
24
class FieldsController extends Controller
25
{
26
    // Public Methods
27
    // =========================================================================
28
29
    /**
30
     * Tweet field preview.
31
     *
32
     * @return Response
33
     * @throws \Twig_Error_Loader
34
     * @throws \yii\base\Exception
35
     */
36
    public function actionTweetFieldPreview(): Response
37
    {
38
        $tweetId = Craft::$app->getRequest()->getParam('id');
39
40
        try {
41
            $tweet = Plugin::getInstance()->getApi()->getTweet($tweetId);
0 ignored issues
show
Bug introduced by
It seems like $tweetId can also be of type array; however, parameter $urlOrId of dukt\twitter\services\Api::getTweet() does only seem to accept integer|string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

41
            $tweet = Plugin::getInstance()->getApi()->getTweet(/** @scrutinizer ignore-type */ $tweetId);
Loading history...
42
43
            if (!$tweet) {
44
                throw new InvalidTweetException('No status found with that ID.');
45
            }
46
47
            $html = Craft::$app->getView()->renderTemplate('twitter/_components/tweet', [
48
                'tweet' => $tweet
49
            ]);
50
51
            return $this->asJson([
52
                'html' => $html,
53
            ]);
54
        } catch (GuzzleException $e) {
55
            $data = Json::decodeIfJson($e->getResponse()->getBody()->getContents());
0 ignored issues
show
Bug introduced by
The method getResponse() does not exist on GuzzleHttp\Exception\GuzzleException. It seems like you code against a sub-type of GuzzleHttp\Exception\GuzzleException such as GuzzleHttp\Exception\RequestException. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

55
            $data = Json::decodeIfJson($e->/** @scrutinizer ignore-call */ getResponse()->getBody()->getContents());
Loading history...
56
57
            if (isset($data['errors'][0]['message'])) {
58
                return $this->asErrorJson($data['errors'][0]['message']);
59
            }
60
61
            Craft::error('Couldn’ load tweet preview: '.$e->getTraceAsString(), __METHOD__);
62
63
            return $this->asErrorJson($e->getMessage());
64
        } catch (InvalidTweetException $e) {
65
            Craft::error('Couldn’ load tweet preview: '.$e->getTraceAsString(), __METHOD__);
66
67
            return $this->asErrorJson($e->getMessage());
68
        }
69
    }
70
}
71