1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* This file is part of Scout Extended. |
7
|
|
|
* |
8
|
|
|
* (c) Algolia Team <[email protected]> |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Algolia\ScoutExtended\Repositories; |
15
|
|
|
|
16
|
|
|
use DateInterval; |
17
|
|
|
use function is_string; |
18
|
|
|
use Algolia\AlgoliaSearch\SearchClient; |
19
|
|
|
use Illuminate\Contracts\Cache\Repository; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @internal |
23
|
|
|
*/ |
24
|
|
|
final class ApiKeysRepository |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* Holds the search key. |
28
|
|
|
*/ |
29
|
|
|
private const SEARCH_KEY = 'scout-extended.user-data.search-key'; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var \Illuminate\Contracts\Cache\Repository |
33
|
|
|
*/ |
34
|
|
|
private $cache; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var \Algolia\AlgoliaSearch\SearchClient |
38
|
|
|
*/ |
39
|
|
|
private $client; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* ApiKeysRepository constructor. |
43
|
|
|
* |
44
|
|
|
* @param \Illuminate\Contracts\Cache\Repository $cache |
45
|
|
|
* @param \Algolia\AlgoliaSearch\SearchClient $client |
46
|
|
|
* |
47
|
|
|
* @return void |
48
|
|
|
*/ |
49
|
3 |
|
public function __construct(Repository $cache, SearchClient $client) |
50
|
|
|
{ |
51
|
3 |
|
$this->cache = $cache; |
52
|
3 |
|
$this->client = $client; |
53
|
3 |
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param string|object $searchable |
57
|
|
|
* |
58
|
|
|
* @return string |
59
|
|
|
*/ |
60
|
3 |
|
public function getSearchKey($searchable): string |
61
|
|
|
{ |
62
|
3 |
|
$searchable = is_string($searchable) ? new $searchable : $searchable; |
63
|
|
|
|
64
|
3 |
|
$searchableAs = $searchable->searchableAs(); |
65
|
|
|
|
66
|
3 |
|
$securedSearchKey = $this->cache->get(self::SEARCH_KEY.'.'.$searchableAs); |
67
|
|
|
|
68
|
3 |
|
if ($securedSearchKey === null) { |
69
|
3 |
|
$id = config('app.name').'::searchKey'; |
70
|
|
|
|
71
|
3 |
|
$keys = $this->client->listApiKeys()['keys']; |
72
|
|
|
|
73
|
3 |
|
$searchKey = null; |
74
|
|
|
|
75
|
3 |
|
foreach ($keys as $key) { |
76
|
2 |
|
if (array_key_exists('description', $key) && $key['description'] === $id) { |
77
|
2 |
|
$searchKey = $key['value']; |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
81
|
3 |
|
$searchKey = $searchKey ?? $this->client->addApiKey(['search'], [ |
82
|
2 |
|
'description' => config('app.name').'::searchKey', |
83
|
3 |
|
])->getBody()['key']; |
84
|
|
|
|
85
|
|
|
// Key will be valid for 25 hours. |
86
|
3 |
|
$validUntil = time() + (3600 * 25); |
87
|
|
|
|
88
|
3 |
|
$securedSearchKey = $this->client::generateSecuredApiKey($searchKey, [ |
89
|
3 |
|
'restrictIndices' => $searchableAs, |
90
|
3 |
|
'validUntil' => $validUntil, |
91
|
|
|
]); |
92
|
|
|
|
93
|
3 |
|
$this->cache->put( |
94
|
3 |
|
self::SEARCH_KEY.'.'.$searchableAs, $securedSearchKey, DateInterval::createFromDateString('24 hours') |
95
|
|
|
); |
96
|
|
|
} |
97
|
|
|
|
98
|
3 |
|
return $securedSearchKey; |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|