Event_Observer::update()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
cc 2
eloc 5
c 2
b 1
f 0
nc 2
nop 1
dl 0
loc 11
rs 9.4285
1
<?php defined('SYSPATH') or die('No direct access allowed.');
2
/**
3
 * Kohana event observer. Uses the SPL observer pattern.
4
 *
5
 * $Id: Event_Observer.php 3769 2008-12-15 00:48:56Z zombor $
6
 *
7
 * @package    Core
8
 * @author     Kohana Team
9
 * @copyright  (c) 2007-2008 Kohana Team
10
 * @license    http://kohanaphp.com/license.html
11
 */
12
abstract class Event_Observer implements SplObserver
13
{
14
15
    // Calling object
16
    protected $caller;
17
18
    /**
19
     * Initializes a new observer and attaches the subject as the caller.
20
     *
21
     * @param   object  Event_Subject
22
     * @return  void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
23
     */
24
    public function __construct(SplSubject $caller)
25
    {
26
        // Update the caller
27
        $this->update($caller);
28
    }
29
30
    /**
31
     * Updates the observer subject with a new caller.
32
     *
33
     * @chainable
34
     * @param   object  Event_Subject
35
     * @return  Event_Observer
36
     */
37
    public function update(SplSubject $caller)
38
    {
39
        if (! ($caller instanceof Event_Subject)) {
40
            throw new Kohana_Exception('event.invalid_subject', get_class($caller), get_class($this));
41
        }
42
43
        // Update the caller
44
        $this->caller = $caller;
45
46
        return $this;
47
    }
48
49
    /**
50
     * Detaches this observer from the subject.
51
     *
52
     * @chainable
53
     * @return  Event_Observer
54
     */
55
    public function remove()
56
    {
57
        // Detach this observer from the caller
58
        $this->caller->detach($this);
59
60
        return $this;
61
    }
62
63
    /**
64
     * Notify the observer of a new message. This function must be defined in
65
     * all observers and must take exactly one parameter of any type.
66
     *
67
     * @param   mixed   message string, object, or array
68
     * @return  void
69
     */
70
    abstract public function notify($message);
71
} // End Event Observer
72