/
CreateSchedule.php
157 lines (138 loc) · 3.37 KB
/
CreateSchedule.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
<?php
/**
* Phue: Philips Hue PHP Client
*
* @author Michael Squires <sqmk@php.net>
* @copyright Copyright (c) 2012 Michael K. Squires
* @license http://github.com/sqmk/Phue/wiki/License
*/
namespace Phue\Command;
use Phue\Client;
use Phue\Transport\TransportInterface;
/**
* Create schedule command
*/
class CreateSchedule implements CommandInterface
{
/**
* Schedule attributes
*
* @var array
*/
protected $attributes = [];
/**
* Command
*
* @var SchedulableInterface
*/
protected $command;
/**
* Constructs a create schedule command
*
* @param string $name Name of schedule
* @param string $time Time to run command
* @param SchedulableInterface $command Schedulable command
*/
public function __construct(
$name = null,
$time = null,
SchedulableInterface $command = null
) {
// Set name, time, command if passed
$name !== null && $this->name($name);
$time !== null && $this->time($time);
$command !== null && $this->command($command);
// Copy description
$this->description = $name;
}
/**
* Set name
*
* @param string $name Name
*
* @return CreateSchedule Self object
*/
public function name($name)
{
$this->attributes['name'] = (string) $name;
return $this;
}
/**
* Set description
*
* @param string $description Description
*
* @return CreateSchedule Self object
*/
public function description($description)
{
$this->attributes['description'] = (string) $description;
return $this;
}
/**
* Set time
*
* @param string $time Time
*
* @return CreateSchedule Self object
*/
public function time($time)
{
$this->attributes['time'] = $this->convertTimeToUtcDate(
(string) $time
);
return $this;
}
/**
* Set command
*
* @param SchedulableInterface $command Schedulable command
*
* @return CreateSchedule Self object
*/
public function command(SchedulableInterface $command)
{
$this->command = $command;
return $this;
}
/**
* Send command
*
* @param Client $client Phue Client
*
* @return int Group Id
*/
public function send(Client $client)
{
// Set command attribute if passed
if ($this->command) {
$this->attributes['command'] = $this->command->getSchedulableParams($client);
}
// Create schedule
$scheduleId = $client->getTransport()->sendRequest(
"{$client->getUsername()}/schedules",
TransportInterface::METHOD_POST,
(object) $this->attributes
);
return $scheduleId;
}
/**
* Convert time to UTC date
*
* @param string $time Time to convert
*
* @return string
*/
public function convertTimeToUtcDate($time)
{
try {
$setTime = new \DateTime($time);
$setTime->setTimeZone(
new \DateTimeZone('UTC')
);
} catch (\Exception $e) {
throw new \InvalidArgumentException('time value could not be parsed');
}
return $setTime->format('Y-m-d\TH:i:s');
}
}