src/DataProvider/AnalysisServiceWeightDataProvider.php line 66

Open in your IDE?
  1. <?php
  2. namespace App\DataProvider;
  3. use ApiPlatform\Core\DataProvider\CollectionDataProviderInterface;
  4. use ApiPlatform\Core\DataProvider\ItemDataProviderInterface;
  5. use ApiPlatform\Core\DataProvider\RestrictedDataProviderInterface;
  6. use App\DataTransformer\AnalysisServiceWeightTransformer;
  7. use App\Entity\AnalysisServiceWeight;
  8. use App\Provider\AnalysisServiceWeightProvider;
  9. use App\Request\AnalysisServiceWeightRequest;
  10. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  11. class AnalysisServiceWeightDataProvider implements CollectionDataProviderInterface, RestrictedDataProviderInterface, ItemDataProviderInterface
  12. {
  13. /**
  14. * @var AnalysisServiceWeightTransformer
  15. */
  16. private $transformer;
  17. /**
  18. * @var AnalysisServiceWeightProvider
  19. */
  20. private $provider;
  21. /**
  22. * AnalysisServiceWeightDataProvider constructor.
  23. *
  24. * @param AnalysisServiceWeightTransformer $transformer
  25. * @param AnalysisServiceWeightProvider $provider
  26. */
  27. public function __construct(AnalysisServiceWeightTransformer $transformer, AnalysisServiceWeightProvider $provider)
  28. {
  29. $this->transformer = $transformer;
  30. $this->provider = $provider;
  31. }
  32. public function getCollection(string $resourceClass, string $operationName = null)
  33. {
  34. $weights = $this->provider->getAnalysisServiceWeights();
  35. $data = $weights->map(
  36. function (AnalysisServiceWeight $weight) {
  37. return $this->transformer->transform($weight);
  38. }
  39. );
  40. return $data;
  41. }
  42. public function getItem(string $resourceClass, $id, string $operationName = null, array $context = [])
  43. {
  44. $weight = $this->provider->getAnalysisServiceWeightById($id);
  45. if ($weight === null) {
  46. throw new NotFoundHttpException();
  47. }
  48. return $this->transformer->transform($weight);
  49. }
  50. public function supports(string $resourceClass, string $operationName = null, array $context = []): bool
  51. {
  52. return AnalysisServiceWeightRequest::class === $resourceClass;
  53. }
  54. }