Resolve AT Protocol DIDs, handles, and schemas with intelligent caching for Laravel
1<?php
2
3namespace SocialDept\AtpResolver\Resolvers;
4
5use GuzzleHttp\Client;
6use GuzzleHttp\Exception\GuzzleException;
7use SocialDept\AtpResolver\Contracts\DidResolver;
8use SocialDept\AtpResolver\Data\DidDocument;
9use SocialDept\AtpResolver\Exceptions\DidResolutionException;
10use SocialDept\AtpResolver\Support\Concerns\HasConfig;
11use SocialDept\AtpResolver\Support\Concerns\ParsesDid;
12
13class PlcDidResolver implements DidResolver
14{
15 use HasConfig;
16 use ParsesDid;
17
18 protected Client $client;
19
20 protected string $plcDirectory;
21
22 /**
23 * Create a new PLC DID resolver instance.
24 *
25 * @param string $plcDirectory The PLC directory URL
26 */
27 public function __construct(?string $plcDirectory = null, ?int $timeout = null)
28 {
29 $this->plcDirectory = $plcDirectory ?? $this->getConfig('resolver.plc_directory', 'https://plc.directory');
30 $this->client = new Client([
31 'timeout' => $timeout ?? $this->getConfig('resolver.timeout', 10),
32 'headers' => [
33 'Accept' => 'application/json',
34 'User-Agent' => 'Beacon/1.0',
35 ],
36 ]);
37 }
38
39 /**
40 * Resolve a DID:PLC to a DID Document.
41 *
42 * @param string $did The DID to resolve (e.g., "did:plc:abc123")
43 */
44 public function resolve(string $did): DidDocument
45 {
46 if (! $this->supports($this->extractMethod($did))) {
47 throw DidResolutionException::unsupportedMethod($this->extractMethod($did));
48 }
49
50 try {
51 $response = $this->client->get("{$this->plcDirectory}/{$did}");
52 $data = json_decode($response->getBody()->getContents(), true);
53
54 if (! is_array($data)) {
55 throw DidResolutionException::resolutionFailed($did, 'Invalid response format');
56 }
57
58 return DidDocument::fromArray($data);
59 } catch (GuzzleException $e) {
60 throw DidResolutionException::resolutionFailed($did, $e->getMessage());
61 }
62 }
63
64 /**
65 * Check if this resolver supports the given DID method.
66 *
67 * @param string $method The DID method (e.g., "plc")
68 */
69 public function supports(string $method): bool
70 {
71 return $method === 'plc';
72 }
73}