@@ 31-110 (lines=80) @@ | ||
28 | /** |
|
29 | * Provides access to the Middleware API resources. |
|
30 | */ |
|
31 | class RaLocationService |
|
32 | { |
|
33 | /** |
|
34 | * @var LibraryRaLocationService |
|
35 | */ |
|
36 | private $service; |
|
37 | ||
38 | /** |
|
39 | * @var ValidatorInterface |
|
40 | */ |
|
41 | private $validator; |
|
42 | ||
43 | /** |
|
44 | * @param LibraryRaLocationService $service |
|
45 | * @param ValidatorInterface $validator |
|
46 | */ |
|
47 | public function __construct(LibraryRaLocationService $service, ValidatorInterface $validator) |
|
48 | { |
|
49 | $this->service = $service; |
|
50 | $this->validator = $validator; |
|
51 | } |
|
52 | ||
53 | /** |
|
54 | * @param string $id |
|
55 | * @return null|RaLocation |
|
56 | */ |
|
57 | public function get($id) |
|
58 | { |
|
59 | $data = $this->service->get($id); |
|
60 | ||
61 | if ($data === null) { |
|
62 | return null; |
|
63 | } |
|
64 | ||
65 | $raLocation = RaLocation::fromData($data); |
|
66 | $message = sprintf("RaLocation '%s' retrieved from the Middleware is invalid", $id); |
|
67 | $this->assertIsValid($raLocation, $message); |
|
68 | ||
69 | return $raLocation; |
|
70 | } |
|
71 | ||
72 | /** |
|
73 | * @param RaLocationSearchQuery $searchQuery |
|
74 | * @return RaLocationCollection |
|
75 | */ |
|
76 | public function search(RaLocationSearchQuery $searchQuery) |
|
77 | { |
|
78 | $data = $this->service->search($searchQuery); |
|
79 | ||
80 | if ($data === null) { |
|
81 | throw new InvalidResponseException( |
|
82 | 'Received a "null" as data when searching for RaLocations, is the library service set up correctly?' |
|
83 | ); |
|
84 | } |
|
85 | ||
86 | $registrationLocations = RaLocationCollection::fromData($data); |
|
87 | ||
88 | $this->assertIsValid( |
|
89 | $registrationLocations, |
|
90 | 'One or more registration authority locations retrieved from the Middleware were invalid' |
|
91 | ); |
|
92 | ||
93 | return $registrationLocations; |
|
94 | } |
|
95 | ||
96 | /** |
|
97 | * @param object $value |
|
98 | * @param null|string $message |
|
99 | */ |
|
100 | private function assertIsValid($value, $message = null) |
|
101 | { |
|
102 | $violations = $this->validator->validate($value); |
|
103 | ||
104 | $message = $message ?: 'Invalid Response Received'; |
|
105 | ||
106 | if (count($violations) > 0) { |
|
107 | throw InvalidResponseException::withViolations($message, $violations); |
|
108 | } |
|
109 | } |
|
110 | } |
|
111 |
@@ 31-110 (lines=80) @@ | ||
28 | /** |
|
29 | * Provides access to the Middleware API resources. |
|
30 | */ |
|
31 | class RaListingService |
|
32 | { |
|
33 | /** |
|
34 | * @var LibraryRaListingService |
|
35 | */ |
|
36 | private $service; |
|
37 | ||
38 | /** |
|
39 | * @var ValidatorInterface |
|
40 | */ |
|
41 | private $validator; |
|
42 | ||
43 | /** |
|
44 | * @param LibraryRaListingService $service |
|
45 | * @param ValidatorInterface $validator |
|
46 | */ |
|
47 | public function __construct(LibraryRaListingService $service, ValidatorInterface $validator) |
|
48 | { |
|
49 | $this->service = $service; |
|
50 | $this->validator = $validator; |
|
51 | } |
|
52 | ||
53 | /** |
|
54 | * @param string $id |
|
55 | * @return null|RaListing |
|
56 | */ |
|
57 | public function get($id) |
|
58 | { |
|
59 | $data = $this->service->get($id); |
|
60 | ||
61 | if ($data === null) { |
|
62 | return null; |
|
63 | } |
|
64 | ||
65 | $raListing = RaListing::fromData($data); |
|
66 | $message = sprintf("RaListing '%s' retrieved from the Middleware is invalid", $id); |
|
67 | $this->assertIsValid($raListing, $message); |
|
68 | ||
69 | return $raListing; |
|
70 | } |
|
71 | ||
72 | /** |
|
73 | * @param RaListingSearchQuery $searchQuery |
|
74 | * @return RaListingCollection |
|
75 | */ |
|
76 | public function search(RaListingSearchQuery $searchQuery) |
|
77 | { |
|
78 | $data = $this->service->search($searchQuery); |
|
79 | ||
80 | if ($data === null) { |
|
81 | throw new InvalidResponseException( |
|
82 | 'Received a "null" as data when searching for RaListings, is the library service set up correctly?' |
|
83 | ); |
|
84 | } |
|
85 | ||
86 | $registrationAuthorities = RaListingCollection::fromData($data); |
|
87 | ||
88 | $this->assertIsValid( |
|
89 | $registrationAuthorities, |
|
90 | 'One or more registration authority listings retrieved from the Middleware were invalid' |
|
91 | ); |
|
92 | ||
93 | return $registrationAuthorities; |
|
94 | } |
|
95 | ||
96 | /** |
|
97 | * @param object $value |
|
98 | * @param null|string $message |
|
99 | */ |
|
100 | private function assertIsValid($value, $message = null) |
|
101 | { |
|
102 | $violations = $this->validator->validate($value); |
|
103 | ||
104 | $message = $message ?: 'Invalid Response Received'; |
|
105 | ||
106 | if (count($violations) > 0) { |
|
107 | throw InvalidResponseException::withViolations($message, $violations); |
|
108 | } |
|
109 | } |
|
110 | } |
|
111 |