QueueManager::push()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 9
nc 2
nop 1
dl 0
loc 14
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of Jitamin.
5
 *
6
 * Copyright (C) Jitamin Team
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Jitamin\Foundation\Queue;
13
14
use Jitamin\Bus\Job\BaseJob;
15
use Jitamin\Foundation\Base;
16
use LogicException;
17
use SimpleQueue\Queue;
18
19
/**
20
 * Class QueueManager.
21
 */
22
class QueueManager extends Base
23
{
24
    /**
25
     * @var Queue
26
     */
27
    protected $queue = null;
28
29
    /**
30
     * Set queue driver.
31
     *
32
     * @param Queue $queue
33
     *
34
     * @return $this
35
     */
36
    public function setQueue(Queue $queue)
37
    {
38
        $this->queue = $queue;
39
40
        return $this;
41
    }
42
43
    /**
44
     * Send a new job to the queue.
45
     *
46
     * @param BaseJob $job
47
     *
48
     * @return $this
49
     */
50
    public function push(BaseJob $job)
51
    {
52
        $jobClassName = get_class($job);
53
54
        if ($this->queue !== null) {
55
            $this->logger->debug(__METHOD__.': Job pushed in queue: '.$jobClassName);
0 ignored issues
show
Documentation introduced by
The property logger does not exist on object<Jitamin\Foundation\Queue\QueueManager>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
56
            $this->queue->push(JobHandler::getInstance($this->container)->serializeJob($job));
57
        } else {
58
            $this->logger->debug(__METHOD__.': Job executed synchronously: '.$jobClassName);
0 ignored issues
show
Documentation introduced by
The property logger does not exist on object<Jitamin\Foundation\Queue\QueueManager>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
59
            call_user_func_array([$job, 'execute'], $job->getJobParams());
60
        }
61
62
        return $this;
63
    }
64
65
    /**
66
     * Wait for new jobs.
67
     *
68
     * @throws LogicException
69
     */
70
    public function listen()
71
    {
72
        if ($this->queue === null) {
73
            throw new LogicException('No queue driver defined!');
74
        }
75
76
        while ($job = $this->queue->pull()) {
77
            JobHandler::getInstance($this->container)->executeJob($job);
78
            $this->queue->completed($job);
79
        }
80
    }
81
}
82