Skip to content

Commit

Permalink
Check method parameters and server group existing
Browse files Browse the repository at this point in the history
  • Loading branch information
muxx committed Feb 7, 2017
1 parent 2eb2b26 commit e523a58
Showing 1 changed file with 39 additions and 3 deletions.
42 changes: 39 additions & 3 deletions src/Dplr/Dplr.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ public function getServers()
*/
public function getServersByGroup($group)
{
$servers = array();
$servers = [];
foreach ($this->servers as $serverName => $groups) {
if (in_array($group, $groups)) {
$servers[] = $serverName;
Expand Down Expand Up @@ -156,12 +156,28 @@ public function newThread()
*/
public function command($command, $serverGroup = null, $timeout = null)
{
if (!is_string($command)) {
throw new \InvalidArgumentException('Command must be string');
}

if ($serverGroup && !is_string($serverGroup)) {
throw new \InvalidArgumentException('Server group must be string');
}

$servers = null;
if ($serverGroup) {
$servers = $this->getServersByGroup($serverGroup);
if (!count($servers)) {
throw new \InvalidArgumentException(sprintf('Not found servers for group "%s"', $serverGroup));
}
}

$this->checkState();

$data = [
'Action' => 'ssh',
'Cmd' => $command,
'Hosts' => $serverGroup ? $this->getServersByGroup($serverGroup) : $this->getServers(),
'Hosts' => $serverGroup ? $servers : $this->getServers(),
'Timeout' => ( (int) $timeout > 0 ? (int) $timeout : $this->defaultTimeout ) * 1000,
];

Expand All @@ -182,13 +198,33 @@ public function command($command, $serverGroup = null, $timeout = null)
*/
public function upload($localFile, $remoteFile, $serverGroup = null, $timeout = null)
{
if (!is_string($localFile)) {
throw new \InvalidArgumentException('Local file must be string');
}

if (!is_string($remoteFile)) {
throw new \InvalidArgumentException('Remote file must be string');
}

if ($serverGroup && !is_string($serverGroup)) {
throw new \InvalidArgumentException('Server group must be string');
}

$servers = null;
if ($serverGroup) {
$servers = $this->getServersByGroup($serverGroup);
if (!count($servers)) {
throw new \InvalidArgumentException(sprintf('Not found servers for group "%s"', $serverGroup));
}
}

$this->checkState();

$data = [
'Action' => 'scp',
'Source' => $localFile,
'Target' => $remoteFile,
'Hosts' => $serverGroup ? $this->getServersByGroup($serverGroup) : $this->getServers(),
'Hosts' => $serverGroup ? $servers : $this->getServers(),
'Timeout' => ( (int) $timeout > 0 ? (int) $timeout : $this->defaultTimeout ) * 1000,
];

Expand Down

0 comments on commit e523a58

Please sign in to comment.