Completed
Push — 2.0 ( a11c65...7cb9cd )
by Marco
11:37
created

Manager::updateFromResults()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 25
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 25
rs 8.439
cc 5
eloc 11
nc 5
nop 1
1
<?php namespace Comodojo\Extender\Schedule;
2
3
use \Comodojo\Foundation\Base\Configuration;
4
use \Comodojo\Foundation\Events\Manager as EventsManager;
5
use \Comodojo\Foundation\Logging\LoggerTrait;
6
use \Comodojo\Foundation\Events\EventsTrait;
7
use \Comodojo\Extender\Components\Database;
8
use \Comodojo\Foundation\Base\ConfigurationTrait;
9
use \Comodojo\Extender\Traits\EntityManagerTrait;
10
use \Comodojo\Extender\Orm\Entities\Schedule;
11
use \Doctrine\ORM\EntityManager;
12
use \Psr\Log\LoggerInterface;
13
use \DateTime;
14
use \Exception;
15
16
/**
17
* @package     Comodojo Extender
18
* @author      Marco Giovinazzi <[email protected]>
19
* @license     MIT
20
*
21
* LICENSE:
22
*
23
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
29
* THE SOFTWARE.
30
 */
31
32
class Manager {
33
34
    use ConfigurationTrait;
35
    use LoggerTrait;
36
    use EventsTrait;
37
    use EntityManagerTrait;
38
39
    /**
40
     * Class constructor
41
     *
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
    }
63
64
    public function get($id) {
65
66
        $em = $this->getEntityManager();
67
68
        $data = $em->find('Comodojo\Extender\Orm\Entities\Schedule', $id);
69
70
        return $data;
71
72
    }
73
74
    public function getByName($name) {
75
76
        $em = $this->getEntityManager();
77
78
        $data = $em->getRepository('Comodojo\Extender\Orm\Entities\Schedule')->findBy(["name" => $name]);
79
80
        if ( empty($data) ) return null;
81
82
        return $data[0];
83
84
    }
85
86
    public function getAll($ready = false) {
87
88
        $logger = $this->getLogger();
89
90
        $time = new DateTime();
91
92
        $em = $this->getEntityManager();
93
94
        $standby = $em->getRepository('Comodojo\Extender\Orm\Entities\Schedule')->findAll();
95
96
        return $ready ? array_filter($standby, function($job) use ($time, $logger) {
97
98
            $name = $job->getName();
99
            $id = $job->getId();
100
            $enabled = $job->getEnabled();
101
            $firstrun = $job->getFirstrun();
102
            $lastrun = $job->getLastrun();
103
            $expression = $job->getExpression();
104
105
            if ( $lastrun !== null ) {
106
                $nextrun = $expression->getNextRunDate($lastrun);
107
            } else {
108
                $nextrun = $firstrun;
109
            }
110
111
            $shouldrun = $nextrun <= $time;
112
113
            $logger->debug("Job $name (id $id) will ".($shouldrun ? "" : "NOT ")."be executed", [
114
                'ENABLED' => $enabled,
115
                'NEXTRUN' => $nextrun->format('r'),
116
                'SHOULDRUN' => $shouldrun
117
            ]);
118
119
            return $shouldrun;
120
121
        }) : $standby;
122
123
    }
124
125
    public function add(Schedule $schedule) {
126
127
        $time = new DateTime();
128
129
        $schedule->setFirstrun($schedule->getNextPlannedRun($time));
130
131
        $em = $this->getEntityManager();
132
133
        $em->persist($schedule);
134
135
        $em->flush();
136
137
        return $schedule->getId();
138
139
    }
140
141
    public function addBulk(array $schedules) {
142
143
        $time = new DateTime();
144
        $records = [];
145
        $em = $this->getEntityManager();
146
147
        foreach ($schedules as $key => $schedule) {
148
149
            try {
150
151
                $schedule->setFirstrun($schedule->getNextPlannedRun($time));
152
153
                $em->persist($schedule);
154
155
                $em->flush();
156
157
            } catch (Exception $e) {
158
159
                $records[$key] = false;
160
161
                continue;
162
163
            }
164
165
            $records[$key] = $schedule->getId();
166
167
        }
168
169
        return $records;
170
171
    }
172
173
    public function edit(Schedule $schedule) {
174
175
        $em = $this->getEntityManager();
176
177
        $id = $schedule->getId();
178
179
        if ( empty($id) ) throw new Exception("Cannot edit scheule without id");
180
181
        $old_schedule = $this->get($schedule->getId());
182
183
        if ( empty($old_schedule) ) throw new Exception("Cannot find schedule with id $id");
184
185
        $old_schedule->merge($schedule);
186
187
        $em->persist($old_schedule);
188
189
        $em->flush();
190
191
        return true;
192
193
    }
194
195 View Code Duplication
    public function remove($name) {
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...
196
197
        $em = $this->getEntityManager();
198
199
        $schedule = $this->getByName($name);
200
201
        if ( is_null($schedule) ) throw new Exception("Cannot find scheule $name");
202
203
        $em->remove($schedule);
204
205
        $em->flush();
206
207
        return true;
208
209
    }
210
211 View Code Duplication
    public function enable($name) {
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...
212
213
        $em = $this->getEntityManager();
214
215
        $schedule = $this->getByName($name);
216
217
        if ( is_null($schedule) ) throw new Exception("Cannot find scheule $name");
218
219
        $schedule->setEnabled(true);
220
221
        $em->persist($schedule);
222
223
        $em->flush();
224
225
        return true;
226
227
    }
228
229 View Code Duplication
    public function disable($name) {
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...
230
231
        $em = $this->getEntityManager();
232
233
        $schedule = $this->getByName($name);
234
235
        if ( is_null($schedule) ) throw new Exception("Cannot find scheule $name");
236
237
        $schedule->setEnabled(false);
238
239
        $em->persist($schedule);
240
241
        $em->flush();
242
243
        return true;
244
245
    }
246
247
}
248