php-censor/src/Store/BuildMetaStore.php

170 lines
4.4 KiB
PHP
Raw Permalink Normal View History

<?php
2016-07-19 20:28:11 +02:00
namespace PHPCensor\Store;
2017-02-16 13:45:50 +01:00
use PHPCensor\Store;
2018-03-04 09:34:19 +01:00
use PHPCensor\Database;
2016-07-19 20:28:11 +02:00
use PHPCensor\Model\BuildMeta;
2018-03-04 11:14:09 +01:00
use PHPCensor\Exception\HttpException;
2017-02-16 13:45:50 +01:00
class BuildMetaStore extends Store
{
2017-11-05 15:48:36 +01:00
/**
* @var string
*/
2017-10-14 20:29:29 +02:00
protected $tableName = 'build_meta';
2017-11-05 15:48:36 +01:00
/**
* @var string
*/
2017-10-14 20:29:29 +02:00
protected $modelName = '\PHPCensor\Model\BuildMeta';
2017-11-05 15:48:36 +01:00
/**
* @var string
*/
2017-10-14 20:29:29 +02:00
protected $primaryKey = 'id';
2017-02-16 13:45:50 +01:00
/**
* Get a BuildMeta by primary key (Id)
2017-10-14 20:29:29 +02:00
*
* @param integer $key
* @param string $useConnection
*
* @return null|BuildMeta
2017-02-16 13:45:50 +01:00
*/
2017-10-14 20:29:29 +02:00
public function getByPrimaryKey($key, $useConnection = 'read')
2017-02-16 13:45:50 +01:00
{
2017-10-14 20:29:29 +02:00
return $this->getById($key, $useConnection);
2017-02-16 13:45:50 +01:00
}
/**
* Get a single BuildMeta by Id.
2017-10-14 20:29:29 +02:00
*
* @param integer $id
* @param string $useConnection
*
2017-02-16 13:45:50 +01:00
* @return null|BuildMeta
2017-10-14 20:29:29 +02:00
*
* @throws HttpException
2017-02-16 13:45:50 +01:00
*/
2017-10-14 20:29:29 +02:00
public function getById($id, $useConnection = 'read')
2017-02-16 13:45:50 +01:00
{
2017-10-14 20:29:29 +02:00
if (is_null($id)) {
2017-11-09 16:48:46 +01:00
throw new HttpException('id passed to ' . __FUNCTION__ . ' cannot be null.');
2017-02-16 13:45:50 +01:00
}
$query = 'SELECT * FROM {{build_meta}} WHERE {{id}} = :id LIMIT 1';
$stmt = Database::getConnection($useConnection)->prepareCommon($query);
2017-10-14 20:29:29 +02:00
$stmt->bindValue(':id', $id);
2017-02-16 13:45:50 +01:00
if ($stmt->execute()) {
if ($data = $stmt->fetch(\PDO::FETCH_ASSOC)) {
return new BuildMeta($data);
}
}
return null;
}
2017-11-09 16:48:46 +01:00
/**
* @param integer $buildId
* @param string $key
*
* @return null|BuildMeta
*
* @throws HttpException
*/
public function getByKey($buildId, $key)
{
if (is_null($buildId)) {
throw new HttpException('buildId passed to ' . __FUNCTION__ . ' cannot be null.');
}
if (!$key) {
throw new HttpException('key passed to ' . __FUNCTION__ . ' cannot be empty.');
}
$query = 'SELECT * FROM {{build_meta}} WHERE {{build_id}} = :build_id AND {{meta_key}} = :meta_key LIMIT 1';
$stmt = Database::getConnection()->prepareCommon($query);
$stmt->bindValue(':build_id', $buildId);
$stmt->bindValue(':meta_key', $key);
if ($stmt->execute()) {
if ($data = $stmt->fetch(\PDO::FETCH_ASSOC)) {
return new BuildMeta($data);
}
}
return null;
}
2017-02-16 13:45:50 +01:00
/**
* Get multiple BuildMeta by BuildId.
2017-10-14 20:29:29 +02:00
*
* @param integer $buildId
* @param integer $limit
* @param string $useConnection
*
2017-02-16 13:45:50 +01:00
* @return array
2017-10-14 20:29:29 +02:00
*
* @throws HttpException
2017-02-16 13:45:50 +01:00
*/
2017-10-14 20:29:29 +02:00
public function getByBuildId($buildId, $limit = 1000, $useConnection = 'read')
2017-02-16 13:45:50 +01:00
{
2017-10-14 20:29:29 +02:00
if (is_null($buildId)) {
2017-02-16 13:45:50 +01:00
throw new HttpException('Value passed to ' . __FUNCTION__ . ' cannot be null.');
}
$query = 'SELECT * FROM {{build_meta}} WHERE {{build_id}} = :build_id LIMIT :limit';
$stmt = Database::getConnection($useConnection)->prepareCommon($query);
2017-10-14 20:29:29 +02:00
$stmt->bindValue(':build_id', $buildId);
2017-02-16 13:45:50 +01:00
$stmt->bindValue(':limit', (int)$limit, \PDO::PARAM_INT);
if ($stmt->execute()) {
$res = $stmt->fetchAll(\PDO::FETCH_ASSOC);
$map = function ($item) {
return new BuildMeta($item);
};
$rtn = array_map($map, $res);
$count = count($rtn);
return ['items' => $rtn, 'count' => $count];
} else {
return ['items' => [], 'count' => 0];
}
}
2015-10-15 11:17:22 +02:00
/**
* Only used by an upgrade migration to move errors from build_meta to build_error
2017-11-05 15:48:36 +01:00
*
* @param integer $limit
*
2015-10-15 11:17:22 +02:00
* @return array
*/
2015-10-15 15:23:55 +02:00
public function getErrorsForUpgrade($limit)
{
2017-01-29 03:49:43 +01:00
$query = 'SELECT * FROM {{build_meta}}
WHERE {{meta_key}} IN (\'phpmd-data\', \'phpcs-data\', \'phpdoccheck-data\', \'technical_debt-data\')
2017-01-29 03:49:43 +01:00
ORDER BY {{id}} ASC LIMIT :limit';
2017-01-29 03:49:43 +01:00
$stmt = Database::getConnection('read')->prepareCommon($query);
$stmt->bindValue(':limit', $limit, \PDO::PARAM_INT);
if ($stmt->execute()) {
$res = $stmt->fetchAll(\PDO::FETCH_ASSOC);
$map = function ($item) {
return new BuildMeta($item);
};
$rtn = array_map($map, $res);
return $rtn;
} else {
2016-04-21 06:58:09 +02:00
return [];
}
}
2013-10-10 02:12:30 +02:00
}