AuthenticatorTrait::oauthForRequestTokenAsync()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 6
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 8
ccs 8
cts 8
cp 1
crap 1
rs 10
1
<?php
2
3
namespace mpyw\Cowitter\Traits;
4
5
use mpyw\Cowitter\Helpers\CurlExecutor;
6
use mpyw\Cowitter\Helpers\TokenParser;
7
use mpyw\Co\CoInterface;
8
9
trait AuthenticatorTrait
10
{
11
    abstract public function withCredentials(array $credentails);
12
    abstract protected function getInternalCurl();
13
    abstract protected function getInternalCredential();
14
15 3
    public function oauthForRequestTokenAsync($oauth_callback = null)
16 3
    {
17 3
        $obj = (yield CurlExecutor::execDecodedAsync($this->getInternalCurl()->oauthForRequestToken($oauth_callback)));
18 3
        yield CoInterface::RETURN_WITH => $this->withCredentials([
19 3
            $this->getInternalCredential()['consumer_key'],
20 3
            $this->getInternalCredential()['consumer_secret'],
21 3
            $obj->oauth_token,
22 3
            $obj->oauth_token_secret,
23
        ]);
24
        // @codeCoverageIgnoreStart
25
    }
26
    // @codeCoverageIgnoreEnd
27
28 2
    public function oauthForAccessTokenAsync($oauth_verifier)
29 2
    {
30 2
        $obj = (yield CurlExecutor::execDecodedAsync($this->getInternalCurl()->oauthForAccessToken($oauth_verifier)));
31 2
        yield CoInterface::RETURN_WITH => $this->withCredentials([
32 2
            $this->getInternalCredential()['consumer_key'],
33 2
            $this->getInternalCredential()['consumer_secret'],
34 2
            $obj->oauth_token,
35 2
            $obj->oauth_token_secret,
36
        ]);
37
        // @codeCoverageIgnoreStart
38
    }
39
    // @codeCoverageIgnoreEnd
40
41 1
    public function xauthForAccessTokenAsync($username, $password)
42 1
    {
43 1
        $obj = (yield CurlExecutor::execDecodedAsync($this->getInternalCurl()->xauthForAccessToken($username, $password)));
44 1
        yield CoInterface::RETURN_WITH => $this->withCredentials([
45 1
            $this->getInternalCredential()['consumer_key'],
46 1
            $this->getInternalCredential()['consumer_secret'],
47 1
            $obj->oauth_token,
48 1
            $obj->oauth_token_secret,
49
        ]);
50
        // @codeCoverageIgnoreStart
51
    }
52
    // @codeCoverageIgnoreEnd
53
54 3
    public function oauthForRequestToken($oauth_callback = null)
55 3
    {
56 3
        $obj = CurlExecutor::execDecoded($this->getInternalCurl()->oauthForRequestToken($oauth_callback));
57 3
        return $this->withCredentials([
58 3
            $this->getInternalCredential()['consumer_key'],
59 3
            $this->getInternalCredential()['consumer_secret'],
60 3
            $obj->oauth_token,
61 3
            $obj->oauth_token_secret,
62
        ]);
63
    }
64
65 2
    public function oauthForAccessToken($oauth_verifier)
66 2
    {
67 2
        $obj = CurlExecutor::execDecoded($this->getInternalCurl()->oauthForAccessToken($oauth_verifier));
68 2
        return $this->withCredentials([
69 2
            $this->getInternalCredential()['consumer_key'],
70 2
            $this->getInternalCredential()['consumer_secret'],
71 2
            $obj->oauth_token,
72 2
            $obj->oauth_token_secret,
73
        ]);
74
    }
75
76 1
    public function xauthForAccessToken($username, $password)
77 1
    {
78 1
        $obj = CurlExecutor::execDecoded($this->getInternalCurl()->xauthForAccessToken($username, $password));
79 1
        return $this->withCredentials([
80 1
            $this->getInternalCredential()['consumer_key'],
81 1
            $this->getInternalCredential()['consumer_secret'],
82 1
            $obj->oauth_token,
83 1
            $obj->oauth_token_secret,
84
        ]);
85
    }
86
87 2
    public function loginAsync($username, $password)
88 2
    {
89 2
        $author = (yield $this->oauthForRequestTokenAsync('oob'));
90 2
        $scraper = $this->getInternalCurl()->browsing();
91 2
        curl_setopt_array($scraper, [
92 2
            CURLOPT_HTTPGET => true,
93 2
            CURLOPT_URL     => $author->getAuthorizeUrl(true),
94
        ]);
95 2
        $authenticity_token = TokenParser::parseAuthenticityToken((yield CurlExecutor::execAsync($scraper)));
96 2
        curl_setopt_array($scraper, [
97 2
            CURLOPT_URL        => $author->getAuthorizeUrl(true),
98 2
            CURLOPT_POST       => true,
99 2
            CURLOPT_POSTFIELDS => http_build_query([
100 2
                'session[username_or_email]' => $username,
101 2
                'session[password]'          => $password,
102 2
                'authenticity_token'         => $authenticity_token,
103 2
            ], '', '&'),
104
        ]);
105 2
        $verifier = TokenParser::parseVerifier((yield CurlExecutor::execAsync($scraper)));
106 1
        yield CoInterface::RETURN_WITH => $author->oauthForAccessTokenAsync($verifier);
107
        // @codeCoverageIgnoreStart
108
    }
109
    // @codeCoverageIgnoreEnd
110
111 2
    public function login($username, $password)
112 2
    {
113 2
        $author = $this->oauthForRequestToken('oob');
114 2
        $scraper = $this->getInternalCurl()->browsing();
115 2
        curl_setopt_array($scraper, [
116 2
            CURLOPT_HTTPGET => true,
117 2
            CURLOPT_URL     => $author->getAuthorizeUrl(true),
118
        ]);
119 2
        $authenticity_token = TokenParser::parseAuthenticityToken(CurlExecutor::exec($scraper));
120 2
        curl_setopt_array($scraper, [
121 2
            CURLOPT_URL        => $author->getAuthorizeUrl(true),
122 2
            CURLOPT_POST       => true,
123 2
            CURLOPT_POSTFIELDS => http_build_query([
124 2
                'session[username_or_email]' => $username,
125 2
                'session[password]'          => $password,
126 2
                'authenticity_token'         => $authenticity_token,
127 2
            ], '', '&'),
128
        ]);
129 2
        $verifier = TokenParser::parseVerifier(CurlExecutor::exec($scraper));
130 1
        return $author->oauthForAccessToken($verifier);
131
    }
132
}
133