Completed
Push — 2.0 ( 76e968...4129d9 )
by Marco
13:01
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\Events\Manager as EventsManager;
5
use \Comodojo\Daemon\Traits\LoggerTrait;
6
use \Comodojo\Daemon\Traits\EventsTrait;
7
use \Comodojo\Extender\Traits\ConfigurationTrait;
8
use \Comodojo\Extender\Traits\EntityManagerTrait;
9
use \Comodojo\Extender\Task\Request;
10
use \Comodojo\Extender\Orm\Entities\Queue;
11
use \Doctrine\ORM\EntityManager;
12
use \Psr\Log\LoggerInterface;
13
use \Exception;
14
15
/**
16
* @package     Comodojo Extender
17
* @author      Marco Giovinazzi <[email protected]>
18
* @license     MIT
19
*
20
* LICENSE:
21
*
22
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
28
* THE SOFTWARE.
29
 */
30
31
class Manager {
32
33
    use ConfigurationTrait;
34
    use LoggerTrait;
35
    use EventsTrait;
36
    use EntityManagerTrait;
37
38
    /**
39
     * Class constructor
40
     *
41
     * @param string $manager_name
0 ignored issues
show
Bug introduced by
There is no parameter named $manager_name. 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...
42
     * @param Configuration $configuration
43
     * @param LoggerInterface $logger
44
     * @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...
45
     * @param EventsManager $events
46
     * @param EntityManager $em
47
     */
48 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...
49
        Configuration $configuration,
50
        LoggerInterface $logger,
51
        EventsManager $events,
52
        EntityManager $em = null
53
    ) {
54
55
        $this->setConfiguration($configuration);
56
        $this->setLogger($logger);
57
        $this->setEvents($events);
58
59
        $em = is_null($em) ? Database::init($configuration)->getEntityManager() : $em;
60
        $this->setEntityManager($em);
61
62
        // $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...
63
        //     '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...
64
        //     '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...
65
        //     '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...
66
        //     '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...
67
        // ));
68
69
    }
70
71
    public function get() {
72
73
        $em = $this->getEntityManager();
74
75
        return $em->getRepository('Comodojo\Extender\Orm\Entities\Queue')->findAll();
76
77
    }
78
79
    public function remove(array $queue) {
80
81
        $em = $this->getEntityManager();
82
83
        foreach ($queue as $record) {
84
            $em->remove($record);
85
        }
86
87
        $em->flush();
88
89
    }
90
91
    public function add($name, Request $request) {
92
93
        $em = $this->getEntityManager();
94
95
        $uid = $this->doAddRequest($name, $request, $em);
96
97
        $em->flush();
98
99
        return $uid;
100
101
    }
102
103
    public function addBulk(array $queue) {
104
105
        $em = $this->getEntityManager();
106
107
        $records = [];
108
109
        foreach ($queue as $name => $request) {
110
            $records[] = $request instanceof Request ? $this->doAddRequest($name, $request, $em) : false;
111
        }
112
113
        $em->flush();
114
115
        return $records;
116
117
    }
118
119
    protected function doAddRequest($name, Request $request, EntityManager $em) {
120
121
        $record = new Queue();
122
        $record->setName($name)->setRequest($request);
123
124
        $em->persist($record);
125
126
        return $request->getUid();
127
128
    }
129
130
}
131