Test Setup Failed
Push — master ( e4e3b8...4a8c65 )
by Craig
05:26
created

DeleteHelper   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 90
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 44
dl 0
loc 90
rs 10
c 1
b 0
f 0
wmc 11

3 Methods

Rating   Name   Duplication   Size   Complexity  
A deleteUser() 0 7 2
A __construct() 0 10 1
B getUserCollection() 0 40 8
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Zikula package.
7
 *
8
 * Copyright Zikula Foundation - 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\CoreBundle\Event\GenericEvent;
20
use Zikula\Bundle\HookBundle\Dispatcher\HookDispatcherInterface;
21
use Zikula\Bundle\HookBundle\Hook\ProcessHook;
22
use Zikula\GroupsModule\Constant;
23
use Zikula\GroupsModule\Entity\RepositoryInterface\GroupRepositoryInterface;
24
use Zikula\UsersModule\Constant as UsersConstant;
25
use Zikula\UsersModule\Entity\RepositoryInterface\UserRepositoryInterface;
26
use Zikula\UsersModule\Entity\UserEntity;
27
use Zikula\UsersModule\HookSubscriber\UserManagementUiHooksSubscriber;
28
use Zikula\UsersModule\RegistrationEvents;
29
use Zikula\UsersModule\UserEvents;
30
31
class DeleteHelper
32
{
33
    /**
34
     * @var EventDispatcherInterface
35
     */
36
    private $eventDispatcher;
37
38
    /**
39
     * @var HookDispatcherInterface
40
     */
41
    private $hookDispatcher;
42
43
    /**
44
     * @var UserRepositoryInterface
45
     */
46
    private $userRepository;
47
48
    /**
49
     * @var GroupRepositoryInterface
50
     */
51
    private $groupRespository;
52
53
    public function __construct(
54
        EventDispatcherInterface $eventDispatcher,
55
        HookDispatcherInterface $hookDispatcher,
56
        UserRepositoryInterface $userRepository,
57
        GroupRepositoryInterface $groupRespository
58
    ) {
59
        $this->eventDispatcher = $eventDispatcher;
60
        $this->hookDispatcher = $hookDispatcher;
61
        $this->userRepository = $userRepository;
62
        $this->groupRespository = $groupRespository;
63
    }
64
65
    /**
66
     * @param string $param gid|status|uid
67
     * @param string $value
68
     * @param string|null $date
69
     *
70
     * @return \Doctrine\Common\Collections\Collection
71
     */
72
    public function getUserCollection(string $param, string $value, string $date = null): Collection
73
    {
74
        switch ($param) {
75
            case 'gid':
76
                if (in_array((int) $value, [Constant::GROUP_ID_USERS, Constant::GROUP_ID_ADMIN])) {
77
                    throw new \InvalidArgumentException('Cannot delete from main User or Administrator group.');
78
                }
79
                $users = $this->groupRespository->find((int) $value)->getUsers();
80
                break;
81
            case 'status':
82
                $statuses = [
83
                    'I' => UsersConstant::ACTIVATED_INACTIVE,
84
                    'P' => UsersConstant::ACTIVATED_PENDING_REG,
85
                    'M' => UsersConstant::ACTIVATED_PENDING_DELETE
86
                ];
87
                if (!array_key_exists($value, $statuses)) {
88
                    throw new \InvalidArgumentException('Invalid status key');
89
                }
90
                $users = $this->userRepository->findBy(['activated' => $statuses[$value]]);
91
                $users = new ArrayCollection($users);
92
                break;
93
            case 'uid':
94
                $user = $this->userRepository->find((int) $value);
95
                $users = new ArrayCollection([$user]);
96
                break;
97
            default:
98
                throw new \InvalidArgumentException('Invalid option name');
99
        }
100
        if (isset($date)) {
101
            $date = \DateTime::createFromFormat('YmdHis', $date, new \DateTimeZone('UTC'));
102
            $users = $users->filter(function (UserEntity $user) use ($date) {
103
                return $user->getRegistrationDate() < $date;
104
            });
105
        }
106
        $adminUser = $this->userRepository->find(UsersConstant::USER_ID_ADMIN);
107
        if ($users->contains($adminUser)) {
108
            $users->removeElement($adminUser);
109
        }
110
111
        return $users;
112
    }
113
114
    public function deleteUser(UserEntity $user): void
115
    {
116
        $eventName = UsersConstant::ACTIVATED_ACTIVE === $user->getActivated() ? UserEvents::DELETE_ACCOUNT : RegistrationEvents::DELETE_REGISTRATION;
117
        $this->eventDispatcher->dispatch(new GenericEvent($user->getUid()), $eventName);
118
        $this->eventDispatcher->dispatch(new GenericEvent(null, ['id' => $user->getUid()]), UserEvents::DELETE_PROCESS);
119
        $this->hookDispatcher->dispatch(UserManagementUiHooksSubscriber::DELETE_PROCESS, new ProcessHook($user->getUid()));
120
        $this->userRepository->removeAndFlush($user);
121
    }
122
}
123