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

EntityService   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 94.12%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 3
dl 0
loc 49
ccs 16
cts 17
cp 0.9412
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 7 1
A getServiceByEntity() 0 10 3
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