~xibo-maintainers/xibo/tempel

« back to all changes in this revision

Viewing changes to lib/Entity/Command.php

  • Committer: Dan Garner
  • Date: 2015-08-11 09:29:02 UTC
  • mto: This revision was merged to the branch mainline in revision 453.
  • Revision ID: git-v1:a86fb4369b7395c13367577d23b14c0ab4528c1a
Transitions fixes.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
<?php
2
 
/*
3
 
 * Spring Signage Ltd - http://www.springsignage.com
4
 
 * Copyright (C) 2015 Spring Signage Ltd
5
 
 * (Command.php)
6
 
 */
7
 
 
8
 
 
9
 
namespace Xibo\Entity;
10
 
 
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;
17
 
 
18
 
/**
19
 
 * Class Command
20
 
 * @package Xibo\Entity
21
 
 *
22
 
 * @SWG\Definition()
23
 
 */
24
 
class Command implements \JsonSerializable
25
 
{
26
 
    use EntityTrait;
27
 
 
28
 
    /**
29
 
     * @SWG\Property(
30
 
     *  description="Command Id"
31
 
     * )
32
 
     * @var int
33
 
     */
34
 
    public $commandId;
35
 
 
36
 
    /**
37
 
     * @SWG\Property(
38
 
     *  description="Command Name"
39
 
     * )
40
 
     * @var string
41
 
     */
42
 
    public $command;
43
 
 
44
 
    /**
45
 
     * @SWG\Property(
46
 
     *  description="Unique Code"
47
 
     * )
48
 
     * @var string
49
 
     */
50
 
    public $code;
51
 
 
52
 
    /**
53
 
     * @SWG\Property(
54
 
     *  description="Description"
55
 
     * )
56
 
     * @var string
57
 
     */
58
 
    public $description;
59
 
 
60
 
    /**
61
 
     * @SWG\Property(
62
 
     *  description="User Id"
63
 
     * )
64
 
     * @var int
65
 
     */
66
 
    public $userId;
67
 
 
68
 
    /**
69
 
     * @SWG\Property(
70
 
     *  description="Command String - when child of a Display Profile"
71
 
     * )
72
 
     * @var string
73
 
     */
74
 
    public $commandString;
75
 
 
76
 
    /**
77
 
     * @SWG\Property(
78
 
     *  description="Validation String - when child of a Display Profile"
79
 
     * )
80
 
     * @var string
81
 
     */
82
 
    public $validationString;
83
 
 
84
 
    /**
85
 
     * Display Profiles using this command
86
 
     * @var array[DisplayProfile]
87
 
     */
88
 
    private $displayProfiles = [];
89
 
 
90
 
    /**
91
 
     * @var DisplayProfileFactory
92
 
     */
93
 
    private $displayProfileFactory;
94
 
 
95
 
    /**
96
 
     * Command constructor.
97
 
     * @param StorageServiceInterface $store
98
 
     * @param LogServiceInterface $log
99
 
     */
100
 
    public function __construct($store, $log)
101
 
    {
102
 
        $this->setCommonDependencies($store, $log);
103
 
    }
104
 
 
105
 
    /**
106
 
     * @param DisplayProfileFactory $displayProfileFactory
107
 
     */
108
 
    public function setChildObjectDependencies($displayProfileFactory)
109
 
    {
110
 
        $this->displayProfileFactory = $displayProfileFactory;
111
 
    }
112
 
 
113
 
    /**
114
 
     * Get Id
115
 
     * @return int
116
 
     */
117
 
    public function getId()
118
 
    {
119
 
        return $this->commandId;
120
 
    }
121
 
 
122
 
    /**
123
 
     * Get OwnerId
124
 
     * @return int
125
 
     */
126
 
    public function getOwnerId()
127
 
    {
128
 
        return $this->userId;
129
 
    }
130
 
 
131
 
    /**
132
 
     * Validate
133
 
     * @throws InvalidArgumentException
134
 
     */
135
 
    public function validate()
136
 
    {
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');
139
 
 
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');
142
 
 
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');
145
 
    }
146
 
 
147
 
    /**
148
 
     * Load
149
 
     * @throws NotFoundException
150
 
     */
151
 
    public function load()
152
 
    {
153
 
        if ($this->loaded || $this->commandId == null)
154
 
            return;
155
 
 
156
 
        $this->displayProfiles = $this->displayProfileFactory->getByCommandId($this->commandId);
157
 
    }
158
 
 
159
 
    /**
160
 
     * Save
161
 
     * @param array $options
162
 
     *
163
 
     * @throws InvalidArgumentException
164
 
     */
165
 
    public function save($options = [])
166
 
    {
167
 
        $options = array_merge($options, ['validate' => true]);
168
 
 
169
 
        if ($options['validate'])
170
 
            $this->validate();
171
 
 
172
 
        if ($this->commandId == null)
173
 
            $this->add();
174
 
        else
175
 
            $this->edit();
176
 
    }
177
 
 
178
 
    /**
179
 
     * Delete
180
 
     */
181
 
    public function delete()
182
 
    {
183
 
        if (!$this->loaded)
184
 
            $this->load();
185
 
 
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]);
191
 
        }
192
 
 
193
 
        $this->getStore()->update('DELETE FROM `command` WHERE `commandId` = :commandId', ['commandId' => $this->commandId]);
194
 
    }
195
 
 
196
 
    private function add()
197
 
    {
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
203
 
        ]);
204
 
    }
205
 
 
206
 
    private function edit()
207
 
    {
208
 
        $this->getStore()->update('
209
 
            UPDATE `command` SET
210
 
              `command` = :command,
211
 
              `code` = :code,
212
 
              `description` = :description,
213
 
              `userId` = :userId
214
 
             WHERE `commandId` = :commandId
215
 
        ', [
216
 
            'command' => $this->command,
217
 
            'code' => $this->code,
218
 
            'description' => $this->description,
219
 
            'userId' => $this->userId,
220
 
            'commandId' => $this->commandId
221
 
        ]);
222
 
    }
223
 
}
 
 
b'\\ No newline at end of file'