-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from CottaCush/features/base-files-updates
Update Base Files
- Loading branch information
Showing
26 changed files
with
1,097 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 3 additions & 2 deletions
5
App/CInterface/BootstrapInterface.php → App/Interfaces/BootstrapInterface.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
{ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
{ | ||
|
@@ -31,4 +32,3 @@ public function sendSuccess($data); | |
*/ | ||
public function sendFail($data, $http_status_code = 500); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
Oops, something went wrong.