Completed
Push — develop ( 456f1c...054456 )
by Marek
03:20
created

UserService::getActiveFollowing()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
namespace AppBundle\Service;
3
4
use AppBundle\Entity\Repository\UserRepository;
5
use AppBundle\Entity\User;
6
use Symfony\Component\Security\Core\User\UserInterface;
7
8
class UserService
9
{
10
    /** @var UserRepository */
11
    private $userRepository;
12
13
    public function __construct(UserRepository $userRepository)
14
    {
15
        $this->userRepository = $userRepository;
16
    }
17
18
    /**
19
     * @param integer $id
20
     *
21
     * @return null|User
22
     */
23
    public function getUserById($id)
24
    {
25
        $user = $this->userRepository->findOneBy(['id' => $id]);
26
27
        return $user;
28
    }
29
30
    /**
31
     * @param string $login
32
     *
33
     * @return null|User
34
     */
35
    public function getUserByLogin($login)
36
    {
37
        $user = $this->userRepository->findOneBy(['login' => $login]);
38
39
        return $user;
40
    }
41
42
    //region Password
43
44
    public function validatePassword(UserInterface $user, $password)
45
    {
46
        $hash = $user->getPassword();
47
48
        if (strlen($hash) === 32) {
49
            // Oldest MD5 passwords
50
            return hash_equals($hash, md5($password));
51
        } elseif (strlen($hash) === 80 && $hash[0] !== '$') {
52
            // Salted SHA1
53
            $hashParts = str_split($hash, 20);
54
            $salt = $hashParts[0].$hashParts[3];
55
            $sha1Hash = $hashParts[1].$hashParts[2];
56
57
            return hash_equals($sha1Hash, sha1($salt.md5($password)));
58
        } else {
59
            return password_verify($password, $hash);
60
        }
61
    }
62
63
    //endregion
64
65
    //region Followers and following
66
67
    /**
68
     * Get user's followers
69
     *
70
     * @param int $userId
71
     *
72
     * @return User[]
73
     */
74
    public function getFollowers($userId)
75
    {
76
        //TODO permissions
77
78
        return $this->userRepository->findAllFollowers($userId);
79
    }
80
81
    /**
82
     * Get users followed by user
83
     *
84
     * @param int $userId
85
     *
86
     * @return User[]
87
     */
88
    public function getFollowing($userId)
89
    {
90
        //TODO permissions
91
92
        return $this->userRepository->findAllFollowing($userId);
93
    }
94
95
    /**
96
     * Get active users followed by user
97
     *
98
     * @param int $userId
99
     *
100
     * @return User[]
101
     */
102
    public function getActiveFollowing($userId)
103
    {
104
        //TODO permissions
105
106
        return $this->userRepository->findActiveFollowing($userId);
107
    }
108
109
    //endregion
110
}
111