Completed
Push — develop ( c0e59c...ad429f )
by Marek
02:45
created

UserService::getFollowing()   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\Encoder\EncoderFactory;
7
8
class UserService
9
{
10
    /** @var EncoderFactory */
11
    private $encoderFactory;
12
13
    /** @var UserRepository */
14
    private $userRepository;
15
16
    public function __construct(EncoderFactory $encoderFactory, UserRepository $userRepository)
17
    {
18
        $this->encoderFactory = $encoderFactory;
19
        $this->userRepository = $userRepository;
20
    }
21
22
    /**
23
     * @param integer $id
24
     *
25
     * @return null|User
26
     */
27
    public function getUserById($id)
28
    {
29
        $user = $this->userRepository->findOneBy(['id' => $id]);
30
31
        return $user;
32
    }
33
34
    /**
35
     * @param string $login
36
     *
37
     * @return null|User
38
     */
39
    public function getUserByLogin($login)
40
    {
41
        $user = $this->userRepository->findOneBy(['login' => $login]);
42
43
        return $user;
44
    }
45
46
    //region Password
47
48
    public function changePassword(User $user, $password)
49
    {
50
        $encoder = $this->encoderFactory->getEncoder($user);
51
52
        $user->setPassword($encoder->encodePassword($password, ''));
53
        $user->setDatePasswordChanged(new \DateTime());
54
55
        $this->userRepository->persist($user);
56
    }
57
58
    public function validatePassword(User $user, $password)
59
    {
60
        $encoder = $this->encoderFactory->getEncoder($user);
61
62
        return $encoder->isPasswordValid($user->getPassword(), $password, '');
63
    }
64
65
    //endregion
66
67
    //region Followers and following
68
69
    /**
70
     * Get user's followers
71
     *
72
     * @param int $userId
73
     *
74
     * @return User[]
75
     */
76
    public function getFollowers($userId)
77
    {
78
        //TODO permissions
79
80
        return $this->userRepository->findAllFollowers($userId);
81
    }
82
83
    /**
84
     * Get users followed by user
85
     *
86
     * @param int $userId
87
     *
88
     * @return User[]
89
     */
90
    public function getFollowing($userId)
91
    {
92
        //TODO permissions
93
94
        return $this->userRepository->findAllFollowing($userId);
95
    }
96
97
    /**
98
     * Get active users followed by user
99
     *
100
     * @param int $userId
101
     *
102
     * @return User[]
103
     */
104
    public function getActiveFollowing($userId)
105
    {
106
        //TODO permissions
107
108
        return $this->userRepository->findActiveFollowing($userId);
109
    }
110
111
    //endregion
112
}
113