Skip to content

Commit

Permalink
separated out ProxyRequest
Browse files Browse the repository at this point in the history
  • Loading branch information
hiqsol committed Jan 22, 2017
1 parent c4c4eaf commit 46f9765
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 31 deletions.
64 changes: 33 additions & 31 deletions src/AbstractRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,10 @@ abstract class AbstractRequest implements \Serializable
protected $responseClass;

/**
* @var string transport implementation to be specified in concrete implementation.
* @var QueryBuilderInterface
*/
public $handlerClass;

protected $builder;

/**
* @var object
*/
protected $worker;

/**
* @var Query
*/
Expand All @@ -50,7 +43,9 @@ abstract class AbstractRequest implements \Serializable

protected $parts = [];

public function __construct(QueryBuilder $builder, Query $query)
abstract public function send($options = []);

public function __construct(QueryBuilderInterface $builder, Query $query)
{
$this->builder = $builder;
$this->query = $query;
Expand Down Expand Up @@ -94,19 +89,14 @@ public function getQuery()
return $this->query;
}

/**
* @return Worker
*/
public function getWorker()
protected function buildRequest()
{
if ($this->worker === null) {
if ($this->isBuilt === null) {
if (!empty($this->query)) {
$this->updateFromQuery();
}
$this->worker = $this->createWorker();
$this->isBuilt = true;
}

return $this->worker;
}

protected function updateFromQuery()
Expand All @@ -124,19 +114,9 @@ protected function updateFromQuery()
$this->buildProtocolVersion();
}

abstract protected function createWorker();

public function send($options = [])
{
$handler = $this->builder->getHandler();
$worker = $handler->send($this->getWorker(), $options);

return new $this->responseClass($worker, $request);
}

protected function buildDbname()
{
$this->dbname = $this->builder->db->name;
$this->dbname = $this->getDb()->name;
}

protected function buildAuth()
Expand Down Expand Up @@ -209,6 +189,7 @@ public function getParts()
{
if (empty($this->parts)) {
$this->getWorker();
$this->buildRequest();
foreach (['dbname', 'method', 'uri', 'headers', 'body', 'version'] as $key) {
$this->parts[$key] = $this->{$key};
}
Expand All @@ -217,12 +198,33 @@ public function getParts()
return $this->parts;
}

public function send()
public function isRaw()
{
return !empty($this->query->options['raw']);
}

protected function getHandler()
{
$handler = $this->getDb()->getHandler();
if ($handler === null) {
$handler = $this->createHandler();
}

return $handler;
}

public function isRaw()
protected function createHandler()
{
return !empty($this->query->options['raw']);
return new $this->handlerClass($this->getDb()->config);
}

protected function getHandlerConfig()
{
return $this->getDb()->config;
}

public function getDb()
{
return $this->builder->db;
}
}
47 changes: 47 additions & 0 deletions src/ProxyRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php
/**
* Tools to use API as ActiveRecord for Yii2
*
* @link https://github.com/hiqdev/yii2-hiart
* @package yii2-hiart
* @license BSD-3-Clause
* @copyright Copyright (c) 2015-2016, HiQDev (http://hiqdev.com/)
*/

namespace hiqdev\hiart;

abstract class ProxyRequest extends AbstractRequest
{
/**
* @var object
*/
protected $worker;

/**
* @var string transport implementation to be specified in concrete implementation.
*/
public $handlerClass;

abstract protected function createWorker();

public function send($options = [])
{
$handler = $this->builder->getHandler();
$worker = $handler->send($this->getWorker(), $options);

return new $this->responseClass($worker, $request);
}

/**
* @return Worker
*/
public function getWorker()
{
if ($this->worker === null) {
$this->buildRequest();
$this->worker = $this->createWorker();
}

return $this->worker;
}
}

0 comments on commit 46f9765

Please sign in to comment.