Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
19 / 19 |
|
100.00% |
8 / 8 |
CRAP | |
100.00% |
1 / 1 |
ConfigProfileRepository | |
100.00% |
19 / 19 |
|
100.00% |
8 / 8 |
10 | |
100.00% |
1 / 1 |
__construct | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
repositoryType | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
repositoryName | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getProfile | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
hasProfile | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
listExposedProfiles | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
extractProfiles | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
extractConfig | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
3 |
1 | <?php |
2 | |
3 | namespace CirrusSearch\Profile; |
4 | |
5 | use Config; |
6 | |
7 | /** |
8 | * Profile repository backed by a Config object. |
9 | */ |
10 | class ConfigProfileRepository implements SearchProfileRepository { |
11 | |
12 | /** |
13 | * @var string |
14 | */ |
15 | private $type; |
16 | |
17 | /** |
18 | * @var string |
19 | */ |
20 | private $name; |
21 | |
22 | /** |
23 | * @var Config |
24 | */ |
25 | private $config; |
26 | |
27 | /** |
28 | * @var string |
29 | */ |
30 | private $configEntry; |
31 | |
32 | /** |
33 | * @param string $type |
34 | * @param string $name |
35 | * @param string $configEntry the name of config key holding the list of profiles |
36 | * @param Config $config |
37 | */ |
38 | public function __construct( $type, $name, $configEntry, Config $config ) { |
39 | $this->type = $type; |
40 | $this->name = $name; |
41 | $this->configEntry = $configEntry; |
42 | $this->config = $config; |
43 | } |
44 | |
45 | /** |
46 | * @return string |
47 | */ |
48 | public function repositoryType() { |
49 | return $this->type; |
50 | } |
51 | |
52 | /** |
53 | * The repository name |
54 | * @return string |
55 | */ |
56 | public function repositoryName() { |
57 | return $this->name; |
58 | } |
59 | |
60 | /** |
61 | * Load a profile named $name |
62 | * @param string $name |
63 | * @return array[]|null the profile data or null if not found |
64 | * @throws SearchProfileException |
65 | */ |
66 | public function getProfile( $name ) { |
67 | $profiles = $this->extractProfiles(); |
68 | return $profiles[$name] ?? null; |
69 | } |
70 | |
71 | /** |
72 | * Check if a profile named $name exists in this repository |
73 | * @param string $name |
74 | * @return bool |
75 | */ |
76 | public function hasProfile( $name ) { |
77 | return isset( $this->extractProfiles()[$name] ); |
78 | } |
79 | |
80 | /** |
81 | * Get the list of profiles that we want to expose to the user. |
82 | * |
83 | * @return array[] list of profiles index by name |
84 | */ |
85 | public function listExposedProfiles() { |
86 | return $this->extractProfiles(); |
87 | } |
88 | |
89 | private function extractProfiles(): array { |
90 | $configEntry = $this->configEntry; |
91 | $config = $this->config; |
92 | return self::extractConfig( $configEntry, $config ); |
93 | } |
94 | |
95 | /** |
96 | * @param string $configEntry |
97 | * @param Config $config |
98 | * @return array |
99 | * @internal For use by CompletionSearchProfileRepository only. |
100 | */ |
101 | public static function extractConfig( string $configEntry, Config $config ): array { |
102 | if ( !$config->has( $configEntry ) ) { |
103 | return []; |
104 | } |
105 | $profiles = $config->get( $configEntry ); |
106 | if ( !is_array( $profiles ) ) { |
107 | throw new SearchProfileException( "Config entry {$configEntry} must be an array or unset" ); |
108 | } |
109 | return $profiles; |
110 | } |
111 | |
112 | } |