Commit 8b33818f authored by Guillaume Perréal's avatar Guillaume Perréal
Browse files

CS.

parent 59792f70
No related merge requests found
Showing with 166 additions and 89 deletions
+166 -89
<?php declare(strict_types=1);
/**
/*
* This file is part of "irstea/api-metadata".
*
* Copyright (C) 2019 IRSTEA
* All rights reserved.
*
* @copyright 2019 IRSTEA
* @author guillaume.perreal
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by the Free
* Software Foundation, either version 3 of the License, or (at your option) any
* later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License and the GNU
* Lesser General Public License along with this program. If not, see
* <https://www.gnu.org/licenses/>.
*/
namespace Irstea\ApiMetadata\Action;
use Irstea\ApiMetadata\Exception\ResourceNotFoundException;
use Irstea\ApiMetadata\Factory\Context;
use Irstea\ApiMetadata\Factory\Operation\OperationFactoryInterface;
......@@ -19,7 +28,7 @@ use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;
/**
* Class OperationGetAction
* Class OperationGetAction.
*/
class OperationGetAction
{
......@@ -34,14 +43,14 @@ class OperationGetAction
/**
* OperationListAction constructor.
* @param ResourceMapInterface $resourceMap
*
* @param ResourceMapInterface $resourceMap
* @param OperationFactoryInterface $operationFactory
*/
public function __construct(
ResourceMapInterface $resourceMap,
OperationFactoryInterface $operationFactory
)
{
) {
$this->resourceMap = $resourceMap;
$this->operationFactory = $operationFactory;
}
......@@ -50,6 +59,7 @@ class OperationGetAction
* @param string $shortName
* @param string $type
* @param string $name
*
* @return Response
*/
public function __invoke(string $shortName, string $type, string $name): Response
......
<?php declare(strict_types=1);
/**
/*
* This file is part of "irstea/api-metadata".
*
* Copyright (C) 2019 IRSTEA
* All rights reserved.
*
* @copyright 2019 IRSTEA
* @author guillaume.perreal
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by the Free
* Software Foundation, either version 3 of the License, or (at your option) any
* later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License and the GNU
* Lesser General Public License along with this program. If not, see
* <https://www.gnu.org/licenses/>.
*/
namespace Irstea\ApiMetadata\Action;
use Irstea\ApiMetadata\Exception\ResourceNotFoundException;
use Irstea\ApiMetadata\Factory\Context;
use Irstea\ApiMetadata\Factory\Operation\OperationFactoryInterface;
......@@ -19,7 +28,7 @@ use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;
/**
* Class OperationListAction
* Class OperationListAction.
*/
class OperationListAction
{
......@@ -34,20 +43,21 @@ class OperationListAction
/**
* OperationListAction constructor.
* @param ResourceMapInterface $resourceMap
*
* @param ResourceMapInterface $resourceMap
* @param OperationFactoryInterface $operationFactory
*/
public function __construct(
ResourceMapInterface $resourceMap,
OperationFactoryInterface $operationFactory
)
{
) {
$this->resourceMap = $resourceMap;
$this->operationFactory = $operationFactory;
}
/**
* @param string $shortName
*
* @return Response
*/
public function __invoke(string $shortName): Response
......@@ -61,7 +71,7 @@ class OperationListAction
$ctx = new Context($className);
foreach ($this->operationFactory->enumerate($className, $ctx) as $key => $operation) {
$operations[$key]= $operation;
$operations[$key] = $operation;
}
return JsonResponse::create(array_merge(
......
<?php declare(strict_types=1);
/**
/*
* This file is part of "irstea/api-metadata".
*
* Copyright (C) 2019 IRSTEA
* All rights reserved.
*
* @copyright 2019 IRSTEA
* @author guillaume.perreal
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by the Free
* Software Foundation, either version 3 of the License, or (at your option) any
* later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License and the GNU
* Lesser General Public License along with this program. If not, see
* <https://www.gnu.org/licenses/>.
*/
namespace Irstea\ApiMetadata\Action;
use Irstea\ApiMetadata\Exception\ResourceNotFoundException;
use Irstea\ApiMetadata\Factory\Context;
use Irstea\ApiMetadata\Factory\Type\TypeFactoryInterface;
......@@ -20,7 +29,7 @@ use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;
/**
* Class ResourceGetAction
* Class ResourceGetAction.
*/
class ResourceGetAction
{
......@@ -35,20 +44,21 @@ class ResourceGetAction
/**
* ResourceListAction constructor.
*
* @param ResourceMapInterface $resourceMap
* @param TypeFactoryInterface $typeFactory
*/
public function __construct(
ResourceMapInterface $resourceMap,
TypeFactoryInterface $typeFactory
)
{
) {
$this->resourceMap = $resourceMap;
$this->typeFactory = $typeFactory;
}
/**
* @param string $shortName
*
* @return Response
*/
public function __invoke(string $shortName): Response
......@@ -58,9 +68,9 @@ class ResourceGetAction
throw new ResourceNotFoundException($shortName);
}
$ctx = new Context($className);
$ctx = new Context($this->typeFactory, $className);
$type = PropertyInfoType::create($className);
$resource = $this->typeFactory->create($type, $ctx);
$resource = $ctx->createType($type);
return JsonResponse::create(array_merge(
['resource' => $resource],
......
<?php declare(strict_types=1);
/**
/*
* This file is part of "irstea/api-metadata".
*
* Copyright (C) 2019 IRSTEA
* All rights reserved.
*
* @copyright 2019 IRSTEA
* @author guillaume.perreal
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by the Free
* Software Foundation, either version 3 of the License, or (at your option) any
* later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License and the GNU
* Lesser General Public License along with this program. If not, see
* <https://www.gnu.org/licenses/>.
*/
namespace Irstea\ApiMetadata\Action;
use Irstea\ApiMetadata\Factory\Context;
use Irstea\ApiMetadata\Factory\Type\TypeFactoryInterface;
use Irstea\ApiMetadata\Helper\PropertyInfoType;
......@@ -19,7 +28,7 @@ use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;
/**
* Class ResourceListAction
* Class ResourceListAction.
*/
class ResourceListAction
{
......@@ -34,14 +43,14 @@ class ResourceListAction
/**
* ResourceListAction constructor.
*
* @param ResourceMapInterface $resourceMap
* @param TypeFactoryInterface $typeFactory
*/
public function __construct(
ResourceMapInterface $resourceMap,
TypeFactoryInterface $typeFactory
)
{
) {
$this->resourceMap = $resourceMap;
$this->typeFactory = $typeFactory;
}
......@@ -53,9 +62,10 @@ class ResourceListAction
{
$resources = [];
foreach ($this->resourceMap as $className => $shortName) {
$ctx = new Context($className);
$ctx = new Context($this->typeFactory, $className);
$type = PropertyInfoType::create($className);
$resource = $this->typeFactory->create($type, $ctx);
$resource = $ctx->createType($type);
$resources[$shortName] = array_merge(
['resource' => $resource],
$ctx->jsonSerialize()
......
......@@ -29,16 +29,18 @@ use Irstea\ApiMetadata\Model\OperationMetadata;
interface OperationFactoryInterface
{
/**
* @param string $class
* @param string $type
* @param string $name
* @param string $class
* @param string $type
* @param string $name
* @param ContextInterface $context
*
* @return OperationMetadata
*/
public function create(string $class, string $type, string $name, ContextInterface $context): OperationMetadata;
/**
* @param string $class
* @param string $class
* @param ContextInterface $context
*
* @return OperationMetadata[]
*/
......
......@@ -95,7 +95,7 @@ class ResourceOperationFactory implements OperationFactoryInterface
throw new \InvalidArgumentException("unhandled operationt type: $type");
}
foreach ((array)$ops as $opName => $attributes) {
foreach ((array) $ops as $opName => $attributes) {
if ($opName === $name) {
return $this->extractMetadata($class, $resource, $type, $name, $attributes, $context);
}
......@@ -164,7 +164,7 @@ class ResourceOperationFactory implements OperationFactoryInterface
$name,
$path,
$method,
$method !== 'GET' && $method !== 'HEAD' ? $this->findInOuts($input, $context): null,
$method !== 'GET' && $method !== 'HEAD' ? $this->findInOuts($input, $context) : null,
$method !== 'HEAD' ? $this->findInOuts($output, $context) : null
);
}
......
......@@ -49,11 +49,11 @@ final class OperationMetadata implements \JsonSerializable
/**
* OperationMetadata constructor.
*
* @param string $resource
* @param string $type
* @param string $name
* @param string $path
* @param string $method
* @param string $resource
* @param string $type
* @param string $name
* @param string $path
* @param string $method
* @param TypeMetadata|null $input
* @param TypeMetadata|null $output
*/
......@@ -73,7 +73,7 @@ final class OperationMetadata implements \JsonSerializable
*/
public function getKey(): string
{
return $this->type .'_' . $this->name;
return $this->type . '_' . $this->name;
}
/**
......@@ -146,12 +146,10 @@ final class OperationMetadata implements \JsonSerializable
}
/**
* {@inheritDoc}
* {@inheritdoc}
*/
public function jsonSerialize()
{
return ['$type' => 'operation'] + \get_object_vars($this);
}
}
......@@ -139,7 +139,7 @@ final class PropertyMetadata implements \JsonSerializable
}
/**
* @inheritDoc
* {@inheritdoc}
*/
public function jsonSerialize()
{
......
......@@ -20,7 +20,6 @@
namespace Irstea\ApiMetadata\Model;
use ApiPlatform\Core\Annotation as API;
use Assert\Assertion;
/**
......
<?php declare(strict_types=1);
/**
/*
* This file is part of "irstea/api-metadata".
*
* Copyright (C) 2019 IRSTEA
* All rights reserved.
*
* @copyright 2019 IRSTEA
* @author guillaume.perreal
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by the Free
* Software Foundation, either version 3 of the License, or (at your option) any
* later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License and the GNU
* Lesser General Public License along with this program. If not, see
* <https://www.gnu.org/licenses/>.
*/
namespace Irstea\ApiMetadata\Service;
use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface;
use ApiPlatform\Core\Metadata\Resource\Factory\ResourceNameCollectionFactoryInterface;
/**
* Class ResourceMap
* Class ResourceMap.
*/
class ResourceMap implements ResourceMapInterface
{
......@@ -33,8 +42,9 @@ class ResourceMap implements ResourceMapInterface
/**
* ResourceMap constructor.
*
* @param ResourceNameCollectionFactoryInterface $nameFactory
* @param ResourceMetadataFactoryInterface $metadataFactory
* @param ResourceMetadataFactoryInterface $metadataFactory
*/
public function __construct(
ResourceNameCollectionFactoryInterface $nameFactory,
......@@ -45,47 +55,52 @@ class ResourceMap implements ResourceMapInterface
}
/**
* @inheritDoc
* {@inheritdoc}
*/
public function getIterator(): \Iterator
{
$this->init();
return new \ArrayIterator($this->resourceMap);
}
/**
* @inheritDoc
* {@inheritdoc}
*/
public function isResourceClass(string $className): bool
{
$this->init();
return isset($this->resourceMap[$className]);
}
/**
* @inheritDoc
* {@inheritdoc}
*/
public function getShortName(string $className): ?string
{
$this->init();
return $this->resourceMap[$className] ?? null;
}
/**
* @inheritDoc
* {@inheritdoc}
*/
public function isResourceName(string $shortName): bool
{
$this->init();
return isset($this->reverseMap[$shortName]);
}
/**
* @inheritDoc
* {@inheritdoc}
*/
public function getClassName(string $shortName): ?string
{
$this->init();
return $this->reverseMap[$shortName] ?? null;
}
......@@ -102,7 +117,7 @@ class ResourceMap implements ResourceMapInterface
$this->resourceMap = [];
$this->reverseMap = [];
foreach($this->nameFactory->create() as $className) {
foreach ($this->nameFactory->create() as $className) {
$metadata = $this->metadataFactory->create($className);
$shortName = $metadata->getShortName();
$this->resourceMap[$className] = $shortName;
......@@ -110,4 +125,3 @@ class ResourceMap implements ResourceMapInterface
}
}
}
<?php declare(strict_types=1);
/**
/*
* This file is part of "irstea/api-metadata".
*
* Copyright (C) 2019 IRSTEA
* All rights reserved.
*
* @copyright 2019 IRSTEA
* @author guillaume.perreal
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by the Free
* Software Foundation, either version 3 of the License, or (at your option) any
* later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License and the GNU
* Lesser General Public License along with this program. If not, see
* <https://www.gnu.org/licenses/>.
*/
namespace Irstea\ApiMetadata\Service;
/**
* Interface ResourceMapInterface
* Interface ResourceMapInterface.
*/
interface ResourceMapInterface extends \IteratorAggregate
{
/**
* @param string $className
*
* @return bool
*/
public function isResourceClass(string $className): bool;
/**
/**
* @param string $shortName
*
* @return bool
*/
public function isResourceName(string $shortName): bool;
/**
* @param string $shortName
*
* @return string|null
*/
public function getClassName(string $shortName): ?string;
/**
* @param string $className
*
* @return string|null
*/
public function getShortName(string $className): ?string;
......
<?php declare(strict_types=1);
/**
/*
* This file is part of "irstea/api-metadata".
*
* Copyright (C) 2019 IRSTEA
* All rights reserved.
*
* @copyright 2019 IRSTEA
* @author guillaume.perreal
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by the Free
* Software Foundation, either version 3 of the License, or (at your option) any
* later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License and the GNU
* Lesser General Public License along with this program. If not, see
* <https://www.gnu.org/licenses/>.
*/
namespace Irstea\ApiMetadata\Tests\Service;
......@@ -20,7 +31,7 @@ use Prophecy\Prophecy\MethodProphecy;
use Prophecy\Prophecy\ObjectProphecy;
/**
* Class ResourceMapTest
* Class ResourceMapTest.
*
* @covers \Irstea\ApiMetadata\Service\ResourceMap
*/
......@@ -56,7 +67,7 @@ class ResourceMapTest extends TestCase
$proph = $metadataFactoryProph->create($stringArg);
$that = $this;
$proph->will(function(array $args) use ($that) {
$proph->will(function (array $args) use ($that) {
return $that->resources[$args[0]] ?? null;
});
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment