Add provider DTO and API endpoint
This commit is contained in:
		
							parent
							
								
									7279f2096e
								
							
						
					
					
						commit
						ec23a41e37
					
				@ -2,3 +2,8 @@ api_v1:
 | 
			
		||||
  resource: ../src/Controller/Api/v1
 | 
			
		||||
  type: annotation
 | 
			
		||||
  prefix: /{provider}/api/v1
 | 
			
		||||
 | 
			
		||||
api_v1_providers:
 | 
			
		||||
  path: /api/v1/providers
 | 
			
		||||
  defaults:
 | 
			
		||||
    _controller: '\App\Controller\Api\v1\ProviderController::index'
 | 
			
		||||
 | 
			
		||||
@ -19,11 +19,14 @@ services:
 | 
			
		||||
        App\Provider\Provider:
 | 
			
		||||
            tags: [ app.provider ]
 | 
			
		||||
 | 
			
		||||
        App\Service\Converter:
 | 
			
		||||
            tags: [ app.converter ]
 | 
			
		||||
 | 
			
		||||
    # makes classes in src/ available to be used as services
 | 
			
		||||
    # this creates a service per class whose id is the fully-qualified class name
 | 
			
		||||
    App\:
 | 
			
		||||
        resource: '../src/*'
 | 
			
		||||
        exclude: '../src/{DependencyInjection,Exception,Modifie,Entity,Model,Migrations,Tests,Functions,Handler,Kernel.php}'
 | 
			
		||||
        exclude: '../src/{DependencyInjection,Exception,Modifier,Entity,Model,Migrations,Tests,Functions,Handler,Kernel.php}'
 | 
			
		||||
 | 
			
		||||
    # controllers are imported separately to make sure services can be injected
 | 
			
		||||
    # as action arguments even if you don't extend any base controller class
 | 
			
		||||
@ -72,19 +75,14 @@ services:
 | 
			
		||||
 | 
			
		||||
    ProxyManager\Configuration: '@proxy.config'
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    # converter
 | 
			
		||||
    App\Service\AggregateConverter:
 | 
			
		||||
        arguments:
 | 
			
		||||
          - !tagged_iterator app.converter
 | 
			
		||||
            - !tagged_iterator app.converter
 | 
			
		||||
 | 
			
		||||
    App\Service\Converter: '@App\Service\AggregateConverter'
 | 
			
		||||
 | 
			
		||||
    App\Service\EntityConverter:
 | 
			
		||||
        tags: ['app.converter']
 | 
			
		||||
 | 
			
		||||
    App\Service\ScheduledStopConverter:
 | 
			
		||||
        tags: ['app.converter']
 | 
			
		||||
 | 
			
		||||
    # serializer configuration
 | 
			
		||||
    App\Service\SerializerContextFactory:
 | 
			
		||||
        arguments:
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										31
									
								
								src/Controller/Api/v1/ProviderController.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										31
									
								
								src/Controller/Api/v1/ProviderController.php
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,31 @@
 | 
			
		||||
<?php
 | 
			
		||||
 | 
			
		||||
namespace App\Controller\Api\v1;
 | 
			
		||||
 | 
			
		||||
use App\Controller\Controller;
 | 
			
		||||
use App\Service\Converter;
 | 
			
		||||
use App\Service\ProviderResolver;
 | 
			
		||||
use Swagger\Annotations as SWG;
 | 
			
		||||
use Symfony\Component\Routing\Annotation\Route;
 | 
			
		||||
use function Kadet\Functional\ref;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * @Route("/providers")
 | 
			
		||||
 * @SWG\Tag(name="Providers")
 | 
			
		||||
 */
 | 
			
		||||
class ProviderController extends Controller
 | 
			
		||||
{
 | 
			
		||||
    /**
 | 
			
		||||
     * @Route("/", methods={"GET"})
 | 
			
		||||
     */
 | 
			
		||||
    public function index(ProviderResolver $resolver, Converter $converter)
 | 
			
		||||
    {
 | 
			
		||||
        $providers = $resolver
 | 
			
		||||
            ->all()
 | 
			
		||||
            ->map(ref([$converter, 'convert']))
 | 
			
		||||
            ->values()
 | 
			
		||||
            ->toArray()
 | 
			
		||||
        ;
 | 
			
		||||
        return $this->json($providers);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										101
									
								
								src/Model/Provider.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										101
									
								
								src/Model/Provider.php
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,101 @@
 | 
			
		||||
<?php
 | 
			
		||||
 | 
			
		||||
namespace App\Model;
 | 
			
		||||
 | 
			
		||||
use Carbon\Carbon;
 | 
			
		||||
use JMS\Serializer\Annotation as Serializer;
 | 
			
		||||
use Swagger\Annotations as SWG;
 | 
			
		||||
 | 
			
		||||
class Provider implements Fillable, Referable
 | 
			
		||||
{
 | 
			
		||||
    use FillTrait;
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Short identifier of provider, ex. "trojmiasto"
 | 
			
		||||
     * @SWG\Property(example="trojmiasto")
 | 
			
		||||
     * @Serializer\Type("string")
 | 
			
		||||
     * @var string
 | 
			
		||||
     */
 | 
			
		||||
    private $id;
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Full name of the provider, ex. "MZKZG Trójmiasto"
 | 
			
		||||
     * @SWG\Property(example="MZKZG Trójmiasto")
 | 
			
		||||
     * @Serializer\Type("string")
 | 
			
		||||
     * @var string
 | 
			
		||||
     */
 | 
			
		||||
    private $name;
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Short name of the provider for easier identification, ex. "Trójmiasto" or "Warszawa"
 | 
			
		||||
     * @SWG\Property(example="Trójmiasto")
 | 
			
		||||
     * @Serializer\Type("string")
 | 
			
		||||
     * @var string
 | 
			
		||||
     */
 | 
			
		||||
    private $shortName;
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Attribution to be presented for this provider, can contain HTML tags.
 | 
			
		||||
     * @SWG\Property(example="Copyright by XYZ inc.")
 | 
			
		||||
     * @Serializer\Type("string")
 | 
			
		||||
     * @var string|null
 | 
			
		||||
     */
 | 
			
		||||
    private $attribution;
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Time when data was last synchronized with this provider.
 | 
			
		||||
     * @Serializer\Type("Carbon")
 | 
			
		||||
     * @var Carbon|null
 | 
			
		||||
     */
 | 
			
		||||
    private $lastUpdate;
 | 
			
		||||
 | 
			
		||||
    public function getId(): string
 | 
			
		||||
    {
 | 
			
		||||
        return $this->id;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function setId(string $id): void
 | 
			
		||||
    {
 | 
			
		||||
        $this->id = $id;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function getName(): string
 | 
			
		||||
    {
 | 
			
		||||
        return $this->name;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function setName(string $name): void
 | 
			
		||||
    {
 | 
			
		||||
        $this->name = $name;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function getShortName(): string
 | 
			
		||||
    {
 | 
			
		||||
        return $this->shortName;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function setShortName(string $shortName): void
 | 
			
		||||
    {
 | 
			
		||||
        $this->shortName = $shortName;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function getAttribution(): ?string
 | 
			
		||||
    {
 | 
			
		||||
        return $this->attribution;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function setAttribution(?string $attribution): void
 | 
			
		||||
    {
 | 
			
		||||
        $this->attribution = $attribution;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function getLastUpdate(): ?Carbon
 | 
			
		||||
    {
 | 
			
		||||
        return $this->lastUpdate;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function setLastUpdate(?Carbon $lastUpdate): void
 | 
			
		||||
    {
 | 
			
		||||
        $this->lastUpdate = $lastUpdate;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@ -8,20 +8,19 @@ use function Kadet\Functional\Predicates\instance;
 | 
			
		||||
class AggregateConverter implements Converter, CacheableConverter
 | 
			
		||||
{
 | 
			
		||||
    private $converters;
 | 
			
		||||
    private $cachedConverters;
 | 
			
		||||
 | 
			
		||||
    public function __construct(iterable $converters)
 | 
			
		||||
    {
 | 
			
		||||
        $this->converters = collect($converters)->each(function (Converter $converter) {
 | 
			
		||||
            if ($converter instanceof RecursiveConverter) {
 | 
			
		||||
                $converter->setParent($this);
 | 
			
		||||
            }
 | 
			
		||||
        });
 | 
			
		||||
        $this->converters = $converters;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function convert($entity)
 | 
			
		||||
    {
 | 
			
		||||
        $this->ensureCachedConverters();
 | 
			
		||||
 | 
			
		||||
        /** @var Converter $converter */
 | 
			
		||||
        $converter = $this->converters->first(function (Converter $converter) use ($entity) {
 | 
			
		||||
        $converter = $this->cachedConverters->first(function (Converter $converter) use ($entity) {
 | 
			
		||||
            return $converter->supports($entity);
 | 
			
		||||
        });
 | 
			
		||||
 | 
			
		||||
@ -41,7 +40,9 @@ class AggregateConverter implements Converter, CacheableConverter
 | 
			
		||||
 | 
			
		||||
    public function getConverters(): Collection
 | 
			
		||||
    {
 | 
			
		||||
        return clone $this->converters;
 | 
			
		||||
        $this->ensureCachedConverters();
 | 
			
		||||
 | 
			
		||||
        return clone $this->cachedConverters;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function flushCache()
 | 
			
		||||
@ -57,8 +58,25 @@ class AggregateConverter implements Converter, CacheableConverter
 | 
			
		||||
 | 
			
		||||
    public function __clone()
 | 
			
		||||
    {
 | 
			
		||||
        $this->converters = $this->converters->map(function ($object) {
 | 
			
		||||
        $this->ensureCachedConverters();
 | 
			
		||||
 | 
			
		||||
        $this->cachedConverters = $this->cachedConverters->map(function ($object) {
 | 
			
		||||
            return clone $object;
 | 
			
		||||
        });
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private function ensureCachedConverters()
 | 
			
		||||
    {
 | 
			
		||||
        if (!$this->cachedConverters) {
 | 
			
		||||
            $this->cachedConverters = collect($this->converters)
 | 
			
		||||
                ->filter(function (Converter $converter) {
 | 
			
		||||
                    return $converter !== $this;
 | 
			
		||||
                })
 | 
			
		||||
                ->each(function (Converter $converter) {
 | 
			
		||||
                    if ($converter instanceof RecursiveConverter) {
 | 
			
		||||
                        $converter->setParent($this);
 | 
			
		||||
                    }
 | 
			
		||||
                });
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										27
									
								
								src/Service/ProviderConverter.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										27
									
								
								src/Service/ProviderConverter.php
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,27 @@
 | 
			
		||||
<?php
 | 
			
		||||
 | 
			
		||||
namespace App\Service;
 | 
			
		||||
 | 
			
		||||
use App\Model\Provider as ProviderDTO;
 | 
			
		||||
use App\Provider\Provider;
 | 
			
		||||
 | 
			
		||||
class ProviderConverter implements Converter
 | 
			
		||||
{
 | 
			
		||||
    public function convert($entity)
 | 
			
		||||
    {
 | 
			
		||||
        /** @var Provider $entity */
 | 
			
		||||
 | 
			
		||||
        return ProviderDTO::createFromArray([
 | 
			
		||||
            'id'          => $entity->getIdentifier(),
 | 
			
		||||
            'shortName'   => $entity->getShortName(),
 | 
			
		||||
            'name'        => $entity->getName(),
 | 
			
		||||
            'attribution' => $entity->getAttribution(),
 | 
			
		||||
            'lastUpdate'  => $entity->getLastUpdate() ? clone $entity->getLastUpdate() : null,
 | 
			
		||||
        ]);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function supports($entity)
 | 
			
		||||
    {
 | 
			
		||||
        return $entity instanceof Provider;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user