Completed
Push — master ( 7b61ef...71e756 )
by Florent
08:30 queued 07:08
created

src/Bundle/JoseFramework/Services/JWSLoader.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * The MIT License (MIT)
7
 *
8
 * Copyright (c) 2014-2019 Spomky-Labs
9
 *
10
 * This software may be modified and distributed under the terms
11
 * of the MIT license.  See the LICENSE file for details.
12
 */
13
14
namespace Jose\Bundle\JoseFramework\Services;
15
16
use Jose\Bundle\JoseFramework\Event\JWSLoadingFailureEvent;
17
use Jose\Bundle\JoseFramework\Event\JWSLoadingSuccessEvent;
18
use Jose\Component\Checker\HeaderCheckerManager;
0 ignored issues
show
This use statement conflicts with another class in this namespace, Jose\Bundle\JoseFramewor...es\HeaderCheckerManager.

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...
19
use Jose\Component\Core\JWKSet;
20
use Jose\Component\Signature\JWS;
21
use Jose\Component\Signature\JWSLoader as BaseJWSLoader;
22
use Jose\Component\Signature\JWSVerifier;
0 ignored issues
show
This use statement conflicts with another class in this namespace, Jose\Bundle\JoseFramework\Services\JWSVerifier.

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...
23
use Jose\Component\Signature\Serializer\JWSSerializerManager;
24
use Psr\EventDispatcher\EventDispatcherInterface;
25
use Throwable;
26
27
final class JWSLoader extends BaseJWSLoader
28
{
29
    /**
30
     * @var EventDispatcherInterface
31
     */
32
    private $eventDispatcher;
33
34
    public function __construct(JWSSerializerManager $serializerManager, JWSVerifier $jwsVerifier, ?HeaderCheckerManager $headerCheckerManager, EventDispatcherInterface $eventDispatcher)
35
    {
36
        parent::__construct($serializerManager, $jwsVerifier, $headerCheckerManager);
37
        $this->eventDispatcher = $eventDispatcher;
38
    }
39
40
    public function loadAndVerifyWithKeySet(string $token, JWKSet $keyset, ?int &$signature, ?string $payload = null): JWS
41
    {
42
        try {
43
            $jws = parent::loadAndVerifyWithKeySet($token, $keyset, $signature, $payload);
44
            $this->eventDispatcher->dispatch(new JWSLoadingSuccessEvent(
45
                $token,
46
                $jws,
47
                $keyset,
48
                $signature
49
            ));
50
51
            return $jws;
52
        } catch (Throwable $throwable) {
53
            $this->eventDispatcher->dispatch(new JWSLoadingFailureEvent(
54
                $token,
55
                $keyset,
56
                $throwable
57
            ));
58
59
            throw $throwable;
60
        }
61
    }
62
}
63