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

Updater   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 6

Importance

Changes 0
Metric Value
wmc 9
lcom 0
cbo 6
dl 0
loc 77
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 1
B updateFromResults() 0 31 5
A getNextCycleTimestamp() 0 16 3
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
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...
42
    * @param EventsManager $events
43
    * @param EntityManager $em
0 ignored issues
show
Bug introduced by
There is no parameter named $em. 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...
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