Test Failed
Push — develop ( ace3b9...fc2314 )
by Nuno
03:41
created

ApiKeysRepository::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 4
ccs 0
cts 3
cp 0
crap 2
rs 10
c 0
b 0
f 0
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 function is_string;
17
use Illuminate\Contracts\Cache\Repository;
18
use Algolia\AlgoliaSearch\Interfaces\ClientInterface;
0 ignored issues
show
Bug introduced by
The type Algolia\AlgoliaSearch\Interfaces\ClientInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
19
20
/**
21
 * @internal
22
 */
23
final class ApiKeysRepository
24
{
25
    private const SEARCH_KEY = 'scout-extended.user-data.search-key';
26
27
    /**
28
     * @var \Illuminate\Contracts\Cache\Repository
29
     */
30
    private $cache;
31
32
    /**
33
     * @var \Algolia\AlgoliaSearch\Interfaces\ClientInterface
34
     */
35
    private $client;
36
37
    /**
38
     * ApiKeysRepository constructor.
39
     *
40
     * @param \Illuminate\Contracts\Cache\Repository $cache
41
     * @param \Algolia\AlgoliaSearch\Interfaces\ClientInterface $client
42
     *
43
     * @return void
44
     */
45
    public function __construct(Repository $cache, ClientInterface $client)
46
    {
47
        $this->cache = $cache;
48
        $this->client = $client;
49
    }
50
51
    /**
52
     * @param  string|object $searchable
53
     *
54
     * @return string
55
     */
56
    public function getSearchKey($searchable): string
57
    {
58
        $searchable = is_string($searchable) ? new $searchable : $searchable;
59
60
        $searchableAs = $searchable->searchableAs();
61
62
        $searchKey = $this->cache->get(self::SEARCH_KEY);
63
64
        if ($searchKey === null) {
65
66
            $id = config('app.name').'::searchKey';
67
68
            $keys = $this->client->listApiKeys()->getBody()['keys'];
69
            $searchKey = null;
70
71
            foreach ($keys as $key) {
72
                if ($key['description'] === $id) {
73
                    $searchKey = $key['value'];
74
                }
75
            }
76
77
            $searchKey = $searchKey ?? $this->client->addApiKey([
78
                    'acl' => ['search'],
79
                    'description' => config('app.name').'::searchKey',
80
                ])->getBody()['key'];
81
82
            $this->cache->put(self::SEARCH_KEY, $searchKey, 1440);
83
        }
84
85
        return $this->client::generateSecuredApiKey($searchKey, [
86
            'restrictIndices' => $searchableAs,
87
        ]);
88
    }
89
}
90