Issues (144)

examples/provider/amazon.php (1 issue)

1
<?php
2
3
/**
4
 * Example of retrieving an authentication token of the Amazon service.
5
 *
6
 * PHP version 5.4
7
 *
8
 * @author     Flávio Heleno <[email protected]>
9
 * @license    http://www.opensource.org/licenses/mit-license.html  MIT License
10
 */
11
12
use OAuth\Common\Consumer\Credentials;
13
use OAuth\Common\Http\Client\CurlClient;
14
use OAuth\Common\Http\Exception\TokenResponseException;
15
use OAuth\Common\Storage\Session;
16
use OAuth\Helper\Example;
0 ignored issues
show
This use statement conflicts with another class in this namespace, Example. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
17
use OAuth\OAuth2\Service\Amazon;
18
19
require_once __DIR__ . '/../bootstrap.php';
20
21
$helper = new Example();
22
$storage = new Session();
23
$client = new CurlClient();
24
25
$helper->setTitle('Amazon');
26
if (empty($_GET)) {
27
    echo $helper->getContent();
28
} elseif (!empty($_GET['key']) && !empty($_GET['secret']) && $_GET['oauth'] !== 'redirect') {
29
    $credentials = new Credentials($_GET['key'], $_GET['secret'], $helper->getCurrentUrl());
30
    $service = new Amazon($credentials, $client, $storage);
31
    echo $helper->getHeader();
32
    echo '<a href="' . $service->getAuthorizationUri() . '">get access token</a>';
33
    echo $helper->getFooter();
34
} elseif (!empty($_GET['code'])) {
35
    $credentials = new Credentials($_GET['key'], $_GET['secret'], $helper->getCurrentUrl());
36
    $service = new Amazon($credentials, $client, $storage);
37
38
    echo $helper->getHeader();
39
40
    try {
41
        $token = $service->requestAccessToken($_GET['code']);
42
        echo 'access token: ' . $token->getAccessToken();
43
    } catch (TokenResponseException $exception) {
44
        $helper->getErrorMessage($exception);
45
    }
46
    echo $helper->getFooter();
47
}
48