Completed
Push — develop ( 77dc65...6da9bc )
by Mario
03:53
created

Users   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 6
dl 0
loc 62
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
B load() 0 42 5
1
<?php
2
3
namespace AppBundle\Tenant;
4
5
use AppBundle\Entity\User;
6
use AppBundle\EventListener\User\IdentityListener;
7
use AppBundle\Service\UserService;
8
use Ds\Component\Tenant\Loader\Loader;
9
use Symfony\Component\Yaml\Yaml;
10
11
/**
12
 * Class Users
13
 */
14
class Users implements Loader
15
{
16
    /**
17
     * @var \AppBundle\Service\UserService
18
     */
19
    protected $userService;
20
21
    /**
22
     * Constructor
23
     *
24
     * @param \AppBundle\Service\UserService $userService
25
     */
26
    public function __construct(UserService $userService)
27
    {
28
        $this->userService = $userService;
29
    }
30
31
    /**
32
     * {@inheritdoc}
33
     */
34
    public function load(array $data)
35
    {
36
        $metadata = $this->userService->getManager()->getClassMetadata(User::class);
37
38
        foreach ($metadata->entityListeners as $event => $listeners) {
39
            foreach ($listeners as $key => $listener) {
40
                if (IdentityListener::class === $listener['class']) {
41
                    unset($metadata->entityListeners[$event][$key]);
42
                }
43
            }
44
        }
45
46
        $yml = file_get_contents('/srv/api-platform/src/AppBundle/Resources/tenant/users.yml');
47
48
        // @todo Figure out how symfony does parameter binding and use the same technique
49
        $yml = strtr($yml, [
50
            '%user.system.uuid%' => $data['user']['system']['uuid'],
51
            '%user.system.password%' => $data['user']['system']['password'],
52
            '%user.admin.uuid%' => $data['user']['admin']['uuid'],
53
            '%user.admin.password%' => $data['user']['admin']['password'],
54
            '%business_unit.administration.uuid%' => $data['business_unit']['administration']['uuid'],
55
            '%tenant.uuid%' => $data['tenant']['uuid']
56
        ]);
57
58
        $users = Yaml::parse($yml, YAML::PARSE_OBJECT_FOR_MAP);
59
        $manager = $this->userService->getCustomManager();
60
61
        foreach ($users->objects as $object) {
62
            $object = (object) array_merge((array) $users->prototype, (array) $object);
63
            $user = $manager->createUser();
64
            $user
65
                ->setUsername($object->username)
66
                ->setEmail($object->email)
67
                ->setPlainPassword($object->password)
68
                ->setRoles($object->roles)
69
                ->setOwner($object->owner)
0 ignored issues
show
Bug introduced by
The method setOwner() does not exist on FOS\UserBundle\Model\UserInterface. It seems like you code against a sub-type of FOS\UserBundle\Model\UserInterface such as AppBundle\Entity\User. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

69
                ->/** @scrutinizer ignore-call */ setOwner($object->owner)
Loading history...
70
                ->setOwnerUuid($object->owner_uuid)
71
                ->setIdentity($object->identity)
72
                ->setIdentityUuid($object->identity_uuid)
73
                ->setEnabled($object->enabled)
74
                ->setTenant($object->tenant);
75
            $manager->updateUser($user);
76
        }
77
    }
78
}
79