| @@ 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-113 (lines=83) @@ | ||
| 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 | * @param string $institution |
|
| 56 | * @param string $actorInstitution |
|
| 57 | * @param string $actorId |
|
| 58 | * @return null|RaListing |
|
| 59 | */ |
|
| 60 | public function get($id, $institution, $actorId) |
|
| 61 | { |
|
| 62 | $data = $this->service->get($id, $institution, $actorId); |
|
| 63 | ||
| 64 | if ($data === null) { |
|
| 65 | return null; |
|
| 66 | } |
|
| 67 | ||
| 68 | $raListing = RaListing::fromData($data); |
|
| 69 | $message = sprintf("RaListing '%s' retrieved from the Middleware is invalid", $id); |
|
| 70 | $this->assertIsValid($raListing, $message); |
|
| 71 | ||
| 72 | return $raListing; |
|
| 73 | } |
|
| 74 | ||
| 75 | /** |
|
| 76 | * @param RaListingSearchQuery $searchQuery |
|
| 77 | * @return RaListingCollection |
|
| 78 | */ |
|
| 79 | public function search(RaListingSearchQuery $searchQuery) |
|
| 80 | { |
|
| 81 | $data = $this->service->search($searchQuery); |
|
| 82 | ||
| 83 | if ($data === null) { |
|
| 84 | throw new InvalidResponseException( |
|
| 85 | 'Received a "null" as data when searching for RaListings, is the library service set up correctly?' |
|
| 86 | ); |
|
| 87 | } |
|
| 88 | ||
| 89 | $registrationAuthorities = RaListingCollection::fromData($data); |
|
| 90 | ||
| 91 | $this->assertIsValid( |
|
| 92 | $registrationAuthorities, |
|
| 93 | 'One or more registration authority listings retrieved from the Middleware were invalid' |
|
| 94 | ); |
|
| 95 | ||
| 96 | return $registrationAuthorities; |
|
| 97 | } |
|
| 98 | ||
| 99 | /** |
|
| 100 | * @param object $value |
|
| 101 | * @param null|string $message |
|
| 102 | */ |
|
| 103 | private function assertIsValid($value, $message = null) |
|
| 104 | { |
|
| 105 | $violations = $this->validator->validate($value); |
|
| 106 | ||
| 107 | $message = $message ?: 'Invalid Response Received'; |
|
| 108 | ||
| 109 | if (count($violations) > 0) { |
|
| 110 | throw InvalidResponseException::withViolations($message, $violations); |
|
| 111 | } |
|
| 112 | } |
|
| 113 | } |
|
| 114 | ||