-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
To support basics of entity management, http processing.
- Loading branch information
1 parent
4623951
commit ae9ffdb
Showing
12 changed files
with
311 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
namespace Pluf\Core\Exception; | ||
|
||
use Pluf\Orm\Attribute\Entity; | ||
use Pluf\Orm\Attribute\Column; | ||
use Pluf\Orm\Attribute\Transients; | ||
use Throwable; | ||
|
||
#[Entity] | ||
#[Transients(["line", "file", "string", "trace", "previous"])] | ||
class InvalidArgumentException extends \Pluf\Core\Exception | ||
{ | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
namespace Pluf\Core\Exception; | ||
|
||
use Pluf\Orm\Attribute\Entity; | ||
use Pluf\Orm\Attribute\Column; | ||
use Pluf\Orm\Attribute\Transients; | ||
use Throwable; | ||
|
||
#[Entity] | ||
#[Transients(["line", "file", "string", "trace", "previous"])] | ||
class NotSupportedMethodException extends \Pluf\Core\Exception | ||
{ | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?php | ||
namespace Pluf\Core\Process\Entity; | ||
|
||
use Pluf\Orm\EntityManager; | ||
|
||
/** | ||
* Creates a list of customer and return them as result | ||
* | ||
* | ||
* @author maso | ||
* | ||
*/ | ||
class CreateEntities | ||
{ | ||
|
||
/** | ||
* Store list of cusomers into the repositoer | ||
* | ||
* | ||
* @param EntityManager $entityManager | ||
* @param array $customers | ||
* list of customer | ||
* @return mixed[] list of entities to persist | ||
*/ | ||
public function __invoke(EntityManager $entityManager, $entities): array | ||
{ | ||
if (! is_array($entities)) { | ||
$entities = [ | ||
$entities | ||
]; | ||
} | ||
$resultList = []; | ||
foreach ($entities as $customer) { | ||
$item = $entityManager->persist($customer); | ||
$resultList[] = $item; | ||
} | ||
return $resultList; | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
namespace Pluf\Core\Process\Entity; | ||
|
||
use Pluf\Scion\UnitTrackerInterface; | ||
|
||
class EntityManagerFactory | ||
{ | ||
|
||
public function __invoke(\Pluf\Orm\EntityManagerFactory $entityManagerFactory, UnitTrackerInterface $unitTracker) | ||
{ | ||
$entityManager = $entityManagerFactory->createEntityManager(); | ||
try { | ||
return $unitTracker->next([ | ||
"entityManager" => $entityManager | ||
]); | ||
} finally { | ||
$entityManager->close(); | ||
} | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
namespace Pluf\Core\Process\Entity; | ||
|
||
use Pluf\Scion\UnitTrackerInterface; | ||
use Throwable; | ||
use Pluf\Orm\EntityManager; | ||
|
||
class EntityManagerTransaction | ||
{ | ||
|
||
public function __invoke(EntityManager $entityManager, UnitTrackerInterface $unitTracker) | ||
{ | ||
$transaction = $entityManager->getTransaction(); | ||
$transaction->begin(); | ||
try { | ||
$result = $unitTracker->next([ | ||
"entityManager" => $entityManager | ||
]); | ||
$transaction->commit(); | ||
return $result; | ||
} catch (Throwable $th) { | ||
$transaction->rollback(); | ||
throw $th; | ||
} | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php | ||
namespace Pluf\Core\Process\Entity; | ||
|
||
use Pluf\Orm\AssertionTrait; | ||
use Pluf\Orm\EntityQuery; | ||
|
||
class ReadEntities | ||
{ | ||
use AssertionTrait; | ||
|
||
private string $class; | ||
private string $name = "entities"; | ||
|
||
public function __construct(string $class, string $name = "entities") | ||
{ | ||
$this->class = $class; | ||
$this->name = $name; | ||
} | ||
|
||
/** | ||
* Store list of cusomers into the repositoer | ||
* | ||
* | ||
* @param EntityQuery $entityQuery to perform on entities | ||
* @return array of results | ||
*/ | ||
public function __invoke(EntityQuery $entityQuery): array | ||
{ | ||
return $entityQuery->entity($this->class) | ||
->mode('select') | ||
->exec(); | ||
} | ||
|
||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
namespace Pluf\Core\Process\Http; | ||
|
||
use Pluf\Orm\EntityManager; | ||
use Pluf\Scion\UnitTrackerInterface; | ||
use Psr\Http\Message\ServerRequestInterface; | ||
|
||
/** | ||
* Creates an entity query based on http request | ||
* | ||
* NOTE: the class must set. | ||
* | ||
* @author maso | ||
* | ||
*/ | ||
class RequestToEntityQuery | ||
{ | ||
public function __invoke(ServerRequestInterface $request, EntityManager $entityManager, UnitTrackerInterface $unitTracker) | ||
{ | ||
$entityQuery = $entityManager->createQuery(); | ||
//XXX: maso, 2021: read params from the request header. | ||
return $unitTracker->next([ | ||
'entityQuery' => $entityQuery | ||
]); | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
<?php | ||
namespace Pluf\Core\Process; | ||
|
||
use Pluf\Orm\ModelDescriptionRepository; | ||
use Pluf\Orm\ObjectMapperBuilder; | ||
use Pluf\Scion\UnitTrackerInterface; | ||
use Psr\Http\Message\ServerRequestInterface; | ||
use Pluf\Orm\AssertionTrait; | ||
|
||
class HttpBodyToEntities | ||
{ | ||
use AssertionTrait; | ||
|
||
private string $class; | ||
private bool $multi; | ||
private string $name = "entities"; | ||
|
||
public function __construct(string $class, bool $multi = true, string $name = "entities") | ||
{ | ||
$this->class = $class; | ||
$this->multi = $multi; | ||
$this->name = $name; | ||
} | ||
|
||
public function __invoke( | ||
ModelDescriptionRepository $modelDescriptionRepository, | ||
ServerRequestInterface $request, | ||
UnitTrackerInterface $unitTracker) | ||
{ | ||
$this->assertEquals("POST", $request->getMethod(), "Unsupported method {{method}}", ["method" => $request->getMethod()]); | ||
|
||
$type = $this->getContentType($request); | ||
$this->assertNotEmpty($type, "Content type is not specified."); | ||
switch ($type) { | ||
case "json": | ||
$data = $request->getBody(); | ||
break; | ||
case "array": | ||
default: | ||
$data = $request->getParsedBody(); | ||
break; | ||
} | ||
|
||
$builder = new ObjectMapperBuilder(); | ||
$objectMapper = $builder->addType($type) | ||
->setModelDescriptionRepository($modelDescriptionRepository) | ||
->supportList($this->multi) | ||
->build(); | ||
|
||
$res = []; | ||
$res[$this->name] = $objectMapper->readValue($data, $this->class, $this->multi); | ||
return $unitTracker->next($res); | ||
} | ||
|
||
public function getContentType(ServerRequestInterface $request): string | ||
{ | ||
$contentTypes = $request->getHeader("Content-Type") ?? []; | ||
|
||
$parsedContentType = 'application/json'; | ||
foreach ($contentTypes as $contentType) { | ||
$fragments = explode(';', $contentType); | ||
$parsedContentType = current($fragments); | ||
} | ||
$contentTypesWithParsedBodies = [ | ||
'application/json', | ||
'application/xml', | ||
'application/yml' | ||
]; | ||
|
||
$type = null; | ||
if (in_array($parsedContentType, $contentTypesWithParsedBodies)) { | ||
switch ($parsedContentType) { | ||
case 'application/json': | ||
$type = "json"; | ||
break; | ||
case 'application/xml': | ||
case 'application/yml': | ||
default: | ||
$type = null; | ||
} | ||
} else { | ||
$type = "array"; | ||
} | ||
|
||
return $type; | ||
} | ||
} | ||
|