FosUserHydrator::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace Swm\Bundle\MailHookBundle\Hydrator;
4
5
use FOS\UserBundle\Model\UserManagerInterface;
6
use Swm\Bundle\MailHookBundle\Event\HookEventInterface;
7
use Swm\Bundle\MailHookBundle\Hook\HookInterface;
8
use Swm\Bundle\MailHookBundle\Hydrator\HydratorInterface;
9
10
class FosUserHydrator implements HydratorInterface
11
{
12
    /**
13
     * @var UserManagerInterface
14
     */
15
    private $userManager;
16
17
    /**
18
     * @param UserManagerInterface $userManager
19
     */
20
    public function __construct(UserManagerInterface $userManager)
21
    {
22
        $this->userManager = $userManager;
23
    }
24
25
    /**
26
     * @param  HookInterface $apiService
27
     * @param  string        $entityName
28
     * @return mixed $entityName instance
29
     */
30
    public function hydrate(HookInterface $apiService, $entityName)
31
    {
32
        $hydratedEntity = new $entityName();
33
34
        if (!$hydratedEntity instanceof HookEventInterface) {
35
            throw new LogicException("Can't hydrate this event");
36
        }
37
38
        $hydratedEntity->setEmail($apiService->getEmail());
39
        $hydratedEntity->setName($apiService->getService());
40
        $hydratedEntity->setMetaData($apiService->getMetaData());
41
42
        $user = $this->userManager->findUserByEmail($apiService->getEmail());
43
        $hydratedEntity->setUser($user);
44
45
        return $hydratedEntity;
46
    }
47
}
48