Skip to content

Commit

Permalink
Merge pull request #5 from CottaCush/features/base-files-updates
Browse files Browse the repository at this point in the history
Update Base Files
  • Loading branch information
netwox authored May 25, 2017
2 parents 355ba68 + 772f801 commit 670bd41
Show file tree
Hide file tree
Showing 26 changed files with 1,097 additions and 61 deletions.
2 changes: 1 addition & 1 deletion App/Bootstrap/BaseServicesBootStrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace App\Bootstrap;

use App\CInterface\BootstrapInterface;
use App\Interfaces\BootstrapInterface;
use App\Constants\Services;
use App\Library\Response;
use OAuth2\GrantType\AuthorizationCode;
Expand Down
2 changes: 1 addition & 1 deletion App/Bootstrap/MiddlewareBootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace App\Bootstrap;

use App\CInterface\BootstrapInterface;
use App\Interfaces\BootstrapInterface;
use App\Middleware\RequestLoggerMiddleware;
use App\Middleware\OAuthMiddleware;
use Phalcon\Config;
Expand Down
4 changes: 1 addition & 3 deletions App/Bootstrap/RouteBootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@

namespace App\Bootstrap;

use App\Bootstrap\Bootstrap;
use App\CInterface\BootstrapInterface;
use App\Interfaces\BootstrapInterface;
use Phalcon\Config;
use Phalcon\Di\Injectable;
use Phalcon\DiInterface;
use PhalconRest\Api;
use Phalcon\Mvc\Micro\Collection as RouteHandler;

/**
Expand Down
6 changes: 6 additions & 0 deletions App/Config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@
'excluded_paths' => [
'/oauth'
]
],

'requestLogger' => [
'excluded_paths' => [
'/authentication'
]
]
]);

Expand Down
2 changes: 1 addition & 1 deletion App/Config/config_common.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
'modelsDir' => __DIR__ . '/../Model/',
'controllersDir' => __DIR__ . '/../Controller/',
'libsDir' => __DIR__ . '/../Library/',
'interfacesDir' => __DIR__ . '/../CInterface/',
'interfacesDir' => __DIR__ . '/../Interfaces/',
'pluginsDir' => __DIR__ . '/../plugins/',
'logsDir' => __DIR__ . '/../logs/',
'constantsDir' => __DIR__ . '/../Constants/',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
<?php

namespace App\CInterface;
namespace App\Interfaces;

use Phalcon\Config;
use Phalcon\Di\Injectable;
use Phalcon\DiInterface;
use PhalconRest\Api;

/**
* Interface BootstrapInterface
* @author Adeyemi Olaoye <[email protected]>
* @package App
* @package App\Interfaces
*/
interface BootstrapInterface
{
Expand Down
4 changes: 2 additions & 2 deletions App/CInterface/JSend.php → App/Interfaces/JSend.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
<?php

namespace App\CInterface;
namespace App\Interfaces;

/**
* Interface JSend
* @author Adeyemi Olaoye <[email protected]>
* @package App\Interfaces
*/
interface JSend
{
Expand All @@ -31,4 +32,3 @@ public function sendSuccess($data);
*/
public function sendFail($data, $http_status_code = 500);
}

17 changes: 17 additions & 0 deletions App/Interfaces/Transformable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace App\Interfaces;

/**
* Interface Transformable
* @author Adeyemi Olaoye <[email protected]>
* @package App\Interfaces
*/
interface Transformable
{
/**
* @author Adeyemi Olaoye <[email protected]>
* @return mixed
*/
public function getModelsToLoad();
}
174 changes: 174 additions & 0 deletions App/Library/Batch.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
<?php

namespace App\Library;

use App\Constants\Services;
use App\Exception\BatchInsertException;
use PDOException;
use Phalcon\Di;
use Phalcon\Logger;

/**
* Batch Mockup
* Allows batch inserts
*
* @usage
* $batch = new Batch('stats');
* $batch->columns = ['score', 'name'];
* $batch->data = [
* [1, 'john'],
* [4, 'fred'],
* [1, 'mickey'],
* ];
* $batch->insert();
*
*/
class Batch
{
/** @var string */
public $table = null;

/** @var array */
public $rows = [];

/** @var array */
public $values = [];

// --------------------------------------------------------------

public function __construct($table = false)
{
if ($table) {
$this->table = (string)$table;
}

$di = Di::getDefault();
$this->db = $di->get('db');

return $this;
}

// --------------------------------------------------------------

/**
* Set the Rows
*
* @param array $rows
*
* @return object Batch
*/
public function setRows($rows)
{
$this->rows = $rows;
$this->rowsString = sprintf('`%s`', implode('`,`', $this->rows));

return $this;
}

// --------------------------------------------------------------

/**
* Set the values
*
* @param $values array
*
* @return object Batch
*/
public function setValues($values)
{
if (!$this->rows) {
throw new \Exception('You must setRows() before setValues');
}
$this->values = $values;

$valueCount = count($values);
$fieldCount = count($this->rows);

// Build the Placeholder String
$placeholders = [];
for ($i = 0; $i < $valueCount; $i++) {
$placeholders[] = '(' . rtrim(str_repeat('?,', $fieldCount), ',') . ')';
}
$this->bindString = implode(',', $placeholders);

// Build the Flat Value Array
$valueList = [];
foreach ($values as $value) {
if (is_array($value)) {
foreach ($value as $v) {
$valueList[] = $v;
}
} else {
$valueList[] = $values;
}
}
$this->valuesFlattened = $valueList;
unset($valueList);

return $this;
}

// --------------------------------------------------------------

/**
* Insert into the Database
*
* @param boolean $ignore Use an INSERT IGNORE (Default: false)
*
* @return bool | int
*/
public function insert($ignore = false)
{
$this->validate();

// Optional ignore string
if ($ignore) {
$insertString = "INSERT IGNORE INTO `%s` (%s) VALUES %s";
} else {
$insertString = "INSERT INTO `%s` (%s) VALUES %s";
}

$query = sprintf(
$insertString,
$this->table,
$this->rowsString,
$this->bindString
);

try {
$this->db->execute($query, $this->valuesFlattened);
return $this->db->affectedRows() > 0;
} catch (PDOException $ex) {
Di::getDefault()->get(Services::LOGGER)->error(
'Could not perform bulk insert ' . $ex->getMessage() . ' TRACE: ' . $ex->getTraceAsString()
);
throw new BatchInsertException($ex->getMessage());
}
}

// --------------------------------------------------------------

/**
* Validates the data before calling SQL
*
* @return void
*/
private function validate()
{
if (!$this->table) {
throw new \Exception('Batch Table must be defined');
}

$requiredCount = count($this->rows);

if ($requiredCount == 0) {
throw new \Exception('Batch Rows cannot be empty');
}

foreach ($this->values as $value) {
if (count($value) !== $requiredCount) {
throw new \Exception('Batch Values must match the same column count of ' . $requiredCount);
}
}
}
}
75 changes: 75 additions & 0 deletions App/Library/HttpClient.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

namespace App\Library;

use Phalcon\Http\Client\Provider\Curl;
use Phalcon\Http\Client\Request;
use Phalcon\Http\Client\Response as ClientResponse;

/**
* Class HttpClient
* @author Adeyemi Olaoye <[email protected]>
* @package App\Library
*/
class HttpClient
{
protected $provider;

public function __construct($timeout = null)
{
$this->provider = Request::getProvider();
if ($this->provider instanceof Curl) {
if (!is_null($timeout)) {
$this->provider->setTimeout(intval($timeout));
}
$this->provider->setOption(CURLOPT_SSL_VERIFYPEER, false);
$this->provider->setOption(CURLOPT_SSL_VERIFYHOST, false);
}
}

public function setHeader($key, $value)
{
$this->provider->header->set($key, $value);
}

public function get($url, $params = [])
{
$url = $url . '?' . http_build_query($params);
return $this->provider->get($url);
}


public function post($url, $data)
{
return $this->provider->post($url, $data);
}

public function put($url, $data)
{
return $this->provider->put($url, $data);
}

public function delete($url)
{
return $this->provider->delete($url);
}

/**
* @return \Phalcon\Http\Client\Provider\Curl|\Phalcon\Http\Client\Provider\Stream
*/
public function getProvider()
{
return $this->provider;
}

/**
* @author Adeyemi Olaoye <[email protected]>
* @param $response ClientResponse
* @param int $successCode
* @return bool
*/
public static function isSuccessful($response, $successCode = 200)
{
return $response->header->statusCode === $successCode;
}
}
Loading

0 comments on commit 670bd41

Please sign in to comment.