Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
80.00% |
12 / 15 |
|
71.43% |
5 / 7 |
CRAP | |
0.00% |
0 / 1 |
SearchProfileRepositoryTransformer | |
80.00% |
12 / 15 |
|
71.43% |
5 / 7 |
11.97 | |
0.00% |
0 / 1 |
__construct | |
71.43% |
5 / 7 |
|
0.00% |
0 / 1 |
4.37 | |||
repositoryType | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
repositoryName | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getProfile | |
100.00% |
1 / 1 |
|
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 | |||
transform | |
66.67% |
2 / 3 |
|
0.00% |
0 / 1 |
2.15 |
1 | <?php |
2 | |
3 | namespace CirrusSearch\Profile; |
4 | |
5 | /** |
6 | * Transforms profile content based on a transformer implementation. |
7 | */ |
8 | class SearchProfileRepositoryTransformer implements SearchProfileRepository { |
9 | |
10 | /** |
11 | * @var SearchProfileRepository |
12 | */ |
13 | private $repository; |
14 | |
15 | /** |
16 | * @var ArrayPathSetter |
17 | */ |
18 | private $transformer; |
19 | |
20 | /** |
21 | * @param SearchProfileRepository $repository |
22 | * @param array|ArrayPathSetter $transformer |
23 | */ |
24 | public function __construct( SearchProfileRepository $repository, $transformer ) { |
25 | if ( is_array( $transformer ) ) { |
26 | $transformer = new ArrayPathSetter( $transformer ); |
27 | } elseif ( !$transformer instanceof ArrayPathSetter ) { |
28 | throw new \InvalidArgumentException( '$transformer must be array|ArrayPathSetter, got ' . |
29 | ( is_object( $transformer ) ? get_class( $transformer ) : gettype( $transformer ) ) ); |
30 | } |
31 | $this->repository = $repository; |
32 | $this->transformer = $transformer; |
33 | } |
34 | |
35 | /** |
36 | * The repository type |
37 | * @return string |
38 | */ |
39 | public function repositoryType() { |
40 | return $this->repository->repositoryType(); |
41 | } |
42 | |
43 | /** |
44 | * The repository name |
45 | * @return string |
46 | */ |
47 | public function repositoryName() { |
48 | return $this->repository->repositoryName(); |
49 | } |
50 | |
51 | /** |
52 | * Load a profile named $name |
53 | * @param string $name |
54 | * @return array|null the profile data or null if not found |
55 | */ |
56 | public function getProfile( $name ) { |
57 | return $this->transform( $this->repository->getProfile( $name ) ); |
58 | } |
59 | |
60 | /** |
61 | * Check if a profile named $name exists in this repository |
62 | * @param string $name |
63 | * @return bool |
64 | */ |
65 | public function hasProfile( $name ) { |
66 | return $this->repository->hasProfile( $name ); |
67 | } |
68 | |
69 | /** |
70 | * Get the list of profiles that we want to expose to the user. |
71 | * |
72 | * @return array[] list of profiles indexed by name |
73 | */ |
74 | public function listExposedProfiles() { |
75 | return array_map( [ $this, 'transform' ], $this->repository->listExposedProfiles() ); |
76 | } |
77 | |
78 | /** |
79 | * Transform the profile |
80 | * @param array|null $profile |
81 | * @return array|null |
82 | */ |
83 | private function transform( array $profile = null ) { |
84 | if ( $profile === null ) { |
85 | return null; |
86 | } |
87 | return $this->transformer->transform( $profile ); |
88 | } |
89 | } |