Manager::doAddRequest()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 2
dl 0
loc 10
rs 10
c 0
b 0
f 0
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
     * If true, $em will be destroyed at shutdown
42
     *
43
     * @var bool
44
     */
45
    private $destroy_em = true;
46
47
    /**
48
     * Class constructor
49
     *
50
     * @param Configuration $configuration
51
     * @param LoggerInterface $logger
52
     * @param EventsManager $events
53
     * @param EntityManager $em
54
     */
55
    public function __construct(
56
        Configuration $configuration,
57
        LoggerInterface $logger,
58
        EventsManager $events,
59
        EntityManager $em = null
60
    ) {
61
62
        $this->setConfiguration($configuration);
63
        $this->setLogger($logger);
64
        $this->setEvents($events);
65
66
        if ( is_null($em) ) {
67
            $this->setEntityManager(
68
                Database::init($configuration)->getEntityManager()
69
            );
70
        } else {
71
            $this->setEntityManager($em);
72
            $this->destroy_em = false;
73
        }
74
75
        // $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...
76
        //     '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...
77
        //     '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...
78
        //     '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...
79
        //     '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...
80
        // ));
0 ignored issues
show
Unused Code Comprehensibility introduced by
75% 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...
81
82
    }
83
84
    public function __destruct() {
85
86
        if ($this->destroy_em) {
87
            $em = $this->getEntityManager();
88
            $em->getConnection()->close();
89
            $em->close();
90
        }
91
92
    }
93
94
    public function get() {
95
96
        $em = $this->getEntityManager();
97
98
        return $em->getRepository('Comodojo\Extender\Orm\Entities\Queue')->findAll();
99
100
    }
101
102
    public function flush(array $queue) {
103
104
        $this->getEvents()->emit( new QueueEvent('flush', null, $queue) );
105
106
        $em = $this->getEntityManager();
107
108
        foreach ($queue as $record) {
109
            $em->remove($record);
110
        }
111
112
        $em->flush();
113
114
    }
115
116
    public function add(Request $request) {
117
118
        $em = $this->getEntityManager();
119
120
        $uid = $this->doAddRequest($request, $em);
121
122
        $em->flush();
123
124
        return $uid;
125
126
    }
127
128
    public function addBulk(array $queue) {
129
130
        $em = $this->getEntityManager();
131
132
        $records = [];
133
134
        foreach ($queue as $name => $request) {
135
            $records[] = $request instanceof Request ? $this->doAddRequest($request, $em) : false;
136
        }
137
138
        $em->flush();
139
140
        return $records;
141
142
    }
143
144
    protected function doAddRequest(Request $request, EntityManager $em) {
145
146
        $this->getEvents()->emit( new QueueEvent('add', $request) );
147
148
        $record = new Queue();
149
        $record->setName($request->getName())->setRequest($request);
150
151
        $em->persist($record);
152
153
        return $request->getUid();
154
155
    }
156
157
}
158