Completed
Push — master ( eca401...a54619 )
by Derek Stephen
02:57
created

OfficialWebAppController   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 15
dl 0
loc 38
ccs 0
cts 22
cp 0
rs 10
c 0
b 0
f 0
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A clientCredentialsExampleAction() 0 20 2
A registerAction() 0 4 1
A indexAction() 0 2 1
1
<?php
2
3
namespace App\Controller;
4
5
use App\Form\User\RegistrationForm;
6
use App\OAuth\SelfSignedProvider;
7
use Bone\Mvc\Controller;
8
use Bone\Mvc\Registry;
9
use Zend\Diactoros\Response\JsonResponse;
10
11
class OfficialWebAppController extends Controller
12
{
13
    public function indexAction()
14
    {
15
16
    }
17
18
    public function registerAction()
19
    {
20
        $form = new RegistrationForm('register');
21
        $this->view->form = $form;
22
    }
23
24
    /**
25
     * Sample page using client_credentials grant to connect to the API
26
     *
27
     * @throws \League\OAuth2\Client\Provider\Exception\IdentityProviderException
28
     */
29
    public function clientCredentialsExampleAction()
30
    {
31
        try {
32
            // This code fetches your access token
33
            // The self signed provider is for dev use only!
34
            $apiKeys = Registry::ahoy()->get('apiKeys');
35
            $options = $apiKeys['clientCredentials'];
36
37
            $provider = new SelfSignedProvider($options);
38
39
            $accessToken = $provider->getAccessToken('client_credentials', ['scope' => ['admin']]);
40
            $request = $provider->getAuthenticatedRequest('GET', $options['host'] . '/client', $accessToken);
41
            $response = $provider->getResponse($request);
42
43
            $data = \json_decode($response->getBody()->getContents());
44
            $response = new JsonResponse($data);
45
46
            return $response; // usually the data would be sent to a view for display, but that's outwith the scope
47
        } catch (\Exception $e) {
48
            die($e->getCode() . $e->getMessage() .  $e->getTraceAsString());
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
49
        }
50
    }
51
52
}
53