Passed
Push — master ( 1c7137...b5ef5e )
by Pieter van der
03:26 queued 14s
created

Tiqr_Message_GCM   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 14
eloc 37
c 1
b 0
f 0
dl 0
loc 75
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A _getService() 0 14 2
C send() 0 39 12
1
<?php
2
/**
3
 * This file is part of the tiqr project.
4
 * 
5
 * The tiqr project aims to provide an open implementation for 
6
 * authentication using mobile devices. It was initiated by 
7
 * SURFnet and developed by Egeniq.
8
 *
9
 * More information: http://www.tiqr.org
10
 *
11
 * @author Joost van Dijk <[email protected]>
12
 * 
13
 * @package tiqr
14
 *
15
 * @license New BSD License - See LICENSE file for details.
16
 *
17
 * @copyright (C) 2010-2015 SURFnet BV
18
 */
19
20
21
/** @internal base includes */
22
require_once('Tiqr/Message/Abstract.php');
23
24
require_once 'Zend/Mobile/Push/Gcm.php';
25
require_once 'Zend/Mobile/Push/Message/Gcm.php';
26
27
/**
28
 * Android Cloud To Device Messaging message.
29
 * @author peter
30
 */
31
class Tiqr_Message_GCM extends Tiqr_Message_Abstract
32
{
33
    private static $_services = array();
34
    
35
    /**
36
     * Factory method for returning a GCM service instance for the given 
37
     * configuration options.
38
     *
39
     * @param array $options configuration options
40
     *
41
     * @return Zend_Mobile_Push_Gcm service instance
42
     *
43
     * @throws Tiqr_Message_Exception_AuthFailure
44
     */
45
    private static function _getService($options)
46
    {
47
        $apikey      = $options['gcm.apikey'];
48
        $application = $options['gcm.application'];
49
50
        $key = "{apikey}@{$application}";
51
52
        if (!isset(self::$_services[$key])) {
53
            $service = new Zend_Mobile_Push_Gcm();
54
            $service->setApiKey($apikey);
55
            self::$_services[$key] = $service;
56
        }
57
        
58
        return self::$_services[$key];
59
    }
60
    
61
    /**
62
     * Send message.
63
     *
64
     * @throws Tiqr_Message_Exception_AuthFailure
65
     * @throws Tiqr_Message_Exception_SendFailure
66
     */
67
    public function send()
68
    {
69
        $service = self::_getService($this->getOptions());
70
        
71
        $data = $this->getCustomProperties();
72
        $data['text'] = $this->getText();
73
74
        $message = new Zend_Mobile_Push_Message_Gcm();
75
        $message->addToken($this->getAddress());
76
        $message->setId($this->getId()); // TODO: GCM equivalent needed?
77
        $message->setData($data);
78
79
        try {
80
            $response = $service->send($message);
81
        } catch (Zend_Http_Client_Exception $e) {
82
            throw new Tiqr_Message_Exception_SendFailure("HTTP client error", true, $e);
83
        } catch (Zend_Mobile_Push_Exception_ServerUnavailable $e) {
84
            throw new Tiqr_Message_Exception_SendFailure("Server unavailable", true, $e);
85
        } catch (Zend_Mobile_Push_Exception_InvalidAuthToken $e) {
86
            throw new Tiqr_Message_Exception_AuthFailure("Invalid token", $e);
87
        } catch (Zend_Mobile_Push_Exception_InvalidPayload $e) {
88
            throw new Tiqr_Message_Exception_SendFailure("Payload too large", $e);
0 ignored issues
show
Bug introduced by
$e of type Zend_Mobile_Push_Exception_InvalidPayload is incompatible with the type boolean expected by parameter $temporary of Tiqr_Message_Exception_SendFailure::__construct(). ( Ignorable by Annotation )

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

88
            throw new Tiqr_Message_Exception_SendFailure("Payload too large", /** @scrutinizer ignore-type */ $e);
Loading history...
89
        } catch (Zend_Mobile_Push_Exception $e) {
90
            throw new Tiqr_Message_Exception_SendFailure("General send error", false, $e);
91
        }
92
93
        // handle errors, ignoring registration_id's
94
        $error = null;
95
        foreach ($response->getResults() as $k => $v) {
96
            if (isset($v['error']) && $v['error'] === "MismatchSenderId") {
97
                throw new Tiqr_Message_Exception_MismatchSenderId("MismatchSenderId", true);
98
            }
99
            if (isset($v['error']) && is_null($error)) {
100
                $error = $v['error'];
101
            }
102
        }
103
104
        if ($error != null) {
105
            throw new Tiqr_Message_Exception_SendFailure("Error in GCM response: " . $error, true);
106
        }
107
108
    }
109
}
110