Completed
Push — master ( 6090d6...edf18c )
by Craig
09:38 queued 03:17
created

MigrationHelper::countUnMigratedUsers()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 9
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 6
c 1
b 0
f 0
nc 1
nop 0
dl 9
loc 9
rs 9.6666
1
<?php
2
3
/*
4
 * This file is part of the Zikula package.
5
 *
6
 * Copyright Zikula Foundation - http://zikula.org/
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Zikula\Bundle\CoreInstallerBundle\Helper;
13
14
use Doctrine\Common\Persistence\ObjectManager;
15
use Doctrine\DBAL\Connection;
16
use Psr\Log\LoggerInterface;
17
use Symfony\Bridge\Doctrine\RegistryInterface;
18
use Symfony\Bridge\Monolog\Logger;
19
use Symfony\Component\Validator\Validator\ValidatorInterface;
20
use Zikula\UsersModule\Constant as UsersConstant;
21
use Zikula\ZAuthModule\Entity\AuthenticationMappingEntity;
22
use Zikula\ZAuthModule\ZAuthConstant;
23
24
/**
25
 * Class MigrationHelper
26
 */
27
class MigrationHelper
28
{
29
    const BATCH_LIMIT = 25;
30
31
    /**
32
     * @var Connection
33
     */
34
    private $conn;
35
36
    /**
37
     * @var ObjectManager
38
     */
39
    private $manager;
40
41
    /**
42
     * @var ValidatorInterface
43
     */
44
    private $validator;
45
46
    /**
47
     * @var Logger
48
     */
49
    private $logger;
50
51
    /**
52
     * MigrationHelper constructor.
53
     * @param RegistryInterface $registry
54
     * @param ValidatorInterface $validator
55
     * @param LoggerInterface $logger
56
     */
57
    public function __construct(
58
        RegistryInterface $registry,
59
        ValidatorInterface $validator,
60
        LoggerInterface $logger
61
    ) {
62
        $this->conn = $registry->getConnection();
63
        $this->manager = $registry->getManager();
64
        $this->validator = $validator;
65
        $this->logger = $logger;
0 ignored issues
show
Documentation Bug introduced by
It seems like $logger of type object<Psr\Log\LoggerInterface> is incompatible with the declared type object<Symfony\Bridge\Monolog\Logger> of property $logger.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
66
    }
67
68
    /**
69
     * @param array $user
70
     * @param string $method
71
     * @return AuthenticationMappingEntity|null
72
     * @throws \Exception
73
     */
74
    public function createMappingFromUser($user, $method = ZAuthConstant::AUTHENTICATION_METHOD_EITHER)
75
    {
76
        $query = $this->conn->createQueryBuilder()
77
            ->select('*')
78
            ->from('zauth_authentication_mapping', 'z')
79
            ->where('z.uid = ?')
80
            ->setParameter(0, $user['uid'])
81
            ->setMaxResults(1);
82
        $mapping = $query->execute()->fetchAll();
83
        if (count($mapping) > 0) {
84
            return null;
85
        }
86
87
        $mapping = new AuthenticationMappingEntity();
88
        $mapping->setUid($user['uid']);
89
        $mapping->setUname($user['uname']);
90
        $mapping->setEmail($user['email']);
91
        $mapping->setVerifiedEmail(true);
92
        $mapping->setPass($user['pass']); // previously salted and hashed
93
        $mapping->setMethod($method);
94
        $errors = $this->validator->validate($mapping);
95
        if ($errors->count() > 0) {
96
            foreach ($errors as $error) {
97
                $this->logger->addError('Unable to migrate user (' . $user['uname'] . '/' . $user['email'] . ') because: ' . $error->getMessage());
98
            }
99
100
            return null;
101
        }
102
103
        return $mapping;
104
    }
105
106
    /**
107
     * @param $uid
108
     * @param $limit
109
     * @return array
110
     */
111
    private function getUnMigratedUsers($uid, $limit)
112
    {
113
        $qb = $this->conn->createQueryBuilder();
114
        $query = $qb
115
            ->select('*')
116
            ->from('users', 'u')
117
            ->where(
118
                $qb->expr()->andX(
119
                    $qb->expr()->gt('u.uid', '?'),
120
                    $qb->expr()->neq('u.pass', "''")
121
                )
122
            )
123
            ->setParameter(0, $uid)
124
            ->orderBy('u.uid', 'ASC')
125
            ->setMaxResults($limit);
126
127
        return $query->execute()->fetchAll();
128
    }
129
130 View Code Duplication
    public function getMaxUnMigratedUid()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
131
    {
132
        $query = $this->conn->createQueryBuilder()
133
            ->select('MAX(u.uid) as max')
134
            ->from('users', 'u')
135
            ->where("u.pass != ''");
136
137
        return $query->execute()->fetchColumn();
138
    }
139
140 View Code Duplication
    public function countUnMigratedUsers()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
141
    {
142
        $query = $this->conn->createQueryBuilder()
143
            ->select('COUNT(u.uid) as count')
144
            ->from('users', 'u')
145
            ->where("u.pass != ''");
146
147
        return $query->execute()->fetchColumn();
148
    }
149
150
    public function migrateUsers($lastUid)
151
    {
152
        $users = $this->getUnMigratedUsers($lastUid, self::BATCH_LIMIT);
153
        $complete = 0;
154
        foreach ($users as $user) {
155
            $mapping = $this->createMappingFromUser($user);
156
            if ($mapping) {
157
                $this->manager->persist($mapping);
158
                $this->conn->insert('users_attributes', [
159
                    'user_id' => $user['uid'],
160
                    'name' => UsersConstant::AUTHENTICATION_METHOD_ATTRIBUTE_KEY,
161
                    'value' => $mapping->getMethod()
162
                ]);
163
                $this->conn->update('users', ['pass' => ''], ['uid' => $user['uid']]);
164
                $complete++;
165
            }
166
            $lastUid = $user['uid'];
167
        }
168
        $this->manager->flush();
169
170
        return ['lastUid' => $lastUid, 'complete' => $complete];
171
    }
172
}
173