Completed
Push — master ( 806b0a...9ceca3 )
by Marco
09:54
created

Manager::__destruct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 0
dl 0
loc 6
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 TasksTable $tasks
0 ignored issues
show
Bug introduced by
The type Comodojo\Extender\Queue\TasksTable was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
53
     * @param EventsManager $events
54
     * @param EntityManager $em
55
     */
56
    public function __construct(
57
        Configuration $configuration,
58
        LoggerInterface $logger,
59
        EventsManager $events,
60
        EntityManager $em = null
61
    ) {
62
63
        $this->setConfiguration($configuration);
64
        $this->setLogger($logger);
65
        $this->setEvents($events);
66
67
        if ( is_null($em) ) {
68
            $this->setEntityManager(
69
                Database::init($configuration)->getEntityManager()
70
            );
71
        } else {
72
            $this->setEntityManager($em);
73
            $this->destroy_em = false;
74
        }
75
76
        // $logger->debug("Tasks Manager online", array(
77
        //     'lagger_timeout' => $this->lagger_timeout,
78
        //     'multithread' => $this->multithread,
79
        //     'max_runtime' => $this->max_runtime,
80
        //     'max_childs' => $this->max_childs
81
        // ));
82
83
    }
84
85
    public function __destruct() {
86
87
        if ($this->destroy_em) {
88
            $em = $this->getEntityManager();
89
            $em->getConnection()->close();
90
            $em->close();
91
        }
92
93
    }
94
95
    public function get() {
96
97
        $em = $this->getEntityManager();
98
99
        return $em->getRepository('Comodojo\Extender\Orm\Entities\Queue')->findAll();
100
101
    }
102
103
    public function flush(array $queue) {
104
105
        $this->getEvents()->emit( new QueueEvent('flush', null, $queue) );
106
107
        $em = $this->getEntityManager();
108
109
        foreach ($queue as $record) {
110
            $em->remove($record);
111
        }
112
113
        $em->flush();
114
115
    }
116
117
    public function add(Request $request) {
118
119
        $em = $this->getEntityManager();
120
121
        $uid = $this->doAddRequest($request, $em);
122
123
        $em->flush();
124
125
        return $uid;
126
127
    }
128
129
    public function addBulk(array $queue) {
130
131
        $em = $this->getEntityManager();
132
133
        $records = [];
134
135
        foreach ($queue as $name => $request) {
136
            $records[] = $request instanceof Request ? $this->doAddRequest($request, $em) : false;
137
        }
138
139
        $em->flush();
140
141
        return $records;
142
143
    }
144
145
    protected function doAddRequest(Request $request, EntityManager $em) {
146
147
        $this->getEvents()->emit( new QueueEvent('add', $request) );
148
149
        $record = new Queue();
150
        $record->setName($request->getName())->setRequest($request);
151
152
        $em->persist($record);
153
154
        return $request->getUid();
155
156
    }
157
158
}
159