Passed
Push — master ( a5dd74...2fed3c )
by Derek Stephen
03:01
created

OfficialWebAppController   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Test Coverage

Coverage 52%

Importance

Changes 0
Metric Value
eloc 24
dl 0
loc 51
ccs 13
cts 25
cp 0.52
rs 10
c 0
b 0
f 0
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A registerAction() 0 4 1
A indexAction() 0 2 1
A clientCredentialsExampleAction() 0 33 2
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 GuzzleHttp\Client;
10
use Zend\Diactoros\Response\JsonResponse;
11
12
class OfficialWebAppController extends Controller
13
{
14
    public function indexAction()
15
    {
16
17
    }
18
19
    public function registerAction()
20
    {
21
        $form = new RegistrationForm('register');
22
        $this->view->form = $form;
23
    }
24
25
    /**
26
     * Sample page using client_credentials grant to connect to the API
27
     *
28
     * @throws \League\OAuth2\Client\Provider\Exception\IdentityProviderException
29
     */
30 1
    public function clientCredentialsExampleAction()
31
    {
32
        try {
33
            // This code fetches your access token
34
            // The self signed provider is for dev use only!
35 1
            $apiKeys = Registry::ahoy()->get('apiKeys');
36 1
            $keys = $apiKeys['clientCredentials'];
37
38 1
            $provider = new SelfSignedProvider([
39 1
                'clientId' => $keys['clientId'],
40 1
                'clientSecret' => $keys['clientSecret'],
41 1
                'redirectUri' => '',
42 1
                'urlAuthorize' => 'http://not-used-with-this-grant',
43 1
                'urlAccessToken' => $keys['baseURL'] . $keys['urlAccessToken'],
44 1
                'urlResourceOwnerDetails' => $keys['baseURL'] . $keys['urlResourceOwnerDetails'],
45
                'verify' => false,
46
            ]);
47
48 1
            $accessToken = $provider->getAccessToken('client_credentials', ['scope' => ['admin']]);
49
50
            // From here on we start calling the API
51
            $client = new Client(['verify' => false]);
52
            $response = $client->get('https://apache/client', [
53
                'headers' => [
54
                    'Authorization' => 'Bearer ' . $accessToken->getToken(),
55
                ],
56
            ]);
57
            $data = \json_decode($response->getBody()->getContents());
58
            $response = new JsonResponse($data);
59
60
            return $response; // usually the data would be sent to a view for display, but that's outwith the scope
61 1
        } catch (\Exception $e) {
62 1
            die(var_dump($e));
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...
Security Debugging Code introduced by
var_dump($e) looks like debug code. Are you sure you do not want to remove it?
Loading history...
Bug introduced by
Are you sure the usage of var_dump($e) is correct as it seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
63
        }
64
    }
65
66
}
67