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

AmbassadorService::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 2
crap 1
1
<?php
2
3
namespace AppBundle\Service\Ambassador;
4
5
use AppBundle\Entity\Band;
6
use AppBundle\Entity\BandMember;
7
use AppBundle\Entity\Infrasctucture\Ambassador;
8
use AppBundle\Entity\Infrasctucture\AmbassadorRepository;
9
use AppBundle\Entity\User;
10
use AppBundle\Exception\FormTypeNotSupported;
11
use AppBundle\Form\AbstractFormType;
12
use AppBundle\Form\Ambassador\AmbassadorFormType;
13
use AppBundle\Service\Entity\BandService;
14
use AppBundle\Service\Entity\Infrastructure\EntityCreatorInterface;
15
use Doctrine\ORM\EntityManager;
16
17
/**
18
 * @author Vehsamrak
19
 */
20
class AmbassadorService implements EntityCreatorInterface
21
{
22
23
    /** @var EntityManager */
24
    private $entityManager;
25
26
    /** @var BandService */
27
    private $bandService;
28
29 10
    public function __construct(EntityManager $entityManager, BandService $bandService)
0 ignored issues
show
Bug introduced by
You have injected the EntityManager via parameter $entityManager. This is generally not recommended as it might get closed and become unusable. Instead, it is recommended to inject the ManagerRegistry and retrieve the EntityManager via getManager() each time you need it.

The EntityManager might become unusable for example if a transaction is rolled back and it gets closed. Let’s assume that somewhere in your application, or in a third-party library, there is code such as the following:

function someFunction(ManagerRegistry $registry) {
    $em = $registry->getManager();
    $em->getConnection()->beginTransaction();
    try {
        // Do something.
        $em->getConnection()->commit();
    } catch (\Exception $ex) {
        $em->getConnection()->rollback();
        $em->close();

        throw $ex;
    }
}

If that code throws an exception and the EntityManager is closed. Any other code which depends on the same instance of the EntityManager during this request will fail.

On the other hand, if you instead inject the ManagerRegistry, the getManager() method guarantees that you will always get a usable manager instance.

Loading history...
30
    {
31 10
        $this->entityManager = $entityManager;
32 10
        $this->bandService = $bandService;
33 10
    }
34
35 2
    public function createEntityByFormData(AbstractFormType $formType, User $creator, string $entityClass)
36
    {
37 2
        if (!$formType instanceof AmbassadorFormType) {
38
            throw new FormTypeNotSupported(get_class($formType));
39
        }
40
41 2
        $ambassadorName = $formType->name;
42 2
        $ambassadorDescription = $formType->description;
43 2
        $ambassadorMemberLogins = (array) $formType->members;
44
45
        /** @var Ambassador $ambassador */
46 2
        $ambassador = new $entityClass($ambassadorName, $creator, $ambassadorDescription);
47
        /** @var AmbassadorRepository $repository */
48 2
        $repository = $this->entityManager->getRepository($entityClass);
49 2
        $repository->persist($ambassador);
50
51
52 2
        if ($ambassador instanceof Band) {
53 1
            $ambassador->addMember($this->bandService->createBandMemberFromCreator($ambassador, $creator));
54
55 1
            foreach ($ambassadorMemberLogins as $memberData) {
56 1
                $memberLogin = $memberData['login'];
57 1
                $memberShortDescription = $memberData['short_description'];
58 1
                $memberDescription = $memberData['description'] ?? '';
59
60
                /** @var BandMember $member */
61 1
                $member = $this->getMemberByAmbassadorAndLogin(
62
                    $ambassador,
63
                    $memberLogin,
64
                    $memberShortDescription,
65
                    $memberDescription
66
                );
67
68 1
                $ambassador->addMember($member);
69
            }
70
        }
71
72 2
        $repository->flush();
73
74 2
        return $ambassador;
75
    }
76
77 1
    private function getMemberByAmbassadorAndLogin(
78
        $ambassador,
79
        $memberLogin,
80
        $memberShortDescription = '',
81
        $memberDescription = ''
82
    ) {
83 1
        $user = $this->entityManager->getRepository(User::class)->findOneByLogin($memberLogin);
84
85 1
        return $this->entityManager->getRepository(BandMember::class)->getOrCreateByBandAndUser(
86
            $ambassador,
87
            $user,
88
            $memberShortDescription,
89
            $memberDescription
90
        );
91
92
    }
93
}
94