TweetDestroyer::withParams()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 10
rs 9.4285
cc 1
eloc 7
nc 1
nop 4
1
<?php namespace Tuiter;
2
3
use GuzzleHttp\Exception\RequestException;
4
5
/**
6
 * Class TweetDestroyer
7
 * @package Tuiter
8
 */
9
final class TweetDestroyer
10
{
11
    /**
12
     * @var TwitterClient
13
     */
14
    private $client;
15
16
    /**
17
     * @param TwitterClient $client
18
     */
19
    public function __construct(TwitterClient $client)
20
    {
21
        $this->client = $client;
22
    }
23
24
    /**
25
     * Factory
26
     *
27
     * @param $consumerKey
28
     * @param $consumerSecret
29
     * @param $token
30
     * @param $tokenSecret
31
     *
32
     * @return self
33
     */
34
    public static function withParams(
35
        $consumerKey,
36
        $consumerSecret,
37
        $token,
38
        $tokenSecret
39
    ) {
40
        return new self(TwitterClient::factory(
41
            $consumerKey, $consumerSecret, $token, $tokenSecret
42
        ));
43
    }
44
45
    /**
46
     * @param $tweetId
47
     *
48
     * @return mixed
49
     */
50
    public function destroy($tweetId)
51
    {
52
        $tweetId = $this->extractTweetId($tweetId);
53
54
        try {
55
            $response = $this->client->post("statuses/destroy/{$tweetId}.json");
56
        } catch (RequestException $e) {
57
            $response = $e->getResponse();
58
        }
59
60
        return $response->json();
61
    }
62
63
    /**
64
     * @param $tweetId
65
     *
66
     * @return string
67
     */
68
    private function extractTweetId($tweetId)
69
    {
70
        return $tweetId instanceof Tweet ? $tweetId->id : $tweetId;
71
    }
72
}
73