Passed
Pull Request — master (#11)
by Simon
01:41
created

SlackAuthController   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 72
rs 10
c 0
b 0
f 0
wmc 5

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getQuery() 0 10 1
A getConfig() 0 11 2
A saveToken() 0 7 1
A index() 0 14 1
1
<?php
2
3
namespace Firesphere\StripeSlack\Controller;
4
5
use GuzzleHttp\Client;
6
use Psr\Http\Message\ResponseInterface;
7
use SilverStripe\Control\Controller;
8
use SilverStripe\Control\Director;
9
use SilverStripe\Control\HTTPRequest;
10
use SilverStripe\Core\Convert;
11
use SilverStripe\SiteConfig\SiteConfig;
12
use SilverStripe\ORM\ValidationException;
13
14
/**
15
 * Class SlackAuthController
16
 *
17
 */
18
class SlackAuthController extends Controller
19
{
20
21
    /**
22
     * It seems like a lot is going on here
23
     * But in reality, it's mainly just configuration stuff
24
     *
25
     * @param HTTPRequest $request
26
     * @throws ValidationException
27
     */
28
    public function index(HTTPRequest $request)
29
    {
30
        list($code, $config, $baseURL, $url) = $this->getConfig($request);
31
32
        $query = $this->getQuery($config, $code);
33
34
        // Setup and request the code
35
        $service = new Client(['base_uri' => $baseURL]);
36
        $response = $service->request('GET', $url . $query);
37
38
        $this->saveToken($response, $config);
39
40
        // A successful write should go back to the admin
41
        $this->redirect('/admin/settings#Root_Slack');
42
    }
43
44
    /**
45
     * @param $config
46
     * @param $code
47
     * @return string
48
     */
49
    public function getQuery($config, $code)
50
    {
51
        $params = [
52
            'client_id'     => $config->SlackClientID,
53
            'client_secret' => $config->SlackClientSecret,
54
            'code'          => $code,
55
            'redirect_uri'  => Director::absoluteURL('/SlackAuthorization/'),
56
        ];
57
58
        return http_build_query($params);
59
    }
60
61
    /**
62
     * @param ResponseInterface $response
63
     * @param SiteConfig $config
64
     * @throws ValidationException
65
     */
66
    public function saveToken($response, $config)
67
    {
68
        // Convert the JSON to use in our config (hidden from user view)
69
        $result = Convert::json2array($response->getBody());
70
71
        $config->SlackToken = $result['access_token'];
72
        $config->write();
73
    }
74
75
    /**
76
     * @param HTTPRequest $request
77
     * @return array
78
     */
79
    public function getConfig(HTTPRequest $request)
80
    {
81
        // Code param
82
        $code = $request->getVar('code');
83
        $config = SiteConfig::current_site_config();
84
        // Build the URL
85
        $baseURL = $config->SlackURL;
86
        $baseURL = (substr($baseURL, -1) === '/') ? $baseURL : $baseURL . '/';
87
        $url = 'api/oauth.access?';
88
89
        return array($code, $config, $baseURL, $url);
90
    }
91
}
92