Passed
Push — master ( 25c2e9...26c6a8 )
by Petr
08:32
created

EntityService   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 89.47%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 3
dl 0
loc 54
ccs 17
cts 19
cp 0.8947
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A createEntityNotFoundResponse() 0 9 1
A createEntityByFormData() 0 12 2
A getServiceByEntity() 0 10 2
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