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

Tiqr_Message_C2DM   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 12
eloc 40
c 2
b 0
f 0
dl 0
loc 74
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
B send() 0 28 8
A _getService() 0 23 4
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 Peter Verhage <[email protected]>
12
 * 
13
 * @package tiqr
14
 *
15
 * @license New BSD License - See LICENSE file for details.
16
 *
17
 * @copyright (C) 2010-2011 SURFnet BV
18
 */
19
20
21
/** @internal base includes */
22
require_once('Tiqr/Message/Abstract.php');
23
24
require_once('Zend/Gdata/ClientLogin.php');
25
require_once 'Zend/Mobile/Push/C2dm.php';
26
require_once 'Zend/Mobile/Push/Message/C2dm.php';
27
28
/**
29
 * Android Cloud To Device Messaging message.
30
 * @author peter
31
 */
32
class Tiqr_Message_C2DM extends Tiqr_Message_Abstract
33
{
34
    private static $_services = array();
35
    
36
    /**
37
     * Factory method for returning a C2DM service instance for the given 
38
     * configuration options.
39
     *
40
     * @param array $options configuration options
41
     *
42
     * @return Zend_Service_Google_C2dm service instance
0 ignored issues
show
Bug introduced by
The type Zend_Service_Google_C2dm was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
43
     *
44
     * @throws Tiqr_Message_Exception_AuthFailure
45
     */
46
    private static function _getService($options)
47
    {
48
        $username = $options['c2dm.username'];
49
        $password = $options['c2dm.password'];        
50
        $application = $options['c2dm.application'];
51
        
52
        $key = "{$username}:{$password}@{$application}";
53
        
54
        if (!isset(self::$_services[$key])) {
55
            try {
56
                $client = Zend_GData_ClientLogin::getHttpClient($username, $password, Zend_Mobile_Push_C2dm::AUTH_SERVICE_NAME, null, $application);
0 ignored issues
show
Bug introduced by
The type Zend_Mobile_Push_C2dm was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
57
            } catch (Zend_Gdata_App_CaptchaRequiredException $e) {
58
                throw new Tiqr_Message_Exception_AuthFailure("Manual login required", $e);
59
            } catch (Zend_Gdata_App_AuthException $e) {
60
                throw new Tiqr_Message_Exception_AuthFailure("Problem authenticating", $e);                
61
            }            
62
            
63
            $service = new Zend_Mobile_Push_C2dm();
64
            $service->setLoginToken($client->getClientLoginToken());        
65
            self::$_services[$key] = $service;
66
        }
67
        
68
        return self::$_services[$key];
69
    }
70
    
71
    /**
72
     * Send message.
73
     *
74
     * @throws Tiqr_Message_Exception_AuthFailure
75
     * @throws Tiqr_Message_Exception_SendFailure
76
     * @throws Tiqr_Message_Exception_InvalidDevice     
77
     */
78
    public function send()
79
    {
80
        $service = self::_getService($this->getOptions());
81
        
82
        $data = $this->getCustomProperties();
83
        $data['text'] = $this->getText();
84
85
        $message = new Zend_Mobile_Push_Message_C2dm();
0 ignored issues
show
Bug introduced by
The type Zend_Mobile_Push_Message_C2dm was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
86
        $message->setToken($this->getAddress());
87
        $message->setId($this->getId());
88
        $message->setData($data);
89
90
        try {
91
            $service->send($message);
92
        } catch (Zend_Mobile_Push_Exception_QuotaExceeded $e) {
93
            throw new Tiqr_Message_Exception_SendFailure("Device quota exceeded", true, $e);
94
        } catch (Zend_Mobile_Push_Exception_DeviceQuotaExceeded $e) {
95
            throw new Tiqr_Message_Exception_SendFailure("Quota exceeded", true, $e);
96
        } catch (Zend_Mobile_Push_Exception_ServerUnavailable $e) {
97
            throw new Tiqr_Message_Exception_SendFailure("Server unavailable", true, $e);
98
        } catch (Zend_Mobile_Push_Exception_InvalidToken $e) {
99
            throw new Tiqr_Message_Exception_InvalidDevice("Invalid token", $e);
0 ignored issues
show
Bug introduced by
The type Tiqr_Message_Exception_InvalidDevice was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
100
        } catch (Zend_Mobile_Push_Exception_InvalidPayload $e) {
101
            throw new Tiqr_Message_Exception_SendFailure("Invalid payload", false, $e);
102
        } catch (Zend_Mobile_Push_Exception_InvalidTopic $e) {
103
            throw new Tiqr_Message_Exception_SendFailure("Invalid topic", false, $e);
104
        } catch (Zend_Mobile_Push_Exception $e) {
105
            throw new Tiqr_Message_Exception_SendFailure("General send error", false, $e);
106
        }
107
    }
108
}
109