1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* (c) Alexander Zhukov <[email protected]> |
5
|
|
|
* |
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
7
|
|
|
* file that was distributed with this source code. |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace Zbox\UnifiedPush\NotificationService; |
11
|
|
|
|
12
|
|
|
use Zbox\UnifiedPush\Exception\DomainException; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class NotificationServices |
16
|
|
|
* @package Zbox\UnifiedPush\NotificationService |
17
|
|
|
*/ |
18
|
|
|
class NotificationServices |
19
|
|
|
{ |
20
|
|
|
const APPLE_PUSH_NOTIFICATIONS_SERVICE = 'APNS'; |
21
|
|
|
const GOOGLE_CLOUD_MESSAGING = 'GCM'; |
22
|
|
|
const MICROSOFT_PUSH_NOTIFICATIONS_SERVICE = 'MPNS'; |
23
|
|
|
|
24
|
|
|
const CREDENTIALS_NULL = 1; |
25
|
|
|
const CREDENTIALS_CERTIFICATE = 2; |
26
|
|
|
const CREDENTIALS_AUTH_TOKEN = 3; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* List of supported notification services |
30
|
|
|
* |
31
|
|
|
* @return array |
32
|
|
|
*/ |
33
|
|
|
public static function getAvailableServices() |
34
|
|
|
{ |
35
|
|
|
return |
36
|
|
|
array( |
37
|
|
|
self::APPLE_PUSH_NOTIFICATIONS_SERVICE, |
38
|
|
|
self::GOOGLE_CLOUD_MESSAGING, |
39
|
|
|
self::MICROSOFT_PUSH_NOTIFICATIONS_SERVICE |
40
|
|
|
); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Checks if notification service is supported |
45
|
|
|
* |
46
|
|
|
* @param string $serviceName |
47
|
|
|
* @return string |
48
|
|
|
* @throws DomainException |
49
|
|
|
*/ |
50
|
|
|
public static function validateServiceName($serviceName) |
51
|
|
|
{ |
52
|
|
|
if (!in_array($serviceName, self::getAvailableServices())) { |
53
|
|
|
throw new DomainException(sprintf("Notification service '%s' is not supported.", $serviceName)); |
54
|
|
|
} |
55
|
|
|
return $serviceName; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param string $serviceName |
60
|
|
|
* @return int |
61
|
|
|
*/ |
62
|
|
|
public static function getCredentialsTypeByService($serviceName) |
63
|
|
|
{ |
64
|
|
|
self::validateServiceName($serviceName); |
65
|
|
|
|
66
|
|
|
$credentials = array( |
67
|
|
|
self::APPLE_PUSH_NOTIFICATIONS_SERVICE => self::CREDENTIALS_CERTIFICATE, |
68
|
|
|
self::GOOGLE_CLOUD_MESSAGING => self::CREDENTIALS_AUTH_TOKEN, |
69
|
|
|
self::MICROSOFT_PUSH_NOTIFICATIONS_SERVICE => self::CREDENTIALS_CERTIFICATE |
70
|
|
|
); |
71
|
|
|
|
72
|
|
|
return $credentials[$serviceName]; |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|