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

Edit::execute()   B

Complexity

Conditions 7
Paths 11

Size

Total Lines 54
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 32
nc 11
nop 2
dl 0
loc 54
rs 7.8331
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php namespace Comodojo\Extender\Socket\Commands\Scheduler;
2
3
use \Comodojo\Extender\Schedule\Manager;
4
use \Comodojo\Daemon\Daemon;
5
use \Comodojo\RpcServer\Request\Parameters;
6
use \Comodojo\Extender\Socket\Messages\Task\Request as TaskRequestMessage;
7
use \Comodojo\Extender\Socket\Messages\Scheduler\Schedule as ScheduleMessage;
8
use \Comodojo\Extender\Task\Request as TaskRequest;
9
use \Comodojo\Extender\Orm\Entities\Schedule;
10
use \Cron\CronExpression;
11
use \Comodojo\Exception\RpcException;
12
use \Exception;
13
14
class Edit {
15
16
    public static function execute(Parameters $params, Daemon $daemon) {
17
18
        $schedule_message = $params->get('schedule');
19
        $request_message = $params->get('request');
20
21
        if (
22
            empty($schedule_message['id']) ||
23
            empty($schedule_message['name']) ||
24
            empty($schedule_message['expression'])
25
        ) {
26
            throw new RpcException("Missing schedule name, id or invalid expression", -32600);
27
        }
28
29
        try {
30
31
            $request = TaskRequest::createFromMessage(
32
                TaskRequestMessage::createFromExport($request_message)
33
            );
34
35
        } catch (Exception $e) {
36
            throw new RpcException("Invalid message payload in request", -32600);
37
        }
38
39
        try {
40
41
            $schedule = new Schedule();
42
            $schedule->setId($schedule_message['id']);
43
            $schedule->setName($schedule_message['name']);
44
            $schedule->setExpression(CronExpression::factory($schedule_message['expression']));
45
            $schedule->setDescription($schedule_message['description']);
46
            $schedule->setEnabled($schedule_message['enabled']);
47
            $schedule->setRequest($request);
48
49
        } catch (Exception $e) {
50
            throw new RpcException("Invalid message payload in schedule", -32600);
51
        }
52
53
        $manager = new Manager(
54
            $daemon->getConfiguration(),
0 ignored issues
show
Bug introduced by
The method getConfiguration() does not exist on Comodojo\Daemon\Daemon. It seems like you code against a sub-type of Comodojo\Daemon\Daemon such as Comodojo\Extender\ExtenderDaemon. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

54
            $daemon->/** @scrutinizer ignore-call */ 
55
                     getConfiguration(),
Loading history...
55
            $daemon->getLogger(),
56
            $daemon->getEvents()
57
        );
58
59
        try {
60
            $result = $manager->edit($schedule);
61
        } catch (Exception $e) {
62
            throw new RpcException($e->getMessage(), -32500);
63
        }
64
65
        $refresh = Refresh::execute($params, $daemon);
0 ignored issues
show
Unused Code introduced by
The assignment to $refresh is dead and can be removed.
Loading history...
66
67
        // should method ignore invalid refresh message here?
68
69
        return $result;
70
71
    }
72
73
}
74