Code Duplication    Length = 23-23 lines in 2 locations

src/Xtools/UserRepository.php 2 locations

@@ 41-63 (lines=23) @@
38
     * @param string $username The username to find.
39
     * @return int
40
     */
41
    public function getId($databaseName, $username)
42
    {
43
        // Use md5 to ensure the key does not contain reserved characters.
44
        $cacheKey = 'user_id.'.$databaseName.'.'.md5($username);
45
        if ($this->cache->hasItem($cacheKey)) {
46
            return $this->cache->getItem($cacheKey)->get();
47
        }
48
49
        $userTable = $this->getTableName($databaseName, 'user');
50
        $sql = "SELECT user_id FROM $userTable WHERE user_name = :username LIMIT 1";
51
        $resultQuery = $this->getProjectsConnection()->prepare($sql);
52
        $resultQuery->bindParam("username", $username);
53
        $resultQuery->execute();
54
        $userId = (int)$resultQuery->fetchColumn();
55
56
        // Cache for 10 minutes.
57
        $cacheItem = $this->cache
58
            ->getItem($cacheKey)
59
            ->set($userId)
60
            ->expiresAfter(new DateInterval('PT10M'));
61
        $this->cache->save($cacheItem);
62
        return $userId;
63
    }
64
65
    /**
66
     * Get the user's registration date.
@@ 71-93 (lines=23) @@
68
     * @param string $username The username to find.
69
     * @return string|null As returned by the database.
70
     */
71
    public function getRegistrationDate($databaseName, $username)
72
    {
73
        // Use md5 to ensure the key does not contain reserved characters.
74
        $cacheKey = 'user_registration.'.$databaseName.'.'.md5($username);
75
        if ($this->cache->hasItem($cacheKey)) {
76
            return $this->cache->getItem($cacheKey)->get();
77
        }
78
79
        $userTable = $this->getTableName($databaseName, 'user');
80
        $sql = "SELECT user_registration FROM $userTable WHERE user_name = :username LIMIT 1";
81
        $resultQuery = $this->getProjectsConnection()->prepare($sql);
82
        $resultQuery->bindParam('username', $username);
83
        $resultQuery->execute();
84
        $registrationDate = $resultQuery->fetchColumn();
85
86
        // Cache for 10 minutes.
87
        $cacheItem = $this->cache
88
            ->getItem($cacheKey)
89
            ->set($registrationDate)
90
            ->expiresAfter(new DateInterval('PT10M'));
91
        $this->cache->save($cacheItem);
92
        return $registrationDate;
93
    }
94
95
    /**
96
     * Get group names of the given user.