Passed
Branch master (b73fcd)
by Thomas
02:06
created

ZohoOAuth::setConfigValues()   B

Complexity

Conditions 9
Paths 24

Size

Total Lines 33
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 25
c 1
b 0
f 0
dl 0
loc 33
rs 8.0555
cc 9
nc 24
nop 1
1
<?php
2
namespace Zoho\OAuth;
3
4
use Exception;
5
use Zoho\OAuth\Exception\ZohoOAuthException;
6
use Zoho\OAuth\Persistence\ZohoOAuthPersistenceByFile;
7
use Zoho\OAuth\Persistence\ZohoOAuthPersistenceHandler;
8
use Zoho\OAuth\Utility\ZohoOAuthConstants;
9
use Zoho\OAuth\Utility\ZohoOAuthParams;
10
11
class ZohoOAuth
12
{
13
14
    private static $configProperties = [];
15
16
    public static function initialize($configuration)
17
    {
18
        self::setConfigValues($configuration);
19
        if (!array_key_exists(ZohoOAuthConstants::TOKEN_PERSISTENCE_PATH,
20
                self::$configProperties) || self::$configProperties[ZohoOAuthConstants::TOKEN_PERSISTENCE_PATH] == "") {
21
            if (!array_key_exists(ZohoOAuthConstants::DATABASE_PORT, self::$configProperties)) {
22
                self::$configProperties[ZohoOAuthConstants::DATABASE_PORT] = "3306";
23
            }
24
            if (!array_key_exists(ZohoOAuthConstants::DATABASE_USERNAME, self::$configProperties)) {
25
                self::$configProperties[ZohoOAuthConstants::DATABASE_USERNAME] = "root";
26
            }
27
            if (!array_key_exists(ZohoOAuthConstants::DATABASE_PASSWORD, self::$configProperties)) {
28
                self::$configProperties[ZohoOAuthConstants::DATABASE_PASSWORD] = "";
29
            }
30
            if (!array_key_exists(ZohoOAuthConstants::DATABASE_NAME, self::$configProperties)) {
31
                self::$configProperties[ZohoOAuthConstants::DATABASE_NAME] = "zohooauth";
32
            }
33
            if (!array_key_exists(ZohoOAuthConstants::HOST_ADDRESS, self::$configProperties)) {
34
                self::$configProperties[ZohoOAuthConstants::HOST_ADDRESS] = "localhost";
35
            }
36
        }
37
        $oAuthParams = new ZohoOAuthParams();
38
        $oAuthParams->setAccessType(self::getConfigValue(ZohoOAuthConstants::ACCESS_TYPE));
39
        $oAuthParams->setClientId(self::getConfigValue(ZohoOAuthConstants::CLIENT_ID));
40
        $oAuthParams->setClientSecret(self::getConfigValue(ZohoOAuthConstants::CLIENT_SECRET));
41
        $oAuthParams->setRedirectURL(self::getConfigValue(ZohoOAuthConstants::REDIRECT_URL));
42
        ZohoOAuthClient::getInstance($oAuthParams);
43
    }
44
45
    private static function setConfigValues($configuration)
46
    {
47
        $config_keys = [
48
            ZohoOAuthConstants::CLIENT_ID,
49
            ZohoOAuthConstants::CLIENT_SECRET,
50
            ZohoOAuthConstants::REDIRECT_URL,
51
            ZohoOAuthConstants::ACCESS_TYPE,
52
            ZohoOAuthConstants::PERSISTENCE_HANDLER_CLASS,
53
            ZohoOAuthConstants::IAM_URL,
54
            ZohoOAuthConstants::TOKEN_PERSISTENCE_PATH,
55
            ZohoOAuthConstants::DATABASE_PORT,
56
            ZohoOAuthConstants::DATABASE_PASSWORD,
57
            ZohoOAuthConstants::DATABASE_USERNAME,
58
            ZohoOAuthConstants::PERSISTENCE_HANDLER_CLASS_NAME,
59
            ZohoOAuthConstants::HOST_ADDRESS,
60
        ];
61
62
        if (!array_key_exists(ZohoOAuthConstants::ACCESS_TYPE,
63
                $configuration) || $configuration[ZohoOAuthConstants::ACCESS_TYPE] == "") {
64
            self::$configProperties[ZohoOAuthConstants::ACCESS_TYPE] = "offline";
65
        }
66
        if (!array_key_exists(ZohoOAuthConstants::PERSISTENCE_HANDLER_CLASS,
67
                $configuration) || $configuration[ZohoOAuthConstants::PERSISTENCE_HANDLER_CLASS] == "") {
68
            self::$configProperties[ZohoOAuthConstants::PERSISTENCE_HANDLER_CLASS] = "ZohoOAuthPersistenceHandler";
69
        }
70
        if (!array_key_exists(ZohoOAuthConstants::IAM_URL,
71
                $configuration) || $configuration[ZohoOAuthConstants::IAM_URL] == "") {
72
            self::$configProperties[ZohoOAuthConstants::IAM_URL] = "https://accounts.zoho.com";
73
        }
74
75
        foreach ($config_keys as $key) {
76
            if (array_key_exists($key, $configuration)) {
77
                self::$configProperties[$key] = $configuration[$key];
78
            }
79
        }
80
    }
81
82
    public static function getConfigValue($key)
83
    {
84
        return isset(self::$configProperties[$key]) ? self::$configProperties[$key] : "";
85
    }
86
87
    public static function getAllConfigs()
88
    {
89
        return self::$configProperties;
90
    }
91
92
    public static function getGrantURL()
93
    {
94
        return self::getIAMUrl() . "/oauth/v2/auth";
95
    }
96
97
    public static function getIAMUrl()
98
    {
99
        return self::getConfigValue(ZohoOAuthConstants::IAM_URL);
100
    }
101
102
    public static function getTokenURL()
103
    {
104
        return self::getIAMUrl() . "/oauth/v2/token";
105
    }
106
107
    public static function getRefreshTokenURL()
108
    {
109
        return self::getIAMUrl() . "/oauth/v2/token";
110
    }
111
112
    public static function getRevokeTokenURL()
113
    {
114
        return self::getIAMUrl() . "/oauth/v2/token/revoke";
115
    }
116
117
    public static function getUserInfoURL()
118
    {
119
        return self::getIAMUrl() . "/oauth/user/info";
120
    }
121
122
    public static function getClientID()
123
    {
124
        return self::getConfigValue(ZohoOAuthConstants::CLIENT_ID);
125
    }
126
127
    public static function getClientSecret()
128
    {
129
        return self::getConfigValue(ZohoOAuthConstants::CLIENT_SECRET);
130
    }
131
132
    public static function getRedirectURL()
133
    {
134
        return self::getConfigValue(ZohoOAuthConstants::REDIRECT_URL);
135
    }
136
137
    public static function getAccessType()
138
    {
139
        return self::getConfigValue(ZohoOAuthConstants::ACCESS_TYPE);
140
    }
141
142
    public static function getPersistenceHandlerInstance()
143
    {
144
        try {
145
            if (ZohoOAuth::getConfigValue("token_persistence_path") != "") {
146
                return new ZohoOAuthPersistenceByFile();
147
            } else {
148
                if (self::$configProperties[ZohoOAuthConstants::PERSISTENCE_HANDLER_CLASS] == "ZohoOAuthPersistenceHandler") {
149
                    return new ZohoOAuthPersistenceHandler();
150
                } else {
151
                    require_once realpath(self::$configProperties[ZohoOAuthConstants::PERSISTENCE_HANDLER_CLASS]);
152
                    $str = self::$configProperties[ZohoOAuthConstants::PERSISTENCE_HANDLER_CLASS_NAME];
153
154
                    return new $str();
155
                }
156
            }
157
        } catch (Exception $ex) {
158
            throw new ZohoOAuthException($ex);
159
        }
160
    }
161
162
    public static function getClientInstance()
163
    {
164
        if (ZohoOAuthClient::getInstanceWithOutParam() == null) {
165
            throw new ZohoOAuthException("ZCRMRestClient::initialize(\$configMap) must be called before this.");
166
167
        }
168
169
        return ZohoOAuthClient::getInstanceWithOutParam();
170
    }
171
}