|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace AppBundle\Service\Entity; |
|
4
|
|
|
|
|
5
|
|
|
use AppBundle\Entity\Infrasctucture\Ambassador; |
|
6
|
|
|
use AppBundle\Entity\User; |
|
7
|
|
|
use AppBundle\Exception\EntityNotFoundException; |
|
8
|
|
|
use AppBundle\Exception\MethodNotImplemented; |
|
9
|
|
|
use AppBundle\Form\AbstractFormType; |
|
10
|
|
|
use AppBundle\Response\ApiError; |
|
11
|
|
|
use AppBundle\Service\Ambassador\AmbassadorService; |
|
12
|
|
|
use AppBundle\Service\Entity\Infrastructure\EntityCreatorInterface; |
|
13
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @author Vehsamrak |
|
17
|
|
|
*/ |
|
18
|
|
|
class EntityService |
|
19
|
|
|
{ |
|
20
|
|
|
|
|
21
|
|
|
/** @var AmbassadorService */ |
|
22
|
|
|
private $ambassadorService; |
|
23
|
|
|
|
|
24
|
10 |
|
public function __construct(AmbassadorService $ambassadorService) |
|
25
|
|
|
{ |
|
26
|
10 |
|
$this->ambassadorService = $ambassadorService; |
|
27
|
10 |
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @param string $entityFullName Entity class name |
|
31
|
|
|
* @param string|int $id Entity id |
|
32
|
|
|
* @return ApiError |
|
33
|
|
|
* @throws EntityNotFoundException |
|
34
|
|
|
*/ |
|
35
|
5 |
|
public function createEntityNotFoundResponse(string $entityFullName, $id): ApiError |
|
36
|
|
|
{ |
|
37
|
5 |
|
$entityName = (new \ReflectionClass($entityFullName))->getShortName(); |
|
38
|
|
|
|
|
39
|
5 |
|
return new ApiError( |
|
40
|
5 |
|
sprintf('%s "%s" was not found.', $entityName, $id), |
|
41
|
5 |
|
Response::HTTP_NOT_FOUND |
|
42
|
|
|
); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @return object Entity |
|
47
|
|
|
*/ |
|
48
|
2 |
|
public function createEntityByFormData(AbstractFormType $formType, User $creator, string $entityClass) |
|
49
|
|
|
{ |
|
50
|
2 |
|
$service = $this->getServiceByEntity($entityClass); |
|
51
|
|
|
|
|
52
|
2 |
|
if (!$service instanceof EntityCreatorInterface) { |
|
53
|
|
|
throw new MethodNotImplemented(__METHOD__); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
2 |
|
$entity = $service->createEntityByFormData($formType, $creator, $entityClass); |
|
57
|
|
|
|
|
58
|
2 |
|
return $entity; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
2 |
|
private function getServiceByEntity(string $entityClass) |
|
62
|
|
|
{ |
|
63
|
2 |
|
$entityClass = new \ReflectionClass($entityClass); |
|
64
|
|
|
|
|
65
|
2 |
|
if ($entityClass->isSubclassOf(Ambassador::class)) { |
|
66
|
2 |
|
return $this->ambassadorService; |
|
67
|
|
|
} else { |
|
68
|
|
|
throw new MethodNotImplemented(__METHOD__); |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
|