1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace RemotelyLiving\Doorkeeper\Logger; |
6
|
|
|
|
7
|
|
|
use RemotelyLiving\Doorkeeper\Identification; |
8
|
|
|
use RemotelyLiving\Doorkeeper\RequestorInterface; |
9
|
|
|
|
10
|
|
|
final class Processor |
11
|
|
|
{ |
12
|
|
|
public const CONTEXT_KEY_REQUESTOR = 'requestor'; |
13
|
|
|
public const CONTEXT_KEY_IDENTIFIERS = 'identifiers'; |
14
|
|
|
public const FEATURE_ID = 'featureId'; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var string[] |
18
|
|
|
*/ |
19
|
|
|
private $filteredIdentities = []; |
20
|
|
|
|
21
|
|
|
public function __invoke(array $record): array |
22
|
|
|
{ |
23
|
|
|
if ( |
24
|
|
|
!isset($record['context'][static::CONTEXT_KEY_REQUESTOR]) |
25
|
|
|
|| !$record['context'][static::CONTEXT_KEY_REQUESTOR] instanceof RequestorInterface |
26
|
|
|
) { |
27
|
|
|
return $record; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** @var \RemotelyLiving\Doorkeeper\RequestorInterface $requestor */ |
31
|
|
|
$requestor = $record['context'][static::CONTEXT_KEY_REQUESTOR]; |
32
|
|
|
$requestorContext = []; |
33
|
|
|
|
34
|
|
|
foreach ($requestor->getIdentificationCollections() as $identificationCollection) { |
35
|
|
|
foreach ($identificationCollection as $id) { |
36
|
|
|
if (isset($this->filteredIdentities[get_class($id)])) { |
37
|
|
|
continue; |
38
|
|
|
} |
39
|
|
|
$key = $this->getKeyFromIdentification($id); |
40
|
|
|
$requestorContext[self::CONTEXT_KEY_IDENTIFIERS][$key][] = $id->getIdentifier(); |
41
|
|
|
} |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
$requestorContext = $this->flattenIdCollections($requestorContext); |
45
|
|
|
$record['context'][static::CONTEXT_KEY_REQUESTOR] = $requestorContext; |
46
|
|
|
|
47
|
|
|
return $record; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function setFilteredIdentityTypes(array $idTypes): void |
51
|
|
|
{ |
52
|
|
|
$this->filteredIdentities = array_flip($idTypes); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
private function getKeyFromIdentification(Identification\IdentificationInterface $identity): string |
56
|
|
|
{ |
57
|
|
|
$classPaths = explode('\\', $identity->getType()); |
58
|
|
|
|
59
|
|
|
return (string) array_pop($classPaths); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
private function flattenIdCollections(array $requestorContext): array |
63
|
|
|
{ |
64
|
|
|
if (!isset($requestorContext[self::CONTEXT_KEY_IDENTIFIERS])) { |
65
|
|
|
return $requestorContext; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
$flattened = []; |
69
|
|
|
|
70
|
|
|
foreach ($requestorContext[self::CONTEXT_KEY_IDENTIFIERS] as $idName => $identifiers) { |
71
|
|
|
$flattened[$idName] = $identifiers; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
$requestorContext[self::CONTEXT_KEY_IDENTIFIERS] = json_encode($flattened); |
75
|
|
|
|
76
|
|
|
return $requestorContext; |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|