php-censor/src/PHPCensor/Store/Base/BuildStoreBase.php
2017-02-05 15:37:46 +07:00

124 lines
3.2 KiB
PHP

<?php
/**
* Build base store for table: build
*/
namespace PHPCensor\Store\Base;
use b8\Database;
use b8\Exception\HttpException;
use PHPCensor\Store;
use PHPCensor\Model\Build;
/**
* Build Base Store
*/
class BuildStoreBase extends Store
{
protected $tableName = 'build';
protected $modelName = '\PHPCensor\Model\Build';
protected $primaryKey = 'id';
/**
* Get a Build by primary key (Id)
*/
public function getByPrimaryKey($value, $useConnection = 'read')
{
return $this->getById($value, $useConnection);
}
/**
* Get a single Build by Id.
* @return null|Build
*/
public function getById($value, $useConnection = 'read')
{
if (is_null($value)) {
throw new HttpException('Value passed to ' . __FUNCTION__ . ' cannot be null.');
}
$query = 'SELECT * FROM {{build}} WHERE {{id}} = :id LIMIT 1';
$stmt = Database::getConnection($useConnection)->prepareCommon($query);
$stmt->bindValue(':id', $value);
if ($stmt->execute()) {
if ($data = $stmt->fetch(\PDO::FETCH_ASSOC)) {
return new Build($data);
}
}
return null;
}
/**
* Get multiple Build by ProjectId.
* @return array
*/
public function getByProjectId($value, $limit = 1000, $useConnection = 'read')
{
if (is_null($value)) {
throw new HttpException('Value passed to ' . __FUNCTION__ . ' cannot be null.');
}
$query = 'SELECT * FROM {{build}} WHERE {{project_id}} = :project_id LIMIT :limit';
$stmt = Database::getConnection($useConnection)->prepareCommon($query);
$stmt->bindValue(':project_id', $value);
$stmt->bindValue(':limit', (int)$limit, \PDO::PARAM_INT);
if ($stmt->execute()) {
$res = $stmt->fetchAll(\PDO::FETCH_ASSOC);
$map = function ($item) {
return new Build($item);
};
$rtn = array_map($map, $res);
$count = count($rtn);
return ['items' => $rtn, 'count' => $count];
} else {
return ['items' => [], 'count' => 0];
}
}
/**
* Get multiple Build by Status.
*
* @param $value
* @param int $limit
* @param string $useConnection
*
* @return array
*
* @throws HttpException
*/
public function getByStatus($value, $limit = 1000, $useConnection = 'read')
{
if (is_null($value)) {
throw new HttpException('Value passed to ' . __FUNCTION__ . ' cannot be null.');
}
$query = 'SELECT * FROM {{build}} WHERE {{status}} = :status LIMIT :limit';
$stmt = Database::getConnection($useConnection)->prepareCommon($query);
$stmt->bindValue(':status', $value);
$stmt->bindValue(':limit', (int)$limit, \PDO::PARAM_INT);
if ($stmt->execute()) {
$res = $stmt->fetchAll(\PDO::FETCH_ASSOC);
$map = function ($item) {
return new Build($item);
};
$rtn = array_map($map, $res);
$count = count($rtn);
return ['items' => $rtn, 'count' => $count];
} else {
return ['items' => [], 'count' => 0];
}
}
}