- WIP on destroy() method for an API DELET call
This commit is contained in:
		
							parent
							
								
									6d7701cfe5
								
							
						
					
					
						commit
						2d6f2c665b
					
				@ -6,7 +6,6 @@ use Psr\Http\Message\RequestInterface;
 | 
				
			|||||||
use Psr\Http\Message\ResponseInterface;
 | 
					use Psr\Http\Message\ResponseInterface;
 | 
				
			||||||
use Psr\Http\Message\ServerRequestInterface;
 | 
					use Psr\Http\Message\ServerRequestInterface;
 | 
				
			||||||
use Psr\Http\Message\UriInterface;
 | 
					use Psr\Http\Message\UriInterface;
 | 
				
			||||||
use Ulmus\Api\Attribute\Obj\Api\ApiAction;
 | 
					 | 
				
			||||||
use Ulmus\Api\Common\MethodEnum;
 | 
					use Ulmus\Api\Common\MethodEnum;
 | 
				
			||||||
use Ulmus\Api\RequestBuilder\Filter;
 | 
					use Ulmus\Api\RequestBuilder\Filter;
 | 
				
			||||||
use Ulmus\Api\RequestBuilder\UrlParameter;
 | 
					use Ulmus\Api\RequestBuilder\UrlParameter;
 | 
				
			||||||
@ -16,6 +15,7 @@ use Ulmus\Api\Stream\Stream;
 | 
				
			|||||||
use Ulmus\Api\Request\JsonRequest;
 | 
					use Ulmus\Api\Request\JsonRequest;
 | 
				
			||||||
use Ulmus\Api\Transport\CurlClient;
 | 
					use Ulmus\Api\Transport\CurlClient;
 | 
				
			||||||
use Ulmus\Api\Transport\CurlTransport;
 | 
					use Ulmus\Api\Transport\CurlTransport;
 | 
				
			||||||
 | 
					use Ulmus\Entity\EntityInterface;
 | 
				
			||||||
use Ulmus\EntityCollection;
 | 
					use Ulmus\EntityCollection;
 | 
				
			||||||
use Ulmus\SearchRequest\SearchRequestInterface;
 | 
					use Ulmus\SearchRequest\SearchRequestInterface;
 | 
				
			||||||
use Ulmus\Ulmus;
 | 
					use Ulmus\Ulmus;
 | 
				
			||||||
@ -66,7 +66,24 @@ class ApiRepository extends \Ulmus\Repository
 | 
				
			|||||||
    {
 | 
					    {
 | 
				
			||||||
        $response = $this->executeRequest(Attribute\Obj\Api\Create::class, $entity->entityGetDataset());
 | 
					        $response = $this->executeRequest(Attribute\Obj\Api\Create::class, $entity->entityGetDataset());
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        if ($replace && $entity instanceof EntityInterface) {
 | 
				
			||||||
            $entity->fromArray($response->getParsedBody());
 | 
					            $entity->fromArray($response->getParsedBody());
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        return $response->getStatusCode() === 200;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public function destroy(object $entity) : bool
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        if ( ! $this->matchEntity($entity) ) {
 | 
				
			||||||
 | 
					            throw new \Exception("Your entity class `" . get_class($entity) . "` cannot match entity type of repository `{$this->entityClass}`");
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        $this->wherePrimaryKeyValue($entity);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        $this->eventExecute(\Ulmus\Event\Query\Delete::class, $this, $entity);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        $response = $this->executeRequest(Attribute\Obj\Api\Delete::class);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        return $response->getStatusCode() === 200;
 | 
					        return $response->getStatusCode() === 200;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
@ -195,12 +212,12 @@ class ApiRepository extends \Ulmus\Repository
 | 
				
			|||||||
        return $this->entityClass::resolveEntity()->getAttributeImplementing($type);
 | 
					        return $this->entityClass::resolveEntity()->getAttributeImplementing($type);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    protected function callApiRequestCallback(RequestInterface $request, ApiAction $attribute) : ServerRequestInterface
 | 
					    protected function callApiRequestCallback(RequestInterface $request, Attribute\Obj\Api\ApiAction $attribute) : ServerRequestInterface
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        return $this->adapter->apiHandler->handleRequest($request, $attribute);
 | 
					        return $this->adapter->apiHandler->handleRequest($request, $attribute);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    protected function callApiResponseCallback(ResponseInterface $response, ApiAction $attribute) : ResponseInterface
 | 
					    protected function callApiResponseCallback(ResponseInterface $response, Attribute\Obj\Api\ApiAction $attribute) : ResponseInterface
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        return $this->adapter->apiHandler->handleResponse($response, $attribute);
 | 
					        return $this->adapter->apiHandler->handleResponse($response, $attribute);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
@ -211,26 +228,6 @@ class ApiRepository extends \Ulmus\Repository
 | 
				
			|||||||
            $this->adapter->apiHandler->debugResponse($transport, $this);
 | 
					            $this->adapter->apiHandler->debugResponse($transport, $this);
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
/*
 | 
					 | 
				
			||||||
    public function collectionFromQuery(? string $entityClass = null) : EntityCollection
 | 
					 | 
				
			||||||
    {
 | 
					 | 
				
			||||||
        $entityClass ??= $this->entityClass;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        $entityCollection = $entityClass::entityCollection();
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        $this->finalizeQuery();
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        foreach(Ulmus::iterateQueryBuilder($this->queryBuilder, $this->adapter) as $entityData) {
 | 
					 | 
				
			||||||
            $entity = $this->instanciateEntity($entityClass);
 | 
					 | 
				
			||||||
            $entity->loadedFromAdapter = $this->adapter->name;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
            $entityCollection->append( $entity->resetVirtualProperties()->entityFillFromDataset($entityData) );
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        $this->eventExecute(\Ulmus\Event\Repository\CollectionFromQueryInterface::class, $this, $entityCollection);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        return $entityCollection;
 | 
					 | 
				
			||||||
    }*/
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
    public function filterServerRequest(SearchRequestInterface $searchRequest, bool $count = true) : \Ulmus\Repository
 | 
					    public function filterServerRequest(SearchRequestInterface $searchRequest, bool $count = true) : \Ulmus\Repository
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
@ -261,4 +258,36 @@ class ApiRepository extends \Ulmus\Repository
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
        return $this;
 | 
					        return $this;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public function wherePrimaryKey(mixed $value, null|string|bool $alias = self::DEFAULT_ALIAS) : self
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        $primaryKeyField = Ulmus::resolveEntity($this->entityClass)->getPrimaryKeyField();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        if ( $primaryKeyField === null ) {
 | 
				
			||||||
 | 
					            throw new \Ulmus\Exception\EntityPrimaryKeyUnknown(sprintf("No primary key found for entity %s", $this->entityClass));
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        $pkField = key($primaryKeyField);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        return $this->where($pkField, $value);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public function wherePrimaryKeyValue(EntityInterface $entity) : self
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        $primaryKeyField = Ulmus::resolveEntity($this->entityClass)->getPrimaryKeyField();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        if ( $primaryKeyField === null ) {
 | 
				
			||||||
 | 
					            throw new \Ulmus\Exception\EntityPrimaryKeyUnknown(sprintf("No primary key found for entity %s", $this->entityClass));
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        $pkField = key($primaryKeyField);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        $value = $entity->$pkField;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        if ( $value !== 0 && empty($value) ) {
 | 
				
			||||||
 | 
					            throw new \Ulmus\Exception\EntityPrimaryKeyUnknown(sprintf("A primary key value has to be defined to delete an item of entity %s.", $entity::class));
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        return $this->where($pkField, $value);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
@ -11,5 +11,6 @@ class Create extends ApiAction
 | 
				
			|||||||
        public string $url,
 | 
					        public string $url,
 | 
				
			||||||
        public MethodEnum $method = MethodEnum::Post,
 | 
					        public MethodEnum $method = MethodEnum::Post,
 | 
				
			||||||
        public int $timeout = 30,
 | 
					        public int $timeout = 30,
 | 
				
			||||||
 | 
					        public MethodEnum $searchMethod = MethodEnum::Get,
 | 
				
			||||||
    ) {}
 | 
					    ) {}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
@ -11,5 +11,6 @@ class Delete extends ApiAction
 | 
				
			|||||||
        public string $url,
 | 
					        public string $url,
 | 
				
			||||||
        public MethodEnum $method = MethodEnum::Delete,
 | 
					        public MethodEnum $method = MethodEnum::Delete,
 | 
				
			||||||
        public int $timeout = 30,
 | 
					        public int $timeout = 30,
 | 
				
			||||||
 | 
					        public MethodEnum $searchMethod = MethodEnum::Get,
 | 
				
			||||||
    ) {}
 | 
					    ) {}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
@ -11,5 +11,6 @@ class Update extends ApiAction
 | 
				
			|||||||
        public string $url,
 | 
					        public string $url,
 | 
				
			||||||
        public MethodEnum $method = MethodEnum::Patch,
 | 
					        public MethodEnum $method = MethodEnum::Patch,
 | 
				
			||||||
        public int $timeout = 30,
 | 
					        public int $timeout = 30,
 | 
				
			||||||
 | 
					        public MethodEnum $searchMethod = MethodEnum::Get,
 | 
				
			||||||
    ) {}
 | 
					    ) {}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
@ -68,18 +68,12 @@ class ConnectionAdapter extends \Ulmus\ConnectionAdapter
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    protected function instanciateAdapter($name): AdapterInterface
 | 
					    protected function instanciateAdapter($name): AdapterInterface
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        $class = substr($name, 0, 2) === "\\" ? $name : "\\Ulmus\\Api\\Adapter\\$name";
 | 
					        if (class_exists($name)) {
 | 
				
			||||||
 | 
					            return new $name();
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        $class = sprintf("\\%s\\Adapter\\%s", __NAMESPACE__, $name);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        return new $class();
 | 
					        return new $class();
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					 | 
				
			||||||
    #public function apiResponseCallback(callable $callback = null) : callable|null
 | 
					 | 
				
			||||||
    #{
 | 
					 | 
				
			||||||
    #    return $callback ? $this->apiResponseCallback = $callback : $this->apiResponseCallback ?? fn(ResponseInterface $response) => $response;
 | 
					 | 
				
			||||||
    #}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    #public function apiRequestCallback(callable $callback = null) : callable|null
 | 
					 | 
				
			||||||
    #{
 | 
					 | 
				
			||||||
    #    return $callback ? $this->apiRequestCallback = $callback : $this->apiRequestCallback ?? fn(RequestInterface $request) => $request;
 | 
					 | 
				
			||||||
    #}
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user