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