Test Setup Failed
Push — master ( ef9069...17042f )
by Craig
05:56
created

DeleteHelper::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
nc 1
nop 4
dl 0
loc 10
rs 10
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Zikula package.
7
 *
8
 * Copyright Zikula - https://ziku.la/
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Zikula\UsersModule\Helper;
15
16
use Doctrine\Common\Collections\ArrayCollection;
17
use Doctrine\Common\Collections\Collection;
18
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
19
use Zikula\Bundle\HookBundle\Dispatcher\HookDispatcherInterface;
20
use Zikula\Bundle\HookBundle\Hook\ProcessHook;
21
use Zikula\GroupsModule\Constant;
22
use Zikula\GroupsModule\Entity\RepositoryInterface\GroupRepositoryInterface;
23
use Zikula\UsersModule\Constant as UsersConstant;
24
use Zikula\UsersModule\Entity\RepositoryInterface\UserRepositoryInterface;
25
use Zikula\UsersModule\Entity\UserEntity;
26
use Zikula\UsersModule\Event\ActiveUserPostDeletedEvent;
27
use Zikula\UsersModule\Event\RegistrationPostDeletedEvent;
28
use Zikula\UsersModule\HookSubscriber\UserManagementUiHooksSubscriber;
29
30
class DeleteHelper
31
{
32
    /**
33
     * @var EventDispatcherInterface
34
     */
35
    private $eventDispatcher;
36
37
    /**
38
     * @var HookDispatcherInterface
39
     */
40
    private $hookDispatcher;
41
42
    /**
43
     * @var UserRepositoryInterface
44
     */
45
    private $userRepository;
46
47
    /**
48
     * @var GroupRepositoryInterface
49
     */
50
    private $groupRespository;
51
52
    public function __construct(
53
        EventDispatcherInterface $eventDispatcher,
54
        HookDispatcherInterface $hookDispatcher,
55
        UserRepositoryInterface $userRepository,
56
        GroupRepositoryInterface $groupRespository
57
    ) {
58
        $this->eventDispatcher = $eventDispatcher;
59
        $this->hookDispatcher = $hookDispatcher;
60
        $this->userRepository = $userRepository;
61
        $this->groupRespository = $groupRespository;
62
    }
63
64
    /**
65
     * @param string $param gid|status|uid
66
     * @param string $value
67
     * @param string|null $date
68
     *
69
     * @return \Doctrine\Common\Collections\Collection
70
     */
71
    public function getUserCollection(string $param, string $value, string $date = null): Collection
72
    {
73
        switch ($param) {
74
            case 'gid':
75
                if (in_array((int) $value, [Constant::GROUP_ID_USERS, Constant::GROUP_ID_ADMIN])) {
76
                    throw new \InvalidArgumentException('Cannot delete from main User or Administrator group.');
77
                }
78
                $users = $this->groupRespository->find((int) $value)->getUsers();
79
                break;
80
            case 'status':
81
                $statuses = [
82
                    'I' => UsersConstant::ACTIVATED_INACTIVE,
83
                    'P' => UsersConstant::ACTIVATED_PENDING_REG,
84
                    'M' => UsersConstant::ACTIVATED_PENDING_DELETE
85
                ];
86
                if (!array_key_exists($value, $statuses)) {
87
                    throw new \InvalidArgumentException('Invalid status key');
88
                }
89
                $users = $this->userRepository->findBy(['activated' => $statuses[$value]]);
90
                $users = new ArrayCollection($users);
91
                break;
92
            case 'uid':
93
                $user = $this->userRepository->find((int) $value);
94
                $users = new ArrayCollection([$user]);
95
                break;
96
            default:
97
                throw new \InvalidArgumentException('Invalid option name');
98
        }
99
        if (isset($date)) {
100
            $date = \DateTime::createFromFormat('YmdHis', $date, new \DateTimeZone('UTC'));
101
            $users = $users->filter(function (UserEntity $user) use ($date) {
102
                return $user->getRegistrationDate() < $date;
103
            });
104
        }
105
        $adminUser = $this->userRepository->find(UsersConstant::USER_ID_ADMIN);
106
        if ($users->contains($adminUser)) {
107
            $users->removeElement($adminUser);
108
        }
109
110
        return $users;
111
    }
112
113
    public function deleteUser(UserEntity $user, bool $fullDeletion = false): void
114
    {
115
        if (UsersConstant::ACTIVATED_ACTIVE === $user->getActivated()) {
116
            $this->eventDispatcher->dispatch(new ActiveUserPostDeletedEvent($user, $fullDeletion));
117
        } else {
118
            $this->eventDispatcher->dispatch(new RegistrationPostDeletedEvent($user));
119
        }
120
        $this->hookDispatcher->dispatch(UserManagementUiHooksSubscriber::DELETE_PROCESS, new ProcessHook($user->getUid()));
121
        if ($fullDeletion) {
122
            $this->userRepository->removeAndFlush($user);
123
        } else {
124
            $this->convertUserToGhost($user);
125
        }
126
    }
127
128
    private function convertUserToGhost(UserEntity $userEntity): void
129
    {
130
        $userEntity->setUname('ghost');
131
        $userEntity->setEmail('[email protected]');
132
        $userEntity->setActivated(UsersConstant::ACTIVATED_PENDING_DELETE);
133
        $userEntity->removeGroups();
134
        foreach ($userEntity->getAttributes() as $attribute) {
135
            $userEntity->delAttribute($attribute->getName());
136
        }
137
        $this->userRepository->persistAndFlush($userEntity);
138
    }
139
}
140