eisen-dev/eisen_front

View on GitHub
webd/includes/restclient.php

Summary

Maintainability
F
1 wk
Test Coverage
File `restclient.php` has 473 lines of code (exceeds 250 allowed). Consider refactoring.
A file should declare new symbols (classes, functions, constants, etc.) and cause no other side effects, or it should execute logic with side effects, but should not do both. The first symbol is defined on line 23 and the first side effect is on line 12.
<?php
/**
* Eisen Frontend
* http://eisen-dev.github.io
*
* Copyright (c) 2016 Alice Ferrazzi <alice.ferrazzi@gmail.com> - Takuma Muramatsu <t.muramatu59@gmail.com>
* Dual licensed under the MIT or GPL Version 3 licenses or later.
* http://eisen-dev.github.io/License.md
*
*/
 
require_once __DIR__ . '/../../vendor/autoload.php';
require_once __DIR__ . '/monologLogger.php';
require_once __DIR__ . '/DbAction.php';
 
use Httpful\Exception\ConnectionErrorException;
There must be one blank line after the last USE statement; 2 found;
use Monolog\Logger;
 
 
/**
* Class restclient
*/
The class restclient has 19 public methods. Consider refactoring restclient to keep number of public methods under 10.
The class restclient has an overall complexity of 55 which is very high. The configured complexity threshold is 50.
Each class must be in a namespace of at least one level (a top-level vendor name)
The class restclient is not named in CamelCase.
Class name "restclient" is not in camel caps format
class restclient
{
/**
* @return Logger
*/
public function logger()
{
Missing class import via use statement (line '30', column '20').
$log = new LoggerAtOnce();
$log = $log->loggerInject();
 
return $log;
}
 
/**
* @param $rest_host
* @param $rest_port
* @param $username
* @param $password
*
* @return string
*/
The parameter $rest_host is not named in camelCase.
The parameter $rest_port is not named in camelCase.
The variable $rest_port is not named in camelCase.
The variable $rest_host is not named in camelCase.
public function restconnect($rest_host, $rest_port, $username, $password)
{
try {
$uri = 'http://' . $rest_host . ':' . $rest_port . '/eisen/api/v1.0/agent';
$response = \Httpful\Request::get($uri)
->authenticateWith($username, $password)
->whenError(
function ($error) {
$this->errorHandler($error);
}
)
->send();
$max = count($response);
for ($i = 0; $i < $max; $i++) {
if (!empty($response)) {
return 'online';
}
}
} catch (ConnectionErrorException $ex) {
$this->errorHandler($ex);
}
return 'offline';
}
 
The parameter $rest_port is not named in camelCase.
The parameter $rest_host is not named in camelCase.
Method name "restclient::host_list" is not in camel caps format
The variable $rest_host is not named in camelCase.
The variable $rest_port is not named in camelCase.
The method host_list is not named in camelCase.
public function host_list($rest_host, $rest_port, $username, $password)
{
try {
$uri = 'http://' . $rest_host . ':' . $rest_port . '/eisen/api/v1.0/hosts';
$response = \Httpful\Request::get($uri)
->authenticateWith($username, $password)
->whenError(
function ($error) {
$this->errorHandler($error);
}
)
->send();
$max = count($response->body->host);
for ($i = 0; $i < $max; $i++) {
if (!empty($response->body->host[$i])) {
Avoid using undefined variables such as '$hosts' which will lead to PHP notices.
$hosts[] = ($response->body->host[$i]);
}
}
} catch (ConnectionErrorException $ex) {
//$this->errorHandler($ex);
}
Avoid using undefined variables such as '$hosts' which will lead to PHP notices.
if (isset($hosts)) {
Avoid using undefined variables such as '$hosts' which will lead to PHP notices.
return $hosts;
}
}
 
Similar blocks of code found in 2 locations. Consider refactoring.
The parameter $rest_host is not named in camelCase.
The parameter $rest_port is not named in camelCase.
Method name "restclient::tasks_list" is not in camel caps format
The variable $rest_host is not named in camelCase.
The variable $rest_port is not named in camelCase.
The method tasks_list is not named in camelCase.
public function tasks_list($rest_host, $rest_port, $username, $password)
{
try {
$uri = 'http://' . $rest_host . ':' . $rest_port . '/eisen/api/v1.0/tasks';
$response = \Httpful\Request::get($uri)
->authenticateWith($username, $password)
->whenError(
function ($error) {
$this->errorHandler($error);
}
)
->send();
$tasks = array();
$max = count($response->body->tasks);
for ($i = 0; $i < $max; $i++) {
if (!empty($response->body->tasks[$i])) {
$tasks[] = ($response->body->tasks[$i]);
}
}
} catch (ConnectionErrorException $ex) {
$this->errorHandler($ex);
}
if (isset($tasks)) {
return $tasks;
}
}
 
Similar blocks of code found in 2 locations. Consider refactoring.
The parameter $rest_host is not named in camelCase.
The parameter $rest_port is not named in camelCase.
Method name "restclient::recipe_list" is not in camel caps format
The variable $rest_host is not named in camelCase.
The variable $rest_port is not named in camelCase.
The method recipe_list is not named in camelCase.
public function recipe_list($rest_host, $rest_port, $username, $password)
{
try {
$uri = 'http://' . $rest_host . ':' . $rest_port . '/eisen/api/v1.0/recipes';
$response = \Httpful\Request::get($uri)
->authenticateWith($username, $password)
->whenError(
function ($error) {
$this->errorHandler($error);
}
)
->send();
Avoid unused local variables such as '$tasks'.
$tasks = array();
$max = count($response->body->recipes);
for ($i = 0; $i < $max; $i++) {
if (!empty($response->body->recipes[$i])) {
Avoid using undefined variables such as '$recipes' which will lead to PHP notices.
$recipes[] = ($response->body->recipes[$i]);
}
}
} catch (ConnectionErrorException $ex) {
$this->errorHandler($ex);
}
Avoid using undefined variables such as '$recipes' which will lead to PHP notices.
if (isset($recipes)) {
Avoid using undefined variables such as '$recipes' which will lead to PHP notices.
return $recipes;
}
}
 
Function `variable_list` has a Cognitive Complexity of 7 (exceeds 5 allowed). Consider refactoring.
The parameter $rest_port is not named in camelCase.
The parameter $rest_host is not named in camelCase.
Method name "restclient::variable_list" is not in camel caps format
The variable $rest_port is not named in camelCase.
The variable $host_id is not named in camelCase.
The variable $rest_host is not named in camelCase.
The method variable_list is not named in camelCase.
public function variable_list($rest_host, $rest_port, $username, $password)
{
try {
$host_id=1;
$uri = 'http://' . $rest_host . ':' . $rest_port . '/eisen/api/v1.0/host/'.$host_id.'/vars';
$response = \Httpful\Request::get($uri)
->authenticateWith($username, $password)
->whenError(
function ($error) {
$this->errorHandler($error);
}
)
->send();
$tasks = array();
$max = count($response->body->var);
for ($i = 0; $i < $max; $i++) {
if (!empty($response->body->var[$i])) {
$tasks[] = ($response->body->var[$i]);
Avoid unused local variables such as '$row'.
foreach ($tasks as $x => $row) {
$tasks[$x]->manager_host = $rest_host;
}
}
}
} catch (ConnectionErrorException $ex) {
$this->errorHandler($ex);
}
return $tasks;
}
 
Method `tasks_run` has 5 arguments (exceeds 4 allowed). Consider refactoring.
Similar blocks of code found in 2 locations. Consider refactoring.
The parameter $rest_port is not named in camelCase.
The parameter $rest_host is not named in camelCase.
The parameter $task_id is not named in camelCase.
Method name "restclient::tasks_run" is not in camel caps format
The variable $rest_port is not named in camelCase.
The variable $rest_host is not named in camelCase.
The variable $task_id is not named in camelCase.
The method tasks_run is not named in camelCase.
public function tasks_run($rest_host, $rest_port, $username, $password, $task_id)
{
try {
$uri = 'http://' . $rest_host . ':' . $rest_port . '/eisen/api/v1.0/task/' . $task_id . '/run';
$response = \Httpful\Request::get($uri)
->authenticateWith($username, $password)
->whenError(
function ($error) {
$this->errorHandler($error);
}
)
->send();
$body = array();
$max = count($response->body->task);
//var_dump($response);
for ($i = 0; $i < $max; $i++) {
if (!empty($response->body->task)) {
$body[] = ($response->body->task);
}
}
} catch (ConnectionErrorException $ex) {
$this->errorHandler($ex);
}
return $body;
}
 
Method `recipe_run` has 5 arguments (exceeds 4 allowed). Consider refactoring.
Similar blocks of code found in 2 locations. Consider refactoring.
The parameter $rest_host is not named in camelCase.
The parameter $rest_port is not named in camelCase.
The parameter $task_id is not named in camelCase.
Method name "restclient::recipe_run" is not in camel caps format
The variable $task_id is not named in camelCase.
The variable $rest_host is not named in camelCase.
The variable $rest_port is not named in camelCase.
The method recipe_run is not named in camelCase.
public function recipe_run($rest_host, $rest_port, $username, $password, $task_id)
{
try {
$uri = 'http://' . $rest_host . ':' . $rest_port . '/eisen/api/v1.0/recipe/' . $task_id . '/run';
$response = \Httpful\Request::get($uri)
->authenticateWith($username, $password)
->whenError(
function ($error) {
$this->errorHandler($error);
}
)
->send();
$body = array();
$max = count($response->body->recipe);
//var_dump($response);
for ($i = 0; $i < $max; $i++) {
if (!empty($response->body->recipe)) {
$body[] = ($response->body->recipe);
}
}
} catch (ConnectionErrorException $ex) {
$this->errorHandler($ex);
}
return $body;
}
/**
* @param $rest_host
* @param $rest_port
* @param $username
* @param $password
* @param $task_id
* @return \Httpful\Response|mixed
* @throws \Httpful\Exception\ConnectionErrorException
*/
Method `tasks_result` has 5 arguments (exceeds 4 allowed). Consider refactoring.
Similar blocks of code found in 2 locations. Consider refactoring.
The parameter $rest_port is not named in camelCase.
The parameter $task_id is not named in camelCase.
The parameter $rest_host is not named in camelCase.
Method name "restclient::tasks_result" is not in camel caps format
The variable $rest_host is not named in camelCase.
The variable $task_id is not named in camelCase.
The variable $rest_port is not named in camelCase.
The method tasks_result is not named in camelCase.
public function tasks_result($rest_host, $rest_port, $username, $password, $task_id)
{
try {
$uri = 'http://' . $rest_host . ':' . $rest_port . '/eisen/api/v1.0/task/' . $task_id . '/result';
$response = \Httpful\Request::get($uri)
->authenticateWith($username, $password)
->whenError(
function ($error) {
$this->errorHandler($error);
}
)
->send();
# convert json from stdobject to array
$response = json_decode($response->raw_body, true);
} catch (ConnectionErrorException $ex) {
$this->errorHandler($ex);
}
return $response;
}
 
/**
* @param $rest_host
* @param $rest_port
* @param $username
* @param $password
* @param $task_id
* @return \Httpful\Response|mixed
* @throws \Httpful\Exception\ConnectionErrorException
*/
Method `recipe_result` has 5 arguments (exceeds 4 allowed). Consider refactoring.
Similar blocks of code found in 2 locations. Consider refactoring.
The parameter $rest_host is not named in camelCase.
The parameter $recipe_id is not named in camelCase.
The parameter $rest_port is not named in camelCase.
Method name "restclient::recipe_result" is not in camel caps format
The variable $rest_host is not named in camelCase.
The variable $rest_port is not named in camelCase.
The variable $recipe_id is not named in camelCase.
The method recipe_result is not named in camelCase.
public function recipe_result($rest_host, $rest_port, $username, $password, $recipe_id)
{
try {
$uri = 'http://' . $rest_host . ':' . $rest_port . '/eisen/api/v1.0/recipe/' . $recipe_id . '/result';
$response = \Httpful\Request::get($uri)
->authenticateWith($username, $password)
->whenError(
function ($error) {
$this->errorHandler($error);
}
)
->send();
# convert json from stdobject to array
$response = json_decode($response->raw_body, true);
} catch (ConnectionErrorException $ex) {
$this->errorHandler($ex);
}
return $response;
}
/**
* @param $rest_host
* @param $rest_port
* @param $username
* @param $password
* @param $host
* @param $groups
*/
The parameter $rest_port is not named in camelCase.
The parameter $rest_host is not named in camelCase.
Method name "restclient::host_register" is not in camel caps format
The variable $rest_port is not named in camelCase.
The variable $rest_host is not named in camelCase.
The method host_register is not named in camelCase.
public function host_register(
Method `host_register` has 6 arguments (exceeds 4 allowed). Consider refactoring.
$rest_host,
$rest_port,
$username,
$password,
$host,
$groups
) {
try {
if (empty($groups)) {
$groups = '';
}
$uri = 'http://' . $rest_host . ':' . $rest_port . '/eisen/api/v1.0/hosts';
Avoid unused local variables such as '$response'.
$response = \Httpful\Request::post($uri)
->sendsJson()// tell it we're sending (Content-Type) JSON...
->authenticateWith($username, $password)
->body('{"host":"' . $host . '","groups":"' . $groups . '"}')// attach a body/payload...
->whenError(
function ($error) {
$this->errorHandler($error);
}
)
->sendIt();
} catch (ConnectionErrorException $ex) {
$this->errorHandler($ex);
}
}
 
/**
* @param $rest_host
* @param $rest_port
* @param $username
* @param $password
* @param $hosts
* @param $command
* @param $module
*
* @return mixed
*/
Method `task_register` has 26 lines of code (exceeds 25 allowed). Consider refactoring.
Similar blocks of code found in 2 locations. Consider refactoring.
The parameter $rest_host is not named in camelCase.
The parameter $rest_port is not named in camelCase.
Method name "restclient::task_register" is not in camel caps format
The variable $rest_port is not named in camelCase.
The variable $rest_host is not named in camelCase.
The method task_register is not named in camelCase.
public function task_register(
Method `task_register` has 7 arguments (exceeds 4 allowed). Consider refactoring.
$rest_host,
$rest_port,
$username,
$password,
$hosts,
$command,
$module
) {
try {
$uri = 'http://' . $rest_host . ':' . $rest_port . '/eisen/api/v1.0/tasks';
$response = \Httpful\Request::post($uri)
->sendsJson()// tell it we're sending (Content-Type) JSON...
->authenticateWith($username, $password)
->body(
'{"hosts":"' .
$hosts .
'","command":"' .
$command .
'","module":"' .
$module .
'"}'
)// attach a body/payload...
->whenError(
function ($error) {
$this->errorHandler($error);
}
)
->sendIt();
$uri = $response->body->task->uri;
$uri = explode("/", $uri);
} catch (ConnectionErrorException $ex) {
$this->errorHandler($ex);
}
return $uri[5];
}
/**
* @param $rest_host
* @param $rest_port
* @param $username
* @param $password
* @param $hosts
* @param $command
* @param $module
*
* @return mixed
*/
 
Method `recipe_register` has 26 lines of code (exceeds 25 allowed). Consider refactoring.
Similar blocks of code found in 2 locations. Consider refactoring.
The parameter $rest_port is not named in camelCase.
The parameter $rest_host is not named in camelCase.
Method name "restclient::recipe_register" is not in camel caps format
The variable $rest_port is not named in camelCase.
The variable $rest_host is not named in camelCase.
The method recipe_register is not named in camelCase.
public function recipe_register(
Method `recipe_register` has 7 arguments (exceeds 4 allowed). Consider refactoring.
$rest_host,
$rest_port,
$username,
$password,
$hosts,
$package,
$file
) {
try {
$uri = 'http://' . $rest_host . ':' . $rest_port . '/eisen/api/v1.0/recipes';
$response = \Httpful\Request::post($uri)
->sendsJson()// tell it we're sending (Content-Type) JSON...
->authenticateWith($username, $password)
->body(
'{"host":"' .
$hosts .
'","file":"' .
$file .
'","package":"' .
$package .
'"}'
)
->whenError(
function ($error) {
$this->errorHandler($error);
}
)
->sendIt();
$uri = $response->body->task->uri;
$uri = explode("/", $uri);
} catch (ConnectionErrorException $ex) {
$this->errorHandler($ex);
}
return $uri[5];
}
/**
* @param $rest_host
* @param $rest_port
* @param $username
* @param $password
* @param $hosts
* @param $command
* @param $module
*
* @return mixed
*/
Method `package_Action` has 28 lines of code (exceeds 25 allowed). Consider refactoring.
The parameter $rest_port is not named in camelCase.
The parameter $rest_host is not named in camelCase.
Method name "restclient::package_Action" is not in camel caps format
The variable $rest_port is not named in camelCase.
The variable $rest_host is not named in camelCase.
The method package_Action is not named in camelCase.
public function package_Action(
Method `package_Action` has 9 arguments (exceeds 4 allowed). Consider refactoring.
$rest_host,
$rest_port,
$username,
$password,
$targetHost,
$targetOs,
$packageName,
$packageVersion,
$packageAction
) {
try {
$uri = 'http://' . $rest_host . ':' . $rest_port . '/eisen/api/v1.0/packages';
$response = \Httpful\Request::post($uri)
->sendsJson()// tell it we're sending (Content-Type) JSON...
->authenticateWith($username, $password)
->body(
'{"targetHost":"' .
$targetHost .
'","targetOS":"' .
$targetOs .
'","packageName":"' .
$packageName .
'","packageVersion":"' .
$packageVersion .
'","packageAction":"' .
$packageAction .
'"}'
)// attach a body/payload...
->whenError(
function ($error) {
$this->errorHandler($error);
}
)
->sendIt();
//$uri = $response->body->task->uri;
//$uri = explode("/", $uri);
} catch (ConnectionErrorException $ex) {
$this->errorHandler($ex);
}
return $response;
}
 
/**
* @param $rest_host
* @param $rest_port
* @param $username
* @param $password
* @param $target_host
* @param $variable_key
* @param $variable_value
*/
The parameter $target_host is not named in camelCase.
The parameter $rest_host is not named in camelCase.
The parameter $variable_key is not named in camelCase.
The parameter $variable_value is not named in camelCase.
The parameter $rest_port is not named in camelCase.
The variable $host_id is not named in camelCase.
The variable $rest_port is not named in camelCase.
The variable $target_host is not named in camelCase.
The variable $rest_host is not named in camelCase.
The variable $variable_key is not named in camelCase.
The variable $variable_value is not named in camelCase.
public function variableRegister(
Method `variableRegister` has 7 arguments (exceeds 4 allowed). Consider refactoring.
$rest_host,
$rest_port,
$username,
$password,
$target_host,
$variable_key,
$variable_value
) {
$host_id = 1;
Blank line found at start of control structure
try {
 
$uri = 'http://' . $rest_host . ':' . $rest_port . '/eisen/api/v1.0/host/'.$host_id.'/vars';
Avoid unused local variables such as '$response'.
$response = \Httpful\Request::post($uri)
->sendsJson()// tell it we're sending (Content-Type) JSON...
->authenticateWith($username, $password)
->body(
'{"host":"' . $target_host .
'","variable_key":"' .
$variable_key .
'","variable_value":"' .
$variable_value .
'"}'
)// attach a body/payload...
->whenError(
function ($error) {
$this->errorHandler($error);
}
)
->sendIt();
} catch (ConnectionErrorException $ex) {
$this->errorHandler($ex);
}
}
 
Method `tasks_delete` has 5 arguments (exceeds 4 allowed). Consider refactoring.
The parameter $task_id is not named in camelCase.
The parameter $rest_host is not named in camelCase.
The parameter $rest_port is not named in camelCase.
Method name "restclient::tasks_delete" is not in camel caps format
The variable $rest_host is not named in camelCase.
The variable $rest_port is not named in camelCase.
The variable $task_id is not named in camelCase.
The method tasks_delete is not named in camelCase.
public function tasks_delete($rest_host, $rest_port, $username, $password, $task_id)
{
try {
$uri = 'http://' . $rest_host . ':' . $rest_port . '/eisen/api/v1.0/task/' . $task_id;
Avoid unused local variables such as '$response'.
$response = \Httpful\Request::delete($uri)
->authenticateWith($username, $password)
->whenError(
function ($error) {
$this->errorHandler($error);
}
)
->send();
//var_dump($response);
} catch (ConnectionErrorException $ex) {
$this->errorHandler($ex);
}
}
 
/**
* @param $rest_host
* @param $rest_port
* @param $username
* @param $password
*
* @return \Httpful\Response|mixed|null
*/
The parameter $rest_host is not named in camelCase.
The parameter $rest_port is not named in camelCase.
Method name "restclient::check_os" is not in camel caps format
The variable $rest_host is not named in camelCase.
The variable $rest_port is not named in camelCase.
The method check_os is not named in camelCase.
public function check_os($rest_host, $rest_port, $username, $password)
{
$response = null;
try {
$uri = 'http://' . $rest_host . ':' . $rest_port . '/eisen/api/v1.0/os_check';
$response = \Httpful\Request::get($uri)
->authenticateWith($username, $password)
->send();
# convert json from stdobject to array
$response = json_decode($response->raw_body, true);
} catch (ConnectionErrorException $ex) {
// set host manager offline
Missing class import via use statement (line '552', column '24').
$dba = new DbAction();
$dbh = $dba->Connect();
$dba->hostManagerStatus($dbh, $rest_host, 'offline');
}
return $response;
}
 
/**
* @param $rest_host
* @param $rest_port
* @param $username
* @param $password
* @param $target_host
* @param $variable_key
* @param $variable_value
*/
The parameter $target_host is not named in camelCase.
The parameter $rest_port is not named in camelCase.
The parameter $target_os is not named in camelCase.
The parameter $rest_host is not named in camelCase.
The variable $target_host is not named in camelCase.
The variable $rest_host is not named in camelCase.
The variable $host_id is not named in camelCase.
The variable $rest_port is not named in camelCase.
The variable $target_os is not named in camelCase.
public function updatePackage(
Method `updatePackage` has 7 arguments (exceeds 4 allowed). Consider refactoring.
$rest_host,
$rest_port,
$username,
$password,
$target_host,
$target_os,
$command
) {
Avoid unused local variables such as '$host_id'.
$host_id = 1;
Blank line found at start of control structure
try {
 
$uri = 'http://' . $rest_host . ':' . $rest_port . '/eisen/api/v1.0/package_retrieve';
Avoid unused local variables such as '$response'.
$response = \Httpful\Request::post($uri)
->sendsJson()// tell it we're sending (Content-Type) JSON...
->authenticateWith($username, $password)
->body(
'{
"target_host":"' .
$target_host .
'","target_os":"' .
$target_os .
'","command":"' .
$command .
'"}'
)// attach a body/payload...
->whenError(
function ($error) {
$this->errorHandler($error);
}
)
->sendIt();
} catch (ConnectionErrorException $ex) {
$this->errorHandler($ex);
}
}
 
public function errorHandler($error)
{
$log = $this->logger();
$log->addError($error);
}
}