Completed
Push — search ( 076ba7...2a5267 )
by Simon
16:36 queued 11:48
created

RefreshOAuthDataTask   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
eloc 29
c 1
b 0
f 0
dl 0
loc 50
ccs 0
cts 38
cp 0
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B execute() 0 48 6
1
<?php
2
/******************************************************************************
3
 * Wikipedia Account Creation Assistance tool                                 *
4
 *                                                                            *
5
 * All code in this file is released into the public domain by the ACC        *
6
 * Development Team. Please see team.json for a list of contributors.         *
7
 ******************************************************************************/
8
9
namespace Waca\ConsoleTasks;
10
11
use Exception;
12
use PDO;
13
use Waca\DataObjects\User;
14
use Waca\Exceptions\OAuthException;
15
use Waca\Helpers\OAuthUserHelper;
16
use Waca\Helpers\SearchHelpers\UserSearchHelper;
17
use Waca\Tasks\ConsoleTaskBase;
18
19
class RefreshOAuthDataTask extends ConsoleTaskBase
20
{
21
    public function execute()
22
    {
23
        $database = $this->getDatabase();
24
25
        $idList = $database
26
            ->query('SELECT user FROM oauthtoken WHERE type = \'access\' AND expiry IS NULL')
27
            ->fetchAll(PDO::FETCH_COLUMN);
28
29
        if (count($idList) > 0) {
30
            /** @var User[] $users */
31
            $users = UserSearchHelper::get($database)->inIds($idList)->fetch();
32
33
            $expiredStatement = $database
34
                ->prepare('UPDATE oauthtoken SET expiry = CURRENT_TIMESTAMP() WHERE user = :u AND type = \'access\'');
35
36
            foreach ($users as $u) {
37
                try {
38
                    $database->beginTransaction();
39
40
                    $oauth = new OAuthUserHelper($u, $database, $this->getOAuthProtocolHelper(),
41
                        $this->getSiteConfiguration());
42
43
                    try {
44
                        $oauth->refreshIdentity();
45
                    }
46
                    catch (OAuthException $ex) {
47
                        $expiredStatement->execute(array(':u' => $u->getId()));
48
                    }
49
50
                    $database->commit();
51
                }
52
                catch (Exception $ex) {
53
                    $database->rollBack();
54
55
                    printf("\n\nFailed updating OAuth data for %s\n", $u->getUsername());
56
                    printf($ex->getMessage());
57
                }
58
                finally {
59
                    if ($database->hasActiveTransaction()) {
60
                        $database->rollBack();
61
                    }
62
                }
63
            }
64
        }
65
66
        $database->beginTransaction();
67
        $database->exec('DELETE FROM oauthtoken WHERE expiry IS NOT NULL AND expiry < NOW() AND type = \'request\'');
68
        $database->commit();
69
    }
70
}