Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactoring #2

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.idea/
20 changes: 11 additions & 9 deletions lib/CoordinatorComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class CoordinatorComponent extends Component implements ICoordinatorComponent
/**
* @var string
*/
public $prefix = "db";
public $prefix = '';
/**
* @var array
*/
Expand All @@ -35,18 +35,19 @@ class CoordinatorComponent extends Component implements ICoordinatorComponent
/**
* @throws InvalidConfigException
*/
public function init() {
public function init()
{
parent::init();

if (!$this->component) {
throw new InvalidConfigException("Please set component for coordinator");
throw new InvalidConfigException('Please set component for coordinator');
}

foreach ($this->component as $value) {
$coordinator = \Yii::createObject($value);

if (!$coordinator instanceof ICoordinator) {
throw new InvalidConfigException("Component coordinator not implements ICoordinator interface.");
throw new InvalidConfigException('Component coordinator not implements ICoordinator interface.');
}

$this->coordinator[] = $coordinator;
Expand All @@ -60,7 +61,8 @@ public function init() {
* @return array
* @throws InvalidConfigException
*/
public function getShard(array $db, $data) {
public function getShard(array $db, $data)
{
$data = (!is_array($data)) ? [$data] : $data;

if (!$data) {
Expand All @@ -78,16 +80,16 @@ public function getShard(array $db, $data) {
return [];
}

$result = array_map(function(&$value) {
return $this->prefix.$value;
$result = array_map(function (&$value) {
return $this->prefix . $value;
}, $params);

$error = array_diff($result, $db);

if ($error) {
throw new InvalidConfigException("Not found shard db in db project");
throw new InvalidConfigException('Not found shard db in db project');
}

return (count($result) == 1) ? $result[0] : $result;
return (1 === count($result)) ? $result[0] : $result;
}
}
22 changes: 14 additions & 8 deletions lib/DbCoordinator.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

namespace axiles89\coordinator;


use yii\base\Component;
use yii\base\InvalidConfigException;
use yii\db\Connection;
Expand All @@ -36,36 +35,43 @@ class DbCoordinator extends Component implements ICoordinator
/**
* @throws InvalidConfigException
*/
public function init() {
public function init()
{
parent::init();

if (!$this->table or !isset($this->table['name']) or !isset($this->table['columnSearch']) or !isset($this->table['columnResult'])) {
throw new InvalidConfigException("Please set table for coordinator component");
if (!$this->table
|| !array_key_exists('name', $this->table)
|| !array_key_exists('columnSearch', $this->table)
|| !array_key_exists('columnResult', $this->table)
) {
throw new InvalidConfigException('Please set table for coordinator component');
}

if (!$this->connect) {
throw new InvalidConfigException("Please set connect for coordinator component");
throw new InvalidConfigException('Please set connect for coordinator component');
}

$this->db = \Yii::createObject($this->connect);

if (!$this->db instanceof Connection) {
throw new InvalidConfigException("Component coordinator not implements Connection interface.");
throw new InvalidConfigException('Component coordinator not implements Connection interface.');
}
}

/**
* @param array $data
* @return array
*/
public function execute(array $data) {
public
function execute(array $data)
{
$result = [];

if (!$data) {
return $result;
}

$return = $this->db->createCommand("SELECT {$this->table['columnResult']} FROM {$this->table['name']} WHERE {$this->table['columnSearch']} IN (".implode(",", $data).")")
$return = $this->db->createCommand("SELECT {$this->table['columnResult']} FROM {$this->table['name']} WHERE {$this->table['columnSearch']} IN (" . implode(',', $data) . ')')
->queryAll();

foreach ($return as $value) {
Expand Down
11 changes: 6 additions & 5 deletions lib/FunctionCoordinator.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,14 @@

namespace axiles89\coordinator;


use yii\base\Component;
use yii\base\InvalidConfigException;

/**
* Class FunctionCoordinator
* @package axiles89\coordinator
*/
class FunctionCoordinator extends Component implements ICoordinator
class FunctionCoordinator extends Component implements ICoordinator
{
/**
* @var
Expand All @@ -27,19 +26,21 @@ class FunctionCoordinator extends Component implements ICoordinator
/**
* @throws InvalidConfigException
*/
public function init() {
public function init()
{
parent::init();

if (!$this->function or !is_callable($this->function)) {
throw new InvalidConfigException("Please set callable function for coordinator component");
throw new InvalidConfigException('Please set callable function for coordinator component');
}
}

/**
* @param array $data
* @return array
*/
public function execute(array $data) {
public function execute(array $data)
{
$result = [];

foreach ($data as $value) {
Expand Down
12 changes: 7 additions & 5 deletions lib/RedisCoordinator.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,29 +37,31 @@ class RedisCoordinator extends Component implements ICoordinator
* @throws InvalidConfigException
* @throws \yii\base\InvalidConfigException
*/
public function init() {
public function init()
{
parent::init();

if (!$this->hashName) {
throw new InvalidCallException("Please set hashName for coordinator component");
throw new InvalidCallException('Please set hashName for coordinator component');
}

if (!$this->connect) {
throw new InvalidConfigException("Please set connect for coordinator component");
throw new InvalidConfigException('Please set connect for coordinator component');
}

$this->db = \Yii::createObject($this->connect);

if (!$this->db instanceof Connection) {
throw new InvalidConfigException("Component coordinator not implements redis Connection interface.");
throw new InvalidConfigException('Component coordinator not implements redis Connection interface.');
}
}

/**
* @param array $data
* @return array
*/
public function execute(array $data) {
public function execute(array $data)
{
$result = [];

if (!$data) {
Expand Down