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

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

Labels
Severity

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\JWELoadingFailureEvent;
17
use Jose\Bundle\JoseFramework\Event\JWELoadingSuccessEvent;
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\Encryption\JWE;
21
use Jose\Component\Encryption\JWEDecrypter;
0 ignored issues
show
This use statement conflicts with another class in this namespace, Jose\Bundle\JoseFramework\Services\JWEDecrypter.

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...
22
use Jose\Component\Encryption\JWELoader as BaseJWELoader;
23
use Jose\Component\Encryption\Serializer\JWESerializerManager;
24
use Psr\EventDispatcher\EventDispatcherInterface;
25
use Throwable;
26
27
final class JWELoader extends BaseJWELoader
28
{
29
    /**
30
     * @var EventDispatcherInterface
31
     */
32
    private $eventDispatcher;
33
34
    public function __construct(JWESerializerManager $serializerManager, JWEDecrypter $jweDecrypter, ?HeaderCheckerManager $headerCheckerManager, EventDispatcherInterface $eventDispatcher)
35
    {
36
        parent::__construct($serializerManager, $jweDecrypter, $headerCheckerManager);
37
        $this->eventDispatcher = $eventDispatcher;
38
    }
39
40
    public function loadAndDecryptWithKeySet(string $token, JWKSet $keyset, ?int &$recipient): JWE
41
    {
42
        try {
43
            $jwe = parent::loadAndDecryptWithKeySet($token, $keyset, $recipient);
44
            $this->eventDispatcher->dispatch(new JWELoadingSuccessEvent(
45
                $token,
46
                $jwe,
47
                $keyset,
48
                $recipient
49
            ));
50
51
            return $jwe;
52
        } catch (Throwable $throwable) {
53
            $this->eventDispatcher->dispatch(new JWELoadingFailureEvent(
54
                $token,
55
                $keyset,
56
                $throwable
57
            ));
58
59
            throw $throwable;
60
        }
61
    }
62
}
63