Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
88.00% |
22 / 25 |
|
90.00% |
9 / 10 |
CRAP | |
0.00% |
0 / 1 |
| ArrayProfileRepository | |
88.00% |
22 / 25 |
|
90.00% |
9 / 10 |
14.34 | |
0.00% |
0 / 1 |
| fromArray | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| lazyLoaded | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| fromFile | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
| __construct | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
2 | |||
| repositoryType | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| repositoryName | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getProfile | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
| hasProfile | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| listExposedProfiles | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getProfiles | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
3 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace CirrusSearch\Profile; |
| 4 | |
| 5 | /** |
| 6 | * Simple profile repository backed by a PHP array |
| 7 | */ |
| 8 | class ArrayProfileRepository implements SearchProfileRepository { |
| 9 | |
| 10 | /** |
| 11 | * @var string |
| 12 | */ |
| 13 | private $name; |
| 14 | |
| 15 | /** |
| 16 | * @var string |
| 17 | */ |
| 18 | private $type; |
| 19 | |
| 20 | /** |
| 21 | * @var array[]|null |
| 22 | */ |
| 23 | private $profiles; |
| 24 | |
| 25 | /** |
| 26 | * @var callable|null |
| 27 | */ |
| 28 | private $callback; |
| 29 | |
| 30 | /** |
| 31 | * @param string $repoType |
| 32 | * @param string $repoName |
| 33 | * @param array $profiles |
| 34 | * @return self |
| 35 | */ |
| 36 | public static function fromArray( $repoType, $repoName, array $profiles ): self { |
| 37 | return new self( $repoType, $repoName, $profiles ); |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * Lazy loaded array using a callback |
| 42 | * @param string $repoType |
| 43 | * @param string $repoName |
| 44 | * @param callable $loader |
| 45 | * @return self |
| 46 | */ |
| 47 | public static function lazyLoaded( $repoType, $repoName, callable $loader ): self { |
| 48 | return new self( $repoType, $repoName, $loader ); |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Lazy loaded array by including a php file. |
| 53 | * |
| 54 | * <b>NOTE:</b> $phpFile will be loaded using PHP's require function |
| 55 | * |
| 56 | * @param string $repoType |
| 57 | * @param string $repoName |
| 58 | * @param string $phpFile |
| 59 | * @return self |
| 60 | */ |
| 61 | public static function fromFile( $repoType, $repoName, $phpFile ): self { |
| 62 | return self::lazyLoaded( $repoType, $repoName, static function () use ( $phpFile ) { |
| 63 | return require $phpFile; |
| 64 | } ); |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * @param string $repoType |
| 69 | * @param string $repoName |
| 70 | * @param array[]|callable $profilesOrCallback |
| 71 | */ |
| 72 | private function __construct( $repoType, $repoName, $profilesOrCallback ) { |
| 73 | $this->type = $repoType; |
| 74 | $this->name = $repoName; |
| 75 | if ( is_callable( $profilesOrCallback ) ) { |
| 76 | $this->callback = $profilesOrCallback; |
| 77 | } else { |
| 78 | $this->profiles = $profilesOrCallback; |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * The repository type |
| 84 | * @return string |
| 85 | */ |
| 86 | public function repositoryType() { |
| 87 | return $this->type; |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * The repository name |
| 92 | * @return string |
| 93 | */ |
| 94 | public function repositoryName() { |
| 95 | return $this->name; |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Load a profile named $name |
| 100 | * @param string $name |
| 101 | * @return array|null the profile data or null if not found |
| 102 | */ |
| 103 | public function getProfile( $name ) { |
| 104 | if ( $this->hasProfile( $name ) ) { |
| 105 | return $this->getProfiles()[$name]; |
| 106 | } |
| 107 | return null; |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Check if a profile named $name exists in this repository |
| 112 | * @param string $name |
| 113 | * @return bool |
| 114 | */ |
| 115 | public function hasProfile( $name ) { |
| 116 | return isset( $this->getProfiles()[$name] ); |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * @return array[] |
| 121 | */ |
| 122 | public function listExposedProfiles() { |
| 123 | return $this->getProfiles(); |
| 124 | } |
| 125 | |
| 126 | private function getProfiles(): array { |
| 127 | if ( $this->profiles === null ) { |
| 128 | $profiles = ( $this->callback )(); |
| 129 | if ( !is_array( $profiles ) ) { |
| 130 | throw new SearchProfileException( "Loader callback for repository " . |
| 131 | $this->name . " of type " . $this->type . |
| 132 | " resolved to " . get_debug_type( $profiles ) . " but expected an array." ); |
| 133 | } |
| 134 | $this->profiles = $profiles; |
| 135 | } |
| 136 | return $this->profiles; |
| 137 | } |
| 138 | } |