sansaralab/monk

View on GitHub
src/Monk.php

Summary

Maintainability
A
1 hr
Test Coverage
<?php
 
namespace Monk;
 
use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException;
use Monk\Base\DefaultUrlFormer;
use Monk\Base\Factory;
use Monk\Interfaces\UrlFormerInterface;
use Monk\Monk\Config;
use Monk\Monk\Method;
use Monk\Monk\Response;
 
/**
* Class Monk
* @package Monk
*/
class Monk extends Factory
{
 
/**
* @var string Request url.
*/
protected $resource = null;
 
/**
* @var null Request method.
*/
protected $method = Method::GET;
 
/**
* @var Config Monk config.
*/
protected $config = null;
 
/**
* @var UrlFormerInterface
*/
protected $urlFormer = null;
 
 
/**
* Monk constructor.
*/
protected function __construct()
{
$this->config = new Config();
$this->urlFormer = new DefaultUrlFormer();
}
 
 
/**
* @return static
*/
public static function test()
{
return static::factory();
}
 
 
/**
* @return Config
*/
public function config(): Config
{
return $this->config;
}
 
 
/**
* @param Config $config
* @return $this
*/
public function setConfig(Config $config)
{
$this->config = $config;
return $this;
}
 
 
/**
* @param string $resource
* @return $this
*/
public function setResource(string $resource)
{
$this->resource = $resource;
return $this;
}
 
 
/**
* @param string $method
* @return $this
*/
public function setMethod(string $method)
{
$this->method = $method;
return $this;
}
 
 
/**
* @param UrlFormerInterface $former
* @return $this
*/
public function setUrlFormer(UrlFormerInterface $former)
{
$this->urlFormer = $former;
return $this;
}
 
 
/**
* @return Response
* @throws \Exception
*/
Method `send` has 33 lines of code (exceeds 25 allowed). Consider refactoring.
Function `send` has a Cognitive Complexity of 7 (exceeds 5 allowed). Consider refactoring.
public function send(): Response
{
$this->assertPredefined();
 
$requestParameters = [];
 
$requestParameters['stream'] = false;
 
// setting parameters by type
$paramsData = $this->config()->getParams();
if (in_array($this->method, [Method::POST, Method::PUT, Method::DELETE])) {
if ($this->config()->getParamsType() === Config::PARAMS_FORM) {
$requestParameters['form_params'] = $paramsData;
} elseif ($this->config()->getParamsType() === Config::PARAMS_BODY) {
$requestParameters['body'] = json_encode($paramsData);
The method send uses an else expression. Else clauses are basically not necessary and you can simplify the code by not using them.
} else {
Missing class import via use statement (line '134', column '27').
throw new \Exception("Wrong request params type: '{$this->config()->getParamsType()}'");
}
}
 
// setting headers
$requestParameters['headers'] = $this->config()->getHeaders();
 
// setting auth
$auth = $this->config()->getAuth();
if (!is_null($auth)) {
$requestParameters['auth'] = [
$auth->getLogin(),
$auth->getPassword()
];
}
 
// getting url
$url = $this->urlFormer->getUrl($this->resource);
 
// sending request
$client = new Client();
try {
$response = $client->request(
$this->method,
$url,
$requestParameters
);
} catch (RequestException $ex) {
$response = $ex->getResponse();
}
 
return Response::factory()->setResponse($response);
}
 
 
/**
* @throws \Exception
*/
protected function assertPredefined()
{
if (is_null($this->method)) {
Missing class import via use statement (line '175', column '23').
throw new \Exception('Request method must be set');
}
if (is_null($this->resource)) {
Missing class import via use statement (line '178', column '23').
throw new \Exception('Request resource must be set');
}
if (is_null($this->urlFormer)) {
Missing class import via use statement (line '181', column '23').
throw new \Exception('Url former must be set');
}
}
}