Passed
Push — master ( 6e4d97...d913e8 )
by Petr
03:45
created

EntityService::createEntityByFormData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

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