Completed
Push — master ( e481c7...bcdb1b )
by Marco
35:35 queued 20:00
created

Updater::updateFromResults()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 29
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 13
nc 5
nop 1
dl 0
loc 29
rs 8.439
c 0
b 0
f 0
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 \Doctrine\ORM\EntityManager;
10
use \Psr\Log\LoggerInterface;
11
use \DateTime;
12
use \Exception;
13
14
/**
15
* @package     Comodojo Extender
16
* @author      Marco Giovinazzi <[email protected]>
17
* @license     MIT
18
*
19
* LICENSE:
20
*
21
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27
* THE SOFTWARE.
28
*/
29
30
class Updater {
31
32
    use ConfigurationTrait;
33
    use LoggerTrait;
34
    use EventsTrait;
35
36
    /**
37
    * Class constructor
38
    *
39
    * @param Configuration $configuration
40
    * @param LoggerInterface $logger
41
    * @param TasksTable $tasks
0 ignored issues
show
Bug introduced by
The type Comodojo\Extender\Schedule\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...
42
    * @param EventsManager $events
43
    * @param EntityManager $em
44
    */
45
    public function __construct(
46
        Configuration $configuration,
47
        LoggerInterface $logger,
48
        EventsManager $events
49
    ) {
50
51
        $this->setConfiguration($configuration);
52
        $this->setLogger($logger);
53
        $this->setEvents($events);
54
55
    }
56
57
    public function updateFromResults(array $results = []) {
58
59
        $em = Database::init($this->getConfiguration())->getEntityManager();
60
61
        foreach ($results as $result) {
62
63
            $id = $result->jid;
64
65
            if ( $id === null ) continue;
66
67
            $job = $em->find('Comodojo\Extender\Orm\Entities\Schedule', $id);
68
69
            if ( $job === null ) continue;
70
71
            if ( $job->getFirstrun() === null ) $job->setFirstrun($result->start);
72
73
            $job->setLastrun($result->start);
74
75
            $em->persist($job);
76
77
        }
78
79
        $em->flush();
80
81
        $ncts = $this->getNextCycleTimestamp($em);
82
83
        $em->close();
84
85
        return $ncts;
86
87
    }
88
89
    protected function getNextCycleTimestamp(EntityManager $em) {
90
91
        $items = $em->getRepository('Comodojo\Extender\Orm\Entities\Schedule')->findAll();
92
93
        $date = new DateTime();
94
        $timestamps = [];
95
96
        foreach ($items as $schedule) {
97
98
            $timestamps[] = $schedule->getNextPlannedRun($date)->getTimestamp();
99
100
        }
101
102
        return empty($timestamps) ? 0 : min($timestamps);
103
104
    }
105
106
}
107