Edit::execute()   B
last analyzed

Complexity

Conditions 8
Paths 12

Size

Total Lines 56
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 34
nc 12
nop 2
dl 0
loc 56
rs 8.1315
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 \InvalidArgumentException;
13
use \Exception;
14
15
class Edit {
16
17
    public static function execute(Parameters $params, Daemon $daemon) {
18
19
        $schedule_message = $params->get('schedule');
20
        $request_message = $params->get('request');
21
22
        if (
23
            empty($schedule_message['id']) ||
24
            empty($schedule_message['name']) ||
25
            empty($schedule_message['expression'])
26
        ) {
27
            throw new RpcException("Missing schedule name, id or invalid expression", -32600);
28
        }
29
30
        try {
31
32
            $request = TaskRequest::createFromMessage(
33
                TaskRequestMessage::createFromExport($request_message)
34
            );
35
36
        } catch (Exception $e) {
37
            throw new RpcException("Invalid message payload in request", -32600);
38
        }
39
40
        try {
41
42
            $schedule = new Schedule();
43
            $schedule->setId($schedule_message['id']);
44
            $schedule->setName($schedule_message['name']);
45
            $schedule->setExpression(CronExpression::factory($schedule_message['expression']));
46
            $schedule->setDescription($schedule_message['description']);
47
            $schedule->setEnabled($schedule_message['enabled']);
48
            $schedule->setRequest($request);
49
50
        } catch (Exception $e) {
51
            throw new RpcException("Invalid message payload in schedule", -32600);
52
        }
53
54
        $manager = new Manager(
55
            $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

55
            $daemon->/** @scrutinizer ignore-call */ 
56
                     getConfiguration(),
Loading history...
56
            $daemon->getLogger(),
57
            $daemon->getEvents()
58
        );
59
60
        try {
61
            $result = $manager->edit($schedule);
62
        } catch (InvalidArgumentException $iae) {
63
            throw new RpcException("No record could be found", -31002);
64
        } catch (Exception $e) {
65
            throw new RpcException($e->getMessage(), -32500);
66
        }
67
68
        $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...
69
70
        // should method ignore invalid refresh message here?
71
72
        return $result;
73
74
    }
75
76
}
77