Completed
Push — master ( 8b2aa0...562606 )
by Julito
09:06
created

WebService   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 96
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 24
dl 0
loc 96
rs 10
c 0
b 0
f 0
wmc 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A validate() 0 3 1
A findUserApiKey() 0 16 3
A isValidUser() 0 16 4
A __construct() 0 5 1
A getUser() 0 3 1
1
<?php
2
/* For licensing terms, see /license.txt */
3
4
use Chamilo\UserBundle\Entity\User;
5
6
/**
7
 * Base class for Web Services.
8
 *
9
 * @author Angel Fernando Quiroz Campos <[email protected]>
10
 *
11
 * @package chamilo.webservices
12
 */
13
class WebService
14
{
15
    /**
16
     * @var User
17
     */
18
    protected $user;
19
    /**
20
     * @var string
21
     */
22
    protected $apiKey;
23
24
    /**
25
     * Class constructor.
26
     *
27
     * @param $username
28
     * @param $apiKey
29
     */
30
    protected function __construct($username, $apiKey)
31
    {
32
        /** @var User user */
33
        $this->user = UserManager::getManager()->findUserByUsername($username);
34
        $this->apiKey = $apiKey;
35
    }
36
37
    /**
38
     * @param string $username
39
     * @param string $apiKeyToValidate
40
     *
41
     * @return WebService
42
     */
43
    public static function validate($username, $apiKeyToValidate)
44
    {
45
        return new self($username, $apiKeyToValidate);
46
    }
47
48
    /**
49
     * Find the api key for a user. If the api key does not exists is created.
50
     *
51
     * @param string $username
52
     * @param string $serviceName
53
     *
54
     * @return string
55
     */
56
    public static function findUserApiKey($username, $serviceName)
57
    {
58
        $user = UserManager::getManager()->findUserByUsername($username);
59
        if ($user) {
60
            $apiKeys = UserManager::get_api_keys($user->getId(), $serviceName);
61
62
            if (empty($apiKeys)) {
63
                UserManager::add_api_key($user->getId(), $serviceName);
64
            }
65
66
            $apiKeys = UserManager::get_api_keys($user->getId(), $serviceName);
67
68
            return current($apiKeys);
0 ignored issues
show
Bug introduced by
It seems like $apiKeys can also be of type false; however, parameter $array of current() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

68
            return current(/** @scrutinizer ignore-type */ $apiKeys);
Loading history...
69
        }
70
71
        return '';
72
    }
73
74
    /**
75
     * Check whether the username and password are valid.
76
     *
77
     * @param string $username
78
     * @param string $password
79
     *
80
     * @throws Exception
81
     *
82
     * @return bool Return true if the password belongs to the username. Otherwise return false
83
     */
84
    public static function isValidUser($username, $password)
85
    {
86
        if (empty($username) || empty($password)) {
87
            return false;
88
        }
89
90
        $user = UserManager::getManager()->findUserByUsername($username);
91
92
        if (!$user) {
93
            return false;
94
        }
95
96
        return UserManager::isPasswordValid(
97
            $user->getPassword(),
98
            $password,
99
            $user->getSalt()
100
        );
101
    }
102
103
    /**
104
     * @return User
105
     */
106
    public function getUser()
107
    {
108
        return $this->user;
109
    }
110
}
111