main_listener   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 119
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 1
dl 0
loc 119
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getSubscribedEvents() 0 9 1
A __construct() 0 14 1
A get_language() 0 6 1
A load_language_on_setup() 0 9 1
A check_login() 0 8 2
B check_friends() 0 40 4
1
<?php
2
3
namespace florinp\messenger\event;
4
5
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
6
use Symfony\Component\Routing\RequestContext;
7
8
use phpbb\template\template;
9
use florinp\messenger\models\main_model;
10
use florinp\messenger\models\friends_model;
11
use phpbb\user;
12
use phpbb\symfony_request;
13
14
class main_listener implements EventSubscriberInterface
15
{
16
17
    static public function getSubscribedEvents()
0 ignored issues
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
18
    {
19
        return array(
20
            'core.user_setup' => 'load_language_on_setup',
21
            'core.page_footer' => 'get_language',
22
            'core.page_header' => 'check_login',
23
            'core.memberlist_view_profile' => 'check_friends'
24
        );
25
    }
26
27
28
    /* @var \phpbb\template\template */
29
    protected $template;
30
31
    /**
32
     * @var main_model
33
     */
34
    protected $model;
35
36
    /**
37
     * @var friends_model
38
     */
39
    protected $friends_model;
40
41
    /**
42
     * @var user
43
     */
44
    protected $user;
45
46
    /**
47
     * @var symfony_request
48
     */
49
    protected $symfony_request;
50
51
    public function __construct(
52
        template $template,
53
        main_model $model,
54
        friends_model $friends_model,
55
        user $user,
56
        symfony_request $symfony_request
57
    )
58
    {
59
        $this->template = $template;
60
        $this->model = $model;
61
        $this->friends_model = $friends_model;
62
        $this->user = $user;
63
        $this->symfony_request = $symfony_request;
64
    }
65
66
    public function get_language()
67
    {
68
        $this->template->assign_vars(array(
69
            'CHAT_LANGUAGE' => $this->user->lang_name
70
        ));
71
    }
72
73
    public function load_language_on_setup($event)
74
    {
75
        $lang_set_ext = $event['lang_set_ext'];
76
        $lang_set_ext[] = array(
77
            'ext_name' => 'florinp/messenger',
78
            'lang_set' => 'common',
79
        );
80
        $event['lang_set_ext'] = $lang_set_ext;
81
    }
82
83
    public function check_login()
84
    {
85
        $s_enable_messenger = 0;
86
        if (in_array($this->user->data['user_type'], array(USER_NORMAL, USER_FOUNDER))) {
87
            $s_enable_messenger = 1;
88
        }
89
        $this->template->assign_var('S_ENABLE_MESSENGER', $s_enable_messenger);
90
    }
91
92
    public function check_friends($event)
93
    {
94
        $context = new RequestContext();
95
        $context->fromRequest($this->symfony_request);
96
        $baseUrl = generate_board_url(true) . $context->getBaseUrl();
97
98
        $scriptName = $this->symfony_request->getScriptName();
99
        $scriptName = substr($scriptName, -1, 1) == '/' ? '' : utf8_basename($scriptName);
100
101
        if ($scriptName != '') {
102
            $baseUrl = str_replace('/' . $scriptName, '', $baseUrl);
103
        }
104
105
        $user_id = $event['member']['user_id'];
106
        $sender_id = $this->user->data['user_id'];
107
        $request = $this->friends_model->get_request_by_sender_id($sender_id);
108
        $check_friend = $this->friends_model->check_friend(array(
109
            'user_id' => $this->user->data['user_id'],
110
            'friend_id' => $user_id,
111
        ));
112
        $check_request = $this->friends_model->check_request(array(
113
            'user_id' => $user_id,
114
            'sender_id' => $this->user->data['user_id']
115
        ));
116
        $check_request_confirm = $this->friends_model->check_request(array(
117
            'user_id' => $this->user->data['user_id'],
118
            'sender_id' => $user_id
119
        ));
120
        $check_widget = true;
121
        if ($user_id == $this->user->data['user_id']) $check_widget = false;
122
        $this->template->assign_vars(array(
123
            'U_USER_ID' => $user_id,
124
            'U_CHECK_FRIEND' => $check_friend,
125
            'U_CHECK_REQUEST' => $check_request,
126
            'U_CHECK_REQUEST_CONFIRM' => $check_request_confirm,
127
            'U_CHECK_WIDGET' => $check_widget,
128
            'U_REQUEST_ID' => $request['request_id'],
129
            'BASE_URL' => $baseUrl
130
        ));
131
    }
132
}
133