Completed
Push — 2.0 ( 8b36d5...0f73ad )
by Marco
13:39
created

Manager::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 22
Code Lines 10

Duplication

Lines 22
Ratio 100 %

Importance

Changes 0
Metric Value
dl 22
loc 22
rs 9.2
c 0
b 0
f 0
cc 2
eloc 10
nc 2
nop 4
1
<?php namespace Comodojo\Extender\Queue;
2
3
use \Comodojo\Foundation\Base\Configuration;
4
use \Comodojo\Foundation\Base\ConfigurationTrait;
5
use \Comodojo\Foundation\Events\Manager as EventsManager;
6
use \Comodojo\Foundation\Logging\LoggerTrait;
7
use \Comodojo\Foundation\Events\EventsTrait;
8
use \Comodojo\Extender\Components\Database;
9
use \Comodojo\Extender\Traits\EntityManagerTrait;
10
use \Comodojo\Extender\Task\Request;
11
use \Comodojo\Extender\Orm\Entities\Queue;
12
use \Comodojo\Extender\Events\QueueEvent;
13
use \Doctrine\ORM\EntityManager;
14
use \Psr\Log\LoggerInterface;
15
use \Exception;
16
17
/**
18
* @package     Comodojo Extender
19
* @author      Marco Giovinazzi <[email protected]>
20
* @license     MIT
21
*
22
* LICENSE:
23
*
24
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
25
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
27
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
29
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
30
* THE SOFTWARE.
31
 */
32
33
class Manager {
34
35
    use ConfigurationTrait;
36
    use LoggerTrait;
37
    use EventsTrait;
38
    use EntityManagerTrait;
39
40
    /**
41
     * Class constructor
42
     *
43
     * @param Configuration $configuration
44
     * @param LoggerInterface $logger
45
     * @param TasksTable $tasks
0 ignored issues
show
Bug introduced by
There is no parameter named $tasks. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
46
     * @param EventsManager $events
47
     * @param EntityManager $em
48
     */
49 View Code Duplication
    public function __construct(
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
50
        Configuration $configuration,
51
        LoggerInterface $logger,
52
        EventsManager $events,
53
        EntityManager $em = null
54
    ) {
55
56
        $this->setConfiguration($configuration);
57
        $this->setLogger($logger);
58
        $this->setEvents($events);
59
60
        $em = is_null($em) ? Database::init($configuration)->getEntityManager() : $em;
61
        $this->setEntityManager($em);
62
63
        // $logger->debug("Tasks Manager online", array(
0 ignored issues
show
Unused Code Comprehensibility introduced by
70% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
64
        //     'lagger_timeout' => $this->lagger_timeout,
0 ignored issues
show
Unused Code Comprehensibility introduced by
56% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
65
        //     'multithread' => $this->multithread,
0 ignored issues
show
Unused Code Comprehensibility introduced by
56% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
66
        //     'max_runtime' => $this->max_runtime,
0 ignored issues
show
Unused Code Comprehensibility introduced by
56% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
67
        //     'max_childs' => $this->max_childs
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
68
        // ));
69
70
    }
71
72
    public function get() {
73
74
        $em = $this->getEntityManager();
75
76
        return $em->getRepository('Comodojo\Extender\Orm\Entities\Queue')->findAll();
77
78
    }
79
80
    public function flush(array $queue) {
81
82
        $this->getEvents()->emit( new QueueEvent('flush', null, $queue) );
83
84
        $em = $this->getEntityManager();
85
86
        foreach ($queue as $record) {
87
            $em->remove($record);
88
        }
89
90
        $em->flush();
91
92
    }
93
94
    public function add(Request $request) {
95
96
        $em = $this->getEntityManager();
97
98
        $uid = $this->doAddRequest($request, $em);
99
100
        $em->flush();
101
102
        return $uid;
103
104
    }
105
106
    public function addBulk(array $queue) {
107
108
        $em = $this->getEntityManager();
109
110
        $records = [];
111
112
        foreach ($queue as $name => $request) {
113
            $records[] = $request instanceof Request ? $this->doAddRequest($request, $em) : false;
114
        }
115
116
        $em->flush();
117
118
        return $records;
119
120
    }
121
122
    protected function doAddRequest(Request $request, EntityManager $em) {
123
124
        $this->getEvents()->emit( new QueueEvent('add', $request) );
125
126
        $record = new Queue();
127
        $record->setName($request->getName())->setRequest($request);
128
129
        $em->persist($record);
130
131
        return $request->getUid();
132
133
    }
134
135
}
136