getClass(self::CLASS_URI); } /** * Returns the number of LtiProviders. * * @return int */ public function count() { return $this->queryResources([], 'count', 0); } /** * @inheritdoc */ public function findAll(): array { return $this->getProviders(); } /** * @inheritdoc */ public function searchByLabel(string $queryString): array { return $this->getProviders([OntologyRdfs::RDFS_LABEL => $queryString]); } /** * Retrieves providers from rdf store corresponding to the given criteria. * * @return LtiProvider[] */ private function getProviders(array $criteria = []): array { $resources = $this->queryResources($criteria, 'search', []); $ltiProviders = []; try { foreach ($resources as $resource) { $ltiProviders[] = $this->getLtiProviderFromResource($resource); } } catch (InvalidArgumentTypeException $exception) { $this->logError('Unable to retrieve provider properties: ' . $exception->getMessage()); return []; } return $ltiProviders; } /** * Retrieves resources from rdf store corresponding to the given criteria, * hydrate the result with the $hydration method * or returns $default on failure * * @param array $criteria * @param string $hydration Hydration method ("search" for actual results, "count" for counting results) * @param array|int $default default value to return on failure * * @return mixed */ private function queryResources(array $criteria, $hydration, $default) { try { /** @var ComplexSearchService $searchService */ $searchService = $this->getServiceLocator()->get(ComplexSearchService::SERVICE_ID); $queryBuilder = $searchService->query(); $query = $searchService->searchType($queryBuilder, self::CLASS_URI, true); if (count($criteria)) { foreach ($criteria as $property => $value) { $query->add($property)->contains($value); } } $queryBuilder->setCriteria($query); return $searchService->getGateway()->$hydration($queryBuilder); } catch (ErrorException $e) { $this->logError('Unable to retrieve providers: ' . $e->getMessage()); return $default; } } private function getLtiProviderFromResource(RdfResource $resource): LtiProvider { return $this->getLtiProviderFactory()->createFromResource($resource); } public function searchById(string $id): ?LtiProvider { $resource = $this->getResource($id); if ($resource->exists()) { $types = $resource->getTypes(); $type = reset($types); if ($type->getUri() !== self::CLASS_URI) { return null; } return $this->getLtiProviderFromResource($this->getResource($id)); } return null; } public function searchByOauthKey(string $oauthKey): ?LtiProvider { $providers = $this->getProviders([DataStore::PROPERTY_OAUTH_KEY => $oauthKey]); $count = count($providers); if ($count === 0) { return null; } if ($count > 1) { $this->logWarning("Found $count LTI providers with the same oauthKey: '$oauthKey'"); } return reset($providers); } public function searchByIssuer(string $issuer, string $clientId = null): ?LtiProvider { $criteria = [self::LTI_TOOL_AUDIENCE => $issuer]; if ($clientId !== null) { $criteria[self::LTI_TOOL_CLIENT_ID] = $clientId; } $providers = $this->getProviders($criteria); $count = count($providers); if ($count === 0) { return null; } if ($count > 1) { $this->logWarning(sprintf('Found %d LTI provider with the same clientId: %s and audience: %s', $count, $clientId, $issuer)); } return reset($providers); } private function getLtiProviderFactory(): LtiProviderFactory { return $this->getServiceLocator()->get(LtiProviderFactory::class); } }