3
* Spring Signage Ltd - http://www.springsignage.com
4
* Copyright (C) 2015 Spring Signage Ltd
11
use Respect\Validation\Validator as v;
12
use Xibo\Exception\InvalidArgumentException;
13
use Xibo\Exception\NotFoundException;
14
use Xibo\Factory\DisplayProfileFactory;
15
use Xibo\Service\LogServiceInterface;
16
use Xibo\Storage\StorageServiceInterface;
20
* @package Xibo\Entity
24
class Command implements \JsonSerializable
30
* description="Command Id"
38
* description="Command Name"
46
* description="Unique Code"
54
* description="Description"
62
* description="User Id"
70
* description="Command String - when child of a Display Profile"
74
public $commandString;
78
* description="Validation String - when child of a Display Profile"
82
public $validationString;
85
* Display Profiles using this command
86
* @var array[DisplayProfile]
88
private $displayProfiles = [];
91
* @var DisplayProfileFactory
93
private $displayProfileFactory;
96
* Command constructor.
97
* @param StorageServiceInterface $store
98
* @param LogServiceInterface $log
100
public function __construct($store, $log)
102
$this->setCommonDependencies($store, $log);
106
* @param DisplayProfileFactory $displayProfileFactory
108
public function setChildObjectDependencies($displayProfileFactory)
110
$this->displayProfileFactory = $displayProfileFactory;
117
public function getId()
119
return $this->commandId;
126
public function getOwnerId()
128
return $this->userId;
133
* @throws InvalidArgumentException
135
public function validate()
137
if (!v::stringType()->notEmpty()->length(1, 254)->validate($this->command))
138
throw new InvalidArgumentException(__('Please enter a command name between 1 and 254 characters'), 'command');
140
if (!v::alpha()->NoWhitespace()->notEmpty()->length(1, 50)->validate($this->code))
141
throw new InvalidArgumentException(__('Please enter a code between 1 and 50 characters containing only alpha characters and no spaces'), 'code');
143
if (!v::stringType()->notEmpty()->length(1, 1000)->validate($this->description))
144
throw new InvalidArgumentException(__('Please enter a description between 1 and 1000 characters'), 'description');
149
* @throws NotFoundException
151
public function load()
153
if ($this->loaded || $this->commandId == null)
156
$this->displayProfiles = $this->displayProfileFactory->getByCommandId($this->commandId);
161
* @param array $options
163
* @throws InvalidArgumentException
165
public function save($options = [])
167
$options = array_merge($options, ['validate' => true]);
169
if ($options['validate'])
172
if ($this->commandId == null)
181
public function delete()
186
// Remove from any display profiles
187
foreach ($this->displayProfiles as $profile) {
188
/* @var \Xibo\Entity\DisplayProfile $profile */
189
$profile->unassignCommand($this);
190
$profile->save(['validate' => false]);
193
$this->getStore()->update('DELETE FROM `command` WHERE `commandId` = :commandId', ['commandId' => $this->commandId]);
196
private function add()
198
$this->commandId = $this->getStore()->insert('INSERT INTO `command` (`command`, `code`, `description`, `userId`) VALUES (:command, :code, :description, :userId)', [
199
'command' => $this->command,
200
'code' => $this->code,
201
'description' => $this->description,
202
'userId' => $this->userId
206
private function edit()
208
$this->getStore()->update('
210
`command` = :command,
212
`description` = :description,
214
WHERE `commandId` = :commandId
216
'command' => $this->command,
217
'code' => $this->code,
218
'description' => $this->description,
219
'userId' => $this->userId,
220
'commandId' => $this->commandId
b'\\ No newline at end of file'