Skip to content

Commit

Permalink
Fix PSR- 2
Browse files Browse the repository at this point in the history
  • Loading branch information
vluzrmos committed May 21, 2015
1 parent 185cd6e commit ce4d6d6
Show file tree
Hide file tree
Showing 4 changed files with 165 additions and 142 deletions.
18 changes: 18 additions & 0 deletions src/Vluzrmos/SlackApi/Contracts/SlackApi.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Vluzrmos\SlackApi\Contracts;

interface SlackApi
{
public function method($method, $url, $parameters);

public function get($apiMethod, $parameters = []);

public function post($apiMethod, $parameters = []);

public function delete($apiMethod, $parameters = []);

public function put($apiMethod, $parameters = []);

public function patch($apiMethod, $parameters = []);
}
188 changes: 98 additions & 90 deletions src/Vluzrmos/SlackApi/SlackApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,197 +3,205 @@
namespace Vluzrmos\SlackApi;

use GuzzleHttp\Client;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Traits\Macroable;
use Vluzrmos\SlackApi\Contracts\SlackApi as Contract;

class SlackApi {
class SlackApi implements Contract
{
use Macroable;

/**
*
* @var \GuzzleHttp\Client
*/
private $client;

/**
* Token of the user of the Slack team (with administrator levels)
* Token of the user of the Slack team (with administrator levels).
*
* @var string
*/
private $token;

/**
* Url to slack.com, by default will use https://slack.com/api
* Url to slack.com, by default will use https://slack.com/api.
*
* @var String
*/
private $url = "https://slack.com/api";
private $url = 'https://slack.com/api';

/**
* @param Client|null $client
* @param String|null $token
*/
function __construct(Client $client = null, $token=null){
$this->setClient($client);
$this->setToken($token);
public function __construct(Client $client = null, $token = null)
{
$this->setClient($client);
$this->setToken($token);
}

/**
* @param string $method
* @param string $url
* @param array $parameters
*
* @return mixed;
*/
public function method($method = "get", $url="", $parameters = []){
return json_decode(($this->getClient()->$method($url, $parameters)->getBody()->getContents()), true);
public function method($method = 'get', $url = '', $parameters = [])
{
return json_decode(($this->getClient()->$method($url, $parameters)->getBody()->getContents()), true);
}

/**
* Send a GET Request
* Send a GET Request.
*
* @param $apiMethod
* @param array $parameters
*
* @return \GuzzleHttp\Message\ResponseInterface
*/
public function get($apiMethod, $parameters = []){
$url = $this->getUrl($apiMethod);
$parameters = $this->mergeParameters($parameters);
public function get($apiMethod, $parameters = [])
{
$url = $this->getUrl($apiMethod);
$parameters = $this->mergeParameters($parameters);

return $this->method('get', $url, $parameters);
return $this->method('get', $url, $parameters);
}

/**
* Send a POST Request
* Send a POST Request.
*
* @param $apiMethod
* @param array $parameters
*
* @return \GuzzleHttp\Message\ResponseInterface
*/
public function post($apiMethod, $parameters = []){
$url = $this->getUrl($apiMethod);
$parameters = $this->mergeParameters($parameters);
public function post($apiMethod, $parameters = [])
{
$url = $this->getUrl($apiMethod);
$parameters = $this->mergeParameters($parameters);

return $this->method('post', $url, $parameters);
return $this->method('post', $url, $parameters);
}

/**
* Send a PUT Request
* Send a PUT Request.
*
* @param $apiMethod
* @param array $parameters
*
* @return \GuzzleHttp\Message\ResponseInterface
*/
public function put($apiMethod, $parameters = []){
$url = $this->getUrl($apiMethod);
$parameters = $this->mergeParameters($parameters);
public function put($apiMethod, $parameters = [])
{
$url = $this->getUrl($apiMethod);
$parameters = $this->mergeParameters($parameters);

return $this->method('put', $url, $parameters);
return $this->method('put', $url, $parameters);
}

/**
* Send a DELETE Request
* Send a DELETE Request.
*
* @param $apiMethod
* @param array $parameters
*
* @return \GuzzleHttp\Message\ResponseInterface
*/
public function delete($apiMethod, $parameters = []){
$url = $this->getUrl($apiMethod);
$parameters = $this->mergeParameters($parameters);
public function delete($apiMethod, $parameters = [])
{
$url = $this->getUrl($apiMethod);
$parameters = $this->mergeParameters($parameters);

return $this->method('delete', $url, $parameters);
return $this->method('delete', $url, $parameters);
}

/**
* Send a PATCH Request
* Send a PATCH Request.
*
* @param $apiMethod
* @param array $parameters
*
* @return \GuzzleHttp\Message\ResponseInterface
*/
public function patch($apiMethod, $parameters = []){
$url = $this->getUrl($apiMethod);
$parameters = $this->mergeParameters($parameters);
public function patch($apiMethod, $parameters = [])
{
$url = $this->getUrl($apiMethod);
$parameters = $this->mergeParameters($parameters);

return $this->method('patch', $url, $parameters);
return $this->method('patch', $url, $parameters);
}

/**
* Generate the url with the api $method.
*
* @param null $method
*
* @return string
*/
protected function getUrl($method=null){
return str_finish($this->url, "/").$method;
protected function getUrl($method = null)
{
return str_finish($this->url, '/').$method;
}

/**
* Get the user token
* Get the user token.
*
* @return null|string
*/
protected function getToken(){
return $this->token;
protected function getToken()
{
return $this->token;
}

/**
* Set the token of your slack team member (be sure is admin token)
* Set the token of your slack team member (be sure is admin token).
*
* @param $token
*/
public function setToken($token){
$this->token = $token;
public function setToken($token)
{
$this->token = $token;
}

/**
* Configures the Guzzle Client
* Configures the Guzzle Client.
*
* @param \GuzzleHttp\Client|Callback|null $client
* @param String $verify SSL cert used for HTTPS requests
*/
public function setClient($client = null, $verify=null){
if(is_callable($client)){
$this->client = value($client);
}
elseif(is_null($client) and is_null($this->client)){
$this->client = new Client();
}
else{
$this->client = $client;
}

$this->setSSLVerifyPath($verify);
}

/**
* Configures the path to SSL Cert used on every HTTPS request
* @param String|null $path
*/
public function setSSLVerifyPath($path = null){
$this->getClient()->setDefaultOption('verify', empty($path)?$this->getSSLVerifyPath():$path);
public function setClient($client = null)
{
if (is_callable($client)) {
$this->client = value($client);
} elseif (is_null($client) and is_null($this->client)) {
$this->client = new Client();
} else {
$this->client = $client;
}

$this->client->setDefaultOption('verify', false);
}

/**
* Get the SSL cert used on every HTTPS request
* @return string
*/
public function getSSLVerifyPath(){
$default = $this->getClient()->getDefaultOption('verify');

if(empty($default) or $default === true){
return realpath(__DIR__."/../../../ssl/curl-ca-bundle.crt");
}
else{
return $default;
}
}
/**
* Merge parameters of the request with token em timestamp string
* Merge parameters of the request with token em timestamp string.
*
* @param $parameters
*
* @return mixed
*/
protected function mergeParameters($parameters){
$parameters['query'] = array_merge([
protected function mergeParameters($parameters)
{
$parameters['query'] = array_merge([
't' => time(),
'token' => $this->getToken()
'token' => $this->getToken(),
], array_get($parameters, 'query', []));

$parameters['body'] = array_get($parameters, 'body');
$parameters['body'] = array_get($parameters, 'body');

return $parameters;
return $parameters;
}

public function getClient(){
return $this->client;
}
}
public function getClient()
{
return $this->client;
}
}
13 changes: 8 additions & 5 deletions src/Vluzrmos/SlackApi/SlackApiFacade.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
<?php namespace Vluzrmos\SlackApi;
<?php

use Illuminate\Support\Facades\Facade;
namespace Vluzrmos\SlackApi;

class SlackApiFacade extends Facade{
use Illuminate\Support\Facades\Facade;

class SlackApiFacade extends Facade
{
/**
* @return string
*/
protected static function getFacadeAccessor(){
return 'slackapi';
protected static function getFacadeAccessor()
{
return 'slackapi';
}
}
Loading

0 comments on commit ce4d6d6

Please sign in to comment.