Completed
Pull Request — master (#16)
by Jacek
08:07 queued 05:58
created

Worker::getName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 2
cts 2
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 0
crap 1
1
<?php
2
3
/**
4
 * Copyright (c) 2016 Jacek Kobus <[email protected]>
5
 * See the file LICENSE.md for copying permission.
6
 */
7
8
namespace PHPExtra\EventManager\Worker;
9
10
use PHPExtra\EventManager\Event\Event;
11
use PHPExtra\EventManager\Listener\Listener;
12
use PHPExtra\EventManager\Priority;
13
14
/**
15
 * The Worker class
16
 *
17
 * @author Jacek Kobus <[email protected]>
18
 */
19
class Worker
20
{
21
    /**
22
     * @var string
23
     */
24
    private $id;
25
26
    /**
27
     * @var Listener
28
     */
29
    private $listener;
30
31
    /**
32
     * @var string
33
     */
34
    private $eventClass;
35
36
    /**
37
     * @var int
38
     */
39
    private $priority;
40
41
    /**
42
     * @var string
43
     */
44
    private $methodName;
45
46
    /**
47
     * Create new worker that will wake-up listener using event
48
     * If priority is null the default (normal) will be used
49
     *
50
     * @param string   $id
51
     * @param Listener $listener
52
     * @param string   $methodName
53
     * @param string   $eventClass
54
     * @param int      $priority
55
     */
56
    public function __construct($id, Listener $listener, $methodName, $eventClass, $priority = Priority::NORMAL)
57 14
    {
58
        $this->id = $id;
59 14
        $this->listener = $listener;
60 2
        $this->methodName = $methodName;
61 2
        $this->eventClass = $eventClass;
62
        $this->priority = $priority;
63 14
    }
64 14
65 14
    /**
66 14
     * @param Event $event
67 14
     *
68 14
     * @return WorkerResult
69
     */
70
    public function run(Event $event)
71
    {
72
        try {
73 9
            call_user_func(array($this->getListener(), $this->getMethod()), $event);
74
            $result = new WorkerResult($this, $event);
75
        } catch (\Exception $e) {
76 9
            $result = new WorkerResult($this, $event, $e);
77 7
        }
78 9
79 2
        return $result;
80
    }
81
82 9
    /**
83
     * @return string
84
     */
85
    public function getId()
86
    {
87
        return $this->id;
88 9
    }
89
90 9
    /**
91
     * Get full worker name including class, method and priority.
92
     */
93
    public function getName()
94
    {
95
        return vsprintf('%s::%s()[P:%s]', array(
96 9
            $this->getListenerClass(),
97
            $this->getMethodName(),
98 9
            $this->getPriority()
99
        ));
100
    }
101
102
    /**
103
     * @return Listener
104 3
     */
105
    public function getListener()
106 3
    {
107
        return $this->listener;
108
    }
109
110
    /**
111
     * @return string
112 9
     */
113
    public function getListenerClass()
114 9
    {
115
        return get_class($this->getListener());
116
    }
117
118
    /**
119
     * @return string
120 10
     */
121
    public function getMethod()
122 10
    {
123
        return $this->getMethodName();
124
    }
125
126
    /**
127
     * @return string
128 12
     */
129
    public function getMethodName()
130 12
    {
131
        return $this->methodName;
132
    }
133
134
    /**
135
     * @return string
136 12
     */
137
    public function getEventClass()
138 12
    {
139
        return $this->eventClass;
140
    }
141
142
    /**
143
     * @return int
144
     */
145
    public function getPriority()
146
    {
147
        return $this->priority;
148
    }
149
150
    /**
151
     * @param $priority
152
     *
153
     * @return $this
154 9
     */
155
    public function setPriority($priority)
156 9
    {
157
        $this->priority = $priority;
158
159
        return $this;
160
    }
161
162
    /**
163
     * {@inheritdoc}
164
     */
165
    public function __toString()
166
    {
167
        return (string)$this->getName();
168
    }
169
}