Passed
Push — master ( d4dd1b...77d1ed )
by Bence
04:11
created

WrapItPushHelper::getAccessToken()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 21
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 4.128

Importance

Changes 0
Metric Value
dl 0
loc 21
ccs 12
cts 15
cp 0.8
rs 9.0534
c 0
b 0
f 0
cc 4
eloc 14
nc 4
nop 0
crap 4.128
1
<?php
2
3
namespace WrapIt\Helpers;
4
5
use WrapIt\WrapIt;
6
use WrapIt\Exceptions\WrapItParameterException;
7
use WrapIt\Http\WrapItApiRequester;
8
use WrapIt\Http\WrapItPushRequester;
9
10
/**
11
 * Class WrapItUserHelper
12
 *
13
 * @package WrapIt
14
 */
15
class WrapItPushHelper {
16
17
    private $access_token = null;
18
19
    private $client_id = null;
20
    private $client_secret = null;
21
    private $domain = null;
22
23
    private $requester;
24
25 10
    public function __construct($wi, $access_token = null) {
26 10
        if (!($wi instanceof WrapIt)) {
27
            throw new WrapItParameterException("WrapIt class required");
28
        }
29
30 10
        $this->client_id = $wi->getClientId();
31 10
        $this->client_secret = $wi->getClientSecret();
32 10
        $this->access_token = $access_token;
33 10
        $this->domain = $wi->getDomain();
34 10
        $this->requester = new WrapItPushRequester($this->domain, $this->getAccessToken());
35 10
    }
36
37 10
    private function getAccessToken() {
38 10
        if ($this->access_token != null) {
39 5
            return $this->access_token;
40
        }
41
42 5
        $apirequester = new WrapItApiRequester($this->domain);
43
44 5
        $data = $apirequester->post("access_token", array(
45 5
            "client_id" => $this->client_id,
46 5
            "client_secret" => $this->client_secret,
47 2
            "grant_type" => "app_token"
48 3
        ));
49
50 5
        if (isset($data["access_token"])) {
51 5
            $this->access_token = $data["access_token"];
52 5
            return $this->access_token;
53
        } else if (isset($data["error"])) {
54
            throw new WrapItParameterException($data["error"]["message"]);
55
        }
56
        throw new WrapItParameterException("Invalid domain or client credentials");
57
    }
58
59
    public function sendPush($userid, $data) {
60
        $data = $this->requester->post("pushservice/$userid", $data);
0 ignored issues
show
Unused Code introduced by
$data is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
61
    }
62
63
    public function getPushTemplate() {
64
        return array(
65
            "categories" => array(),
66
            "device_types" => array(
67
                "mail" => false,
68
                "sms" => false,
69
                "browser" => false,
70
                "phone" => false
71
            ),
72
            "properties" => array(
73
                "attribution" => null,
74
                "app_icon" => null,
75
                "hero_image" => null,
76
                "badge" => null,
77
                "sound" => array(
78
                    "UWP" => null,
79
                    "Apple" => null
80
                )
81
            ),
82
            "title" => null,
83
            "lines" => array(),
84
            "actions" => array()
85
        );
86
    }
87
88
}
89