PheanstalkProxy::tubeExists()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 5
ccs 3
cts 3
cp 1
crap 1
rs 10
1
<?php
2
3
namespace Pyrowman\PheanstalkBundle\Proxy;
4
5
use Pheanstalk\Command\CreateScheduleCommand;
6
use Pheanstalk\Command\GetWorkflowInstancesCommand;
7
use Pheanstalk\Pheanstalk;
8
use Pheanstalk\Structure\Schedule;
9
use Pheanstalk\Structure\TaskInstance;
10
use Pheanstalk\Structure\TimeSchedule;
11
use Pheanstalk\Structure\Tube;
12
use Pheanstalk\Structure\Workflow;
13
use Pheanstalk\Structure\WorkflowInstance;
14
use Pyrowman\PheanstalkBundle\Event\CommandEvent;
15
use Pheanstalk\Connection;
16
use Pheanstalk\PheanstalkInterface;
17
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
18
19
class PheanstalkProxy implements PheanstalkProxyInterface
20
{
21
    /**
22
     * @var EventDispatcherInterface
23
     */
24
    protected $dispatcher;
25
26
    /**
27
     * @var string
28
     */
29
    protected $name;
30
31
    /**
32
     * @var Pheanstalk
33
     */
34
    protected $pheanstalk;
35
36
    /**
37
     * @var $currentClass PheanstalkInterface
0 ignored issues
show
Documentation Bug introduced by
The doc comment $currentClass at position 0 could not be parsed: Unknown type name '$currentClass' at position 0 in $currentClass.
Loading history...
38
     */
39
    private $currentClass;
40
41
    /**
42
     * {@inheritDoc}
43
     */
44 1
    public function setConnection(Connection $connection)
45
    {
46 1
        $this->pheanstalk->setConnection($connection);
47
48 1
        return $this;
49
    }
50
51
    /**
52
     * {@inheritDoc}
53
     */
54 1
    public function getConnection()
55
    {
56 1
        return $this->pheanstalk->getConnection();
57
    }
58
59
    /**
60
     * @return PheanstalkInterface
61
     */
62 1
    public function getCurrentClass(): PheanstalkInterface
63
    {
64 1
        return $this->currentClass ?? $this;
65
    }
66
67
    /**
68
     * @param PheanstalkInterface $currentClass
69
     *
70
     * @return PheanstalkInterface
71
     */
72 1
    public function setCurrentClass(PheanstalkInterface $currentClass): PheanstalkInterface
73
    {
74 1
        $this->currentClass = $currentClass;
75 1
        return $this;
76
    }
77
78
    /**
79
     * {@inheritDoc}
80
     */
81 1
    public function delete(Workflow $workflow)
82
    {
83 1
        $this->dispatch(new CommandEvent($this, ['workflow' => $workflow])/*, CommandEvent::DELETE*/);
84
85 1
        return $this->pheanstalk->delete($workflow);
86
    }
87
88
    /**
89
     * {@inheritDoc}
90
     */
91 1
    public function workflowExists($name)
92
    {
93 1
        $this->dispatch(new CommandEvent($this, ['name' => $name])/*, CommandEvent::WORKFLOW_EXISTS*/);
94
95 1
        return $this->pheanstalk->workflowExists($name);
96
    }
97
98
    /**
99
     * {@inheritDoc}
100
     */
101 1
    public function getWorkflow(Workflow $workflow)
102
    {
103 1
        $this->dispatch(new CommandEvent($this, ['workflow' => $workflow])/*, CommandEvent::TASK_EXISTS*/);
104
105 1
        return $this->pheanstalk->getWorkflow($workflow);
106
    }
107
108
    /**
109
     * {@inheritDoc}
110
     */
111 1
    public function getWorkflowInstances(?Workflow $workflow = null, ?string $status = null)
112
    {
113 1
        $this->dispatch(new CommandEvent($this, [
114 1
            'workflow'  => $workflow,
115 1
            'status'    => $status
116
        ])/*, CommandEvent::WORKFLOW_INSTANCES*/);
117
118
119 1
        return $this->pheanstalk->getWorkflowInstances($workflow, $status);
120
    }
121
122
    /**
123
     * {@inheritDoc}
124
     */
125 1
    public function getWorkflowInstancesDetails(WorkflowInstance $workflowInstance)
126
    {
127 1
        $this->dispatch(new CommandEvent($this, ['workflowInstance'  => $workflowInstance])/*,
128
            CommandEvent::WORKFLOW_INSTANCES_DETAILS*/);
129
130 1
        return $this->pheanstalk->getWorkflowInstancesDetails($workflowInstance);
131
    }
132
133
    /**
134
     * {@inheritDoc}
135
     */
136 1
    public function tubeExists($name)
137
    {
138 1
        $this->dispatch(new CommandEvent($this, ['name' => $name])/*, CommandEvent::TUBE_EXISTS*/);
139
140 1
        return $this->pheanstalk->tubeExists($name);
141
    }
142
143
    /**
144
     * {@inheritDoc}
145
     */
146 1
    public function listTubes()
147
    {
148 1
        $this->dispatch(new CommandEvent($this)/*, CommandEvent::LIST_TUBES*/);
149
150 1
        return $this->pheanstalk->listTubes();
151
    }
152
153
    /**
154
     * {@inheritDoc}
155
     */
156 1
    public function peek()
157
    {
158 1
        $this->dispatch(new CommandEvent($this)/*, CommandEvent::PEEK*/);
159
160 1
        return $this->pheanstalk->peek();
161
    }
162
163
    /**
164
     * {@inheritDoc}
165
     */
166 1
    public function put(Workflow $workflow)
167
    {
168 1
        $this->dispatch(new CommandEvent($this, ['workflow' => $workflow])/*, CommandEvent::PUT*/);
169
170 1
        return $this->pheanstalk->put($workflow);
171
    }
172
173
    /**
174
     * {@inheritDoc}
175
     */
176 1
    public function statsTube(Tube $tube)
177
    {
178 1
        $this->dispatch(new CommandEvent($this, ['tube' => $tube])/*, CommandEvent::STATS_TUBE*/);
179
180 1
        return $this->pheanstalk->statsTube($tube);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->pheanstalk->statsTube($tube) returns the type array which is incompatible with the return type mandated by Pheanstalk\PheanstalkInterface::statsTube() of object.

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
181
    }
182
183
    /**
184
     * {@inheritDoc}
185
     */
186 2
    public function stats()
187
    {
188 2
        $this->dispatch(new CommandEvent($this)/*, CommandEvent::STATS*/);
189
190 2
        return $this->pheanstalk->stats();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->pheanstalk->stats() returns the type array which is incompatible with the return type mandated by Pheanstalk\PheanstalkInterface::stats() of object.

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
191
    }
192
193
    /**
194
     * @return EventDispatcherInterface
195
     */
196 1
    public function getDispatcher()
197
    {
198 1
        return $this->dispatcher;
199
    }
200
201
    /**
202
     * @param EventDispatcherInterface $dispatch
203
     */
204 28
    public function setDispatcher(EventDispatcherInterface $dispatch)
205
    {
206 28
        $this->dispatcher = $dispatch;
207
    }
208
209
    /**
210
     * {@inheritDoc}
211
     */
212 1
    public function getPheanstalk()
213
    {
214 1
        return $this->pheanstalk;
215
    }
216
217
    /**
218
     * {@inheritDoc}
219
     */
220 29
    public function setPheanstalk(PheanstalkInterface $pheanstalk)
221
    {
222 29
        $this->pheanstalk = $pheanstalk;
0 ignored issues
show
Documentation Bug introduced by
$pheanstalk is of type Pheanstalk\PheanstalkInterface, but the property $pheanstalk was declared to be of type Pheanstalk\Pheanstalk. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
223 29
        $this->pheanstalk->setCurrentClass($this);
224
225 29
        return $this;
226
    }
227
228
    /**
229
     * {@inheritDoc}
230
     */
231 1
    public function getName()
232
    {
233 1
        return $this->name;
234
    }
235
236
    /**
237
     * {@inheritDoc}
238
     */
239 2
    public function setName($name)
240
    {
241 2
        $this->name = $name;
242
243 2
        return $this;
244
    }
245
246
    /**
247
     * {@inheritdoc}
248
     */
249 2
    public function create(Workflow $workflow, $force = false): Workflow
250
    {
251 2
        $this->dispatch(new CommandEvent($this, ['workflow' => $workflow])/*, CommandEvent::CREATE_WORKFLOW*/);
252
253 2
        $workflow = $this->pheanstalk->create($workflow);
254 2
        return $workflow;
255
    }
256
257
    /**
258
     * {@inheritdoc}
259
     */
260 1
    public function update(Workflow $workflow): Workflow
261
    {
262 1
        $this->dispatch(new CommandEvent($this, ['workflow' => $workflow])/*, CommandEvent::UPDATE_WORKFLOW*/);
263
264 1
        return $this->pheanstalk->update($workflow);
265
    }
266
267
    /**
268
     * {@inheritdoc}
269
     */
270 1
    public function createSchedule(Schedule $schedule)
271
    {
272 1
        $this->dispatch(new CommandEvent($this, [
273 1
                'schedule'  => $schedule,
274
            ])/*, CommandEvent::CREATE_SCHEDULE*/);
275
276 1
        $workflowSchedule = $this->pheanstalk->createSchedule($schedule);
277 1
        return $workflowSchedule;
278
    }
279
280
    /**
281
     * {@inheritdoc}
282
     */
283 1
    public function deleteSchedule(Schedule $schedule)
284
    {
285 1
            $this->dispatch(new CommandEvent($this, [
286 1
                'schedule'  => $schedule,
287
            ])/*, CommandEvent::DELETE_SCHEDULE*/);
288
289 1
        return $this->pheanstalk->deleteSchedule($schedule);
290
    }
291
292
    /**
293
     * {@inheritdoc}
294
     */
295 1
    public function getSchedule(int $schedule)
296
    {
297 1
        $this->dispatch(new CommandEvent($this, [
298 1
                'schedule'  => $schedule,
299
            ])/*, CommandEvent::GET_SCHEDULE*/);
300
301 1
        return $this->pheanstalk->getSchedule($schedule);
302
    }
303
304
    /**
305
     * {@inheritdoc}
306
     */
307 1
    public function updateSchedule(Schedule $schedule): Schedule
308
    {
309 1
        $this->dispatch(new CommandEvent($this, [
310 1
                'schedule'  => $schedule,
311
            ])/*, CommandEvent::UPDATE_SCHEDULE*/);
312
313 1
        return $this->pheanstalk->updateSchedule($schedule);
314
    }
315
316
    /**
317
     * {@inheritdoc}
318
     */
319 1
    public function listSchedules()
320
    {
321 1
        $this->dispatch(new CommandEvent($this, [])/*, CommandEvent::LIST_SCHEDULE*/);
322
323 1
        return $this->pheanstalk->listSchedules();
324
    }
325
326
    /**
327
     * {@inheritdoc}
328
     */
329 1
    public function createTask(string $name, string $group, string $path, $queue = 'default', $useAgent = false, $user = null, $host = null, $comment = null): Workflow
330
    {
331
        $datas = [
332 1
            'name'      => $name,
333 1
            'group'     => $group,
334 1
            'path'      => $path,
335 1
            'queue'     => $queue,
336 1
            'useAgent'  => $useAgent,
337 1
            'user'      => $user,
338 1
            'host'      => $host,
339 1
            'comment'   => $comment
340
        ];
341 1
        $this->dispatch(new CommandEvent($this, $datas)/*, CommandEvent::CREATE_TASK*/);
342
343
344 1
        return $this->pheanstalk->createTask($name, $group, $path, $queue, $useAgent, $user, $host, $comment);
345
    }
346
347
    /**
348
     * {@inheritdoc}
349
     */
350 1
    public function createTube(Tube $tube): Tube
351
    {
352 1
        $this->dispatch(new CommandEvent($this, ['tube' => $tube])/*, CommandEvent::CREATE_TUBE*/);
353
354 1
        return $this->pheanstalk->createTube($tube);
355
    }
356
357
    /**
358
     * {@inheritdoc}
359
     */
360 1
    public function updateTube(Tube $tube): Tube
361
    {
362 1
        $this->dispatch(new CommandEvent($this, ['tube' => $tube])/*, CommandEvent::UPDATE_TUBE*/);
363
364 1
        return $this->pheanstalk->updateTube($tube);
365
    }
366
367
    /**
368
     * {@inheritdoc}
369
     */
370 1
    public function cancel(WorkflowInstance $workflowInstance)
371
    {
372 1
        $this->dispatch(new CommandEvent($this, ['workflowInstance' => $workflowInstance])/*, CommandEvent::CANCEL*/);
373
374 1
        return $this->pheanstalk->cancel($workflowInstance);
375
    }
376
377 1
    public function kill(WorkflowInstance $workflowInstance, TaskInstance $taskInstance)
378
    {
379 1
        $this->dispatch(new CommandEvent($this, ['workflowInstance' => $workflowInstance, 'taskInstance' => $taskInstance])/*, CommandEvent::CANCEL*/);
380
381 1
        return $this->pheanstalk->kill($workflowInstance, $taskInstance);
382
    }
383
384
    /**
385
     * @param CommandEvent $commandEvent
386
     * @param string|null  $eventName
387
     */
388 25
    protected function dispatch(CommandEvent $commandEvent/*, string $eventName = null*/)
389
    {
390 25
        if ($this->dispatcher) {
391 25
            $this->dispatcher->dispatch($commandEvent/*, $eventName */);
392
        }
393
    }
394
}
395