Completed
Push — add-pleio-mod ( 6d68a5 )
by
unknown
28:07
created

AccessRequest::getIconURL()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
ccs 0
cts 3
cp 0
crap 2
1
<?php
2
namespace ModPleio;
3
4
class AccessRequest {
5
    public function __construct($data) {
6
        $this->id = (int) $data->id;
0 ignored issues
show
Bug introduced by
The property id does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
7
        $this->user = unserialize($data->user);
0 ignored issues
show
Bug introduced by
The property user does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
8
        $this->time_created = $data->time_created;
0 ignored issues
show
Bug introduced by
The property time_created does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
9
    }
10
11
    public function getURL() {
12
        return $this->user["url"];
13
    }
14
15
    public function getIconURL() {
16
        return $this->user["icon"];
17
    }
18
19
    public function getType() {
20
        return "accessRequest";
21
    }
22
23
    public function approve() {
24
        $resourceOwner = new ResourceOwner($this->user);
25
        $loginHandler = new LoginHandler($resourceOwner);
26
        $site = elgg_get_site_entity();
27
28
        try {
29
            $user = $loginHandler->createUser();
30
            if ($user) {
31
                $this->remove();
32
                $this->sendEmail(
33
                    elgg_echo("pleio:approved:subject", [$site->name]),
34
                    elgg_echo("pleio:approved:body", [
35
                        $user->name,
36
                        $site->name,
37
                        $site->url
38
                    ])
39
                );
40
                return true;
41
            }
42
        } catch (\RegistrationException $e) {
43
            register_error($e->getMessage());
44
        }
45
46
        return false;
47
    }
48
49
    public function decline() {
50
        $site = elgg_get_site_entity();
51
        $resourceOwner = new ResourceOwner($this->user);
52
53
        if ($this->remove()) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->remove() of type integer|false is loosely compared to true; this is ambiguous if the integer can be zero. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
54
            $this->sendEmail(
55
                elgg_echo("pleio:declined:subject", [$site->name]),
56
                elgg_echo("pleio:declined:body", [
57
                    $resourceOwner->getName(),
58
                    $site->name
59
                ])
60
            );
61
62
            return true;
63
        }
64
65
        return false;
66
    }
67
68
    public function remove() {
69
        return delete_data("DELETE FROM pleio_request_access WHERE id = {$this->id}");
70
    }
71
72
    private function sendEmail($subject, $body, $params = null) {
73
        $site = elgg_get_site_entity();
74
75
        if ($site->email) {
76
            $from = $site->email;
77
        } else {
78
            $from = "noreply@" . get_site_domain($site->guid);
79
        }
80
81
        return elgg_send_email($from, $this->user["email"], $subject, $body, $params);
82
    }
83
}