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

clientCredentialsExampleAction()   A

Complexity

Conditions 2
Paths 9

Size

Total Lines 20
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 12
nc 9
nop 0
dl 0
loc 20
ccs 0
cts 14
cp 0
crap 6
rs 9.8666
c 0
b 0
f 0
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