php-censor/src/Store/ProjectStore.php

242 lines
6.2 KiB
PHP
Raw Normal View History

2013-05-03 17:02:53 +02:00
<?php
2016-07-19 20:28:11 +02:00
namespace PHPCensor\Store;
2013-05-03 17:02:53 +02:00
2018-03-04 09:34:19 +01:00
use PHPCensor\Database;
2016-07-19 20:28:11 +02:00
use PHPCensor\Model\Project;
2017-02-16 13:45:50 +01:00
use PHPCensor\Store;
2018-03-04 11:14:09 +01:00
use PHPCensor\Exception\HttpException;
2013-05-03 17:02:53 +02:00
/**
2017-02-16 13:45:50 +01:00
* @author Dan Cryer <dan@block8.co.uk>
*/
class ProjectStore extends Store
2013-05-03 17:02:53 +02:00
{
2017-11-05 15:48:36 +01:00
/**
* @var string
*/
2017-10-14 20:29:29 +02:00
protected $tableName = 'project';
2017-11-05 15:48:36 +01:00
/**
* @var string
*/
2017-10-14 20:29:29 +02:00
protected $modelName = '\PHPCensor\Model\Project';
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 Project by primary key (Id)
2017-10-14 20:29:29 +02:00
*
* @param integer $key
* @param string $useConnection
*
* @return Project|null
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 Project by Id.
2017-10-14 20:29:29 +02:00
*
* @param integer $id
* @param string $useConnection
*
* @return Project|null
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-02-16 13:45:50 +01:00
throw new HttpException('Value passed to ' . __FUNCTION__ . ' cannot be null.');
}
$query = 'SELECT * FROM {{project}} 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 Project($data);
}
}
return null;
}
/**
* Get a single Project by Ids.
2017-11-05 15:48:36 +01:00
*
* @param integer[] $values
* @param string $useConnection
*
* @throws HttpException
*
* @return Project[]
*/
public function getByIds($values, $useConnection = 'read')
{
if (empty($values)) {
throw new HttpException('Values passed to ' . __FUNCTION__ . ' cannot be empty.');
}
$query = 'SELECT * FROM {{project}} WHERE {{id}} IN ('.implode(', ', array_map('intval', $values)).')';
$stmt = Database::getConnection($useConnection)->prepareCommon($query);
$rtn = [];
if ($stmt->execute()) {
while ($data = $stmt->fetch(\PDO::FETCH_ASSOC)) {
$rtn[$data['id']] = new Project($data);
}
}
return $rtn;
}
2017-02-16 13:45:50 +01:00
/**
* Get multiple Project by Title.
2017-10-14 20:29:29 +02:00
*
* @param string $title
* @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 getByTitle($title, $limit = 1000, $useConnection = 'read')
2017-02-16 13:45:50 +01:00
{
2017-10-14 20:29:29 +02:00
if (is_null($title)) {
2017-02-16 13:45:50 +01:00
throw new HttpException('Value passed to ' . __FUNCTION__ . ' cannot be null.');
}
$query = 'SELECT * FROM {{project}} WHERE {{title}} = :title LIMIT :limit';
$stmt = Database::getConnection($useConnection)->prepareCommon($query);
2017-10-14 20:29:29 +02:00
$stmt->bindValue(':title', $title);
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 Project($item);
};
$rtn = array_map($map, $res);
$count = count($rtn);
return ['items' => $rtn, 'count' => $count];
} else {
return ['items' => [], 'count' => 0];
}
}
/**
2017-10-21 10:51:05 +02:00
* Returns a list of all branch names.
2017-10-14 20:29:29 +02:00
*
* @param $projectId
2017-10-14 20:29:29 +02:00
*
* @return array
*/
public function getKnownBranches($projectId)
{
2017-01-29 03:49:43 +01:00
$query = 'SELECT DISTINCT {{branch}} from {{build}} WHERE {{project_id}} = :pid';
$stmt = Database::getConnection('read')->prepareCommon($query);
$stmt->bindValue(':pid', $projectId);
if ($stmt->execute()) {
$res = $stmt->fetchAll(\PDO::FETCH_ASSOC);
$map = function ($item) {
return $item['branch'];
};
$rtn = array_map($map, $res);
return $rtn;
} else {
2016-04-21 06:58:09 +02:00
return [];
}
}
/**
* Get a list of all projects, ordered by their title.
2017-10-14 20:29:29 +02:00
*
2017-01-15 16:35:42 +01:00
* @param boolean $archived
2017-10-14 20:29:29 +02:00
*
* @return array
*/
2017-01-15 16:35:42 +01:00
public function getAll($archived = false)
{
2017-01-15 16:35:42 +01:00
$archived = (integer)$archived;
2017-01-29 03:49:43 +01:00
$query = 'SELECT * FROM {{project}} WHERE {{archived}} = :archived ORDER BY {{title}} ASC';
$stmt = Database::getConnection('read')->prepareCommon($query);
2017-01-15 16:35:42 +01:00
$stmt->bindValue(':archived', $archived);
if ($stmt->execute()) {
$res = $stmt->fetchAll(\PDO::FETCH_ASSOC);
$map = function ($item) {
return new Project($item);
};
$rtn = array_map($map, $res);
$count = count($rtn);
2016-04-21 06:58:09 +02:00
return ['items' => $rtn, 'count' => $count];
} else {
2016-04-21 06:58:09 +02:00
return ['items' => [], 'count' => 0];
}
}
/**
* Get multiple Project by GroupId.
2017-10-14 20:29:29 +02:00
*
* @param integer $groupId
2017-01-15 16:35:42 +01:00
* @param boolean $archived
* @param integer $limit
* @param string $useConnection
2017-10-14 20:29:29 +02:00
*
* @return array
2017-10-14 20:29:29 +02:00
*
* @throws \Exception
*/
2017-10-14 20:29:29 +02:00
public function getByGroupId($groupId, $archived = false, $limit = 1000, $useConnection = 'read')
{
2017-10-14 20:29:29 +02:00
if (is_null($groupId)) {
throw new \Exception('Value passed to ' . __FUNCTION__ . ' cannot be null.');
}
2017-01-15 16:35:42 +01:00
$archived = (integer)$archived;
2017-01-29 03:49:43 +01:00
$query = 'SELECT * FROM {{project}} WHERE {{group_id}} = :group_id AND {{archived}} = :archived ORDER BY {{title}} LIMIT :limit';
$stmt = Database::getConnection($useConnection)->prepareCommon($query);
2017-10-14 20:29:29 +02:00
$stmt->bindValue(':group_id', $groupId);
2017-01-15 16:35:42 +01:00
$stmt->bindValue(':archived', $archived);
$stmt->bindValue(':limit', (int)$limit, \PDO::PARAM_INT);
if ($stmt->execute()) {
$res = $stmt->fetchAll(\PDO::FETCH_ASSOC);
$map = function ($item) {
return new Project($item);
};
$rtn = array_map($map, $res);
$count = count($rtn);
2016-04-21 06:58:09 +02:00
return ['items' => $rtn, 'count' => $count];
} else {
2016-04-21 06:58:09 +02:00
return ['items' => [], 'count' => 0];
}
}
2013-05-16 17:03:34 +02:00
}