Completed
Pull Request — 2.0 (#75)
by Julien
04:02
created

Observer::listen()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
c 0
b 0
f 0
rs 9.4285
cc 1
eloc 7
nc 1
nop 1
1
<?php
2
/*
3
 * This file is part of the Pomm's Foundation package.
4
 *
5
 * (c) 2014 - 2015 Grégoire HUBERT <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
namespace PommProject\Foundation\Observer;
11
12
use PommProject\Foundation\Exception\NotificationException;
13
use PommProject\Foundation\Client\Client;
14
use PommProject\Foundation\Session\Session;
15
16
/**
17
 * Observer
18
 *
19
 * Observer session client.
20
 * Listen to notifications sent to the server.
21
 *
22
 * @package   Foundation
23
 * @copyright 2014 - 2015 Grégoire HUBERT
24
 * @author    Grégoire HUBERT
25
 * @license   X11 {@link http://opensource.org/licenses/mit-license.php}
26
 * @see       Client
27
 */
28
class Observer extends Client
29
{
30
    protected $channel;
31
32
    /**
33
     * __construct
34
     *
35
     * Constructor
36
     *
37
     * @access public
38
     * @param  string $channel
39
     */
40
    public function __construct($channel)
41
    {
42
        $this->channel = $channel;
43
    }
44
45
    /**
46
     * getClientType
47
     *
48
     * @see Client
49
     */
50
    public function getClientType()
51
    {
52
        return 'observer';
53
    }
54
55
    /**
56
     * getClientIdentifier
57
     *
58
     * @see Client
59
     */
60
    public function getClientIdentifier()
61
    {
62
        return $this->channel;
63
    }
64
65
    /**
66
     * initialize
67
     *
68
     * @see Client
69
     */
70
    public function initialize(Session $session)
71
    {
72
        parent::initialize($session);
73
        $this->restartListening();
74
    }
75
76
    /**
77
     * shutdown
78
     *
79
     * @see Client
80
     */
81
    public function shutdown()
82
    {
83
        $this->unlisten($this->channel);
84
    }
85
86
    /**
87
     * getNotification
88
     *
89
     * Check if a notification is pending. If so, the payload is returned.
90
     * Otherwise, null is returned.
91
     *
92
     * @access public
93
     * @return array
94
     */
95
    public function getNotification()
96
    {
97
        return $this
98
            ->getSession()
99
            ->getConnection()
100
            ->getNotification()
101
            ;
102
    }
103
104
    /**
105
     * restartListening
106
     *
107
     * Send a LISTEN command to the backend. This is called in the initialize()
108
     * method but it can be unlisten if the listen command took place in a
109
     * transaction.
110
     *
111
     * @access public
112
     * @return Observer $this
113
     */
114
    public function restartListening()
115
    {
116
        return $this->listen($this->channel);
117
    }
118
119
    /**
120
     * listen
121
     *
122
     * Start to listen on the given channel. The observer automatically starts
123
     * listening when registered against the session.
124
     * NOTE: When listen is issued in a transaction it is unlisten when the
125
     * transaction is committed or rollback.
126
     *
127
     * @access protected
128
     * @param  string    $channel
129
     * @return Observer $this
130
     */
131
    protected function listen($channel)
132
    {
133
        $this
134
            ->executeAnonymousQuery(
135
                sprintf(
136
                    "listen %s",
137
                    $this->escapeIdentifier($channel)
138
                )
139
            );
140
141
        return $this;
142
    }
143
144
    /**
145
     * unlisten
146
     *
147
     * Stop listening to events.
148
     *
149
     * @access protected
150
     * @param  string   $channel
151
     * @return Observer $this
152
     *
153
     */
154
    protected function unlisten($channel)
155
    {
156
        $this->executeAnonymousQuery(
157
            sprintf(
158
                "unlisten %s",
159
                $this->escapeIdentifier($channel)
160
            )
161
        );
162
163
        return $this;
164
    }
165
166
    /**
167
     * throwNotification
168
     *
169
     * Check if a notification is pending. If so, a NotificationException is thrown.
170
     *
171
     * @access public
172
     * @throws  NotificationException
173
     * @return Observer $this
174
     */
175
    public function throwNotification()
176
    {
177
        $notification = $this->getNotification();
178
179
        if ($notification !== null) {
180
            throw new NotificationException($notification);
181
        }
182
183
        return $this;
184
    }
185
186
    /**
187
     * executeAnonymousQuery
188
     *
189
     * Proxy for Connection::executeAnonymousQuery()
190
     *
191
     * @access protected
192
     * @param  string   $sql
193
     * @return Observer $this
194
     * @see Connection
195
     */
196
    protected function executeAnonymousQuery($sql)
197
    {
198
        $this
199
            ->getSession()
200
            ->getConnection()
201
            ->executeAnonymousQuery($sql)
202
            ;
203
204
        return $this;
205
    }
206
207
    /**
208
     * escapeIdentifier
209
     *
210
     * Proxy for Connection::escapeIdentifier()
211
     *
212
     * @access protected
213
     * @param  string $string
214
     * @return string
215
     * @see Connection
216
     */
217
    protected function escapeIdentifier($string)
218
    {
219
        return $this
220
            ->getSession()
221
            ->getConnection()
222
            ->escapeIdentifier($string)
223
            ;
224
    }
225
}
226