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
|
|
|
|
*
|
2018-03-01 13:36:28 +01:00
|
|
|
* @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
|
|
|
|
*
|
2018-03-01 13:36:28 +01:00
|
|
|
* @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;
|
|
|
|
}
|
|
|
|
|
2017-11-05 22:34:11 +01:00
|
|
|
/**
|
|
|
|
* Get a single Project by Ids.
|
2017-11-05 15:48:36 +01:00
|
|
|
*
|
|
|
|
* @param integer[] $values
|
|
|
|
* @param string $useConnection
|
|
|
|
*
|
|
|
|
* @throws HttpException
|
|
|
|
*
|
2017-11-05 22:34:11 +01:00
|
|
|
* @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];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-08 12:25:33 +01:00
|
|
|
/**
|
2017-10-21 10:51:05 +02:00
|
|
|
* Returns a list of all branch names.
|
2017-10-14 20:29:29 +02:00
|
|
|
*
|
2014-12-08 12:25:33 +01:00
|
|
|
* @param $projectId
|
2017-10-14 20:29:29 +02:00
|
|
|
*
|
2014-12-08 12:25:33 +01:00
|
|
|
* @return array
|
|
|
|
*/
|
2014-09-16 13:27:32 +02:00
|
|
|
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);
|
2014-09-16 13:27:32 +02:00
|
|
|
$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 [];
|
2014-09-16 13:27:32 +02:00
|
|
|
}
|
|
|
|
}
|
2014-12-02 17:26:55 +01:00
|
|
|
|
2014-12-08 12:25:33 +01:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
*
|
2014-12-08 12:25:33 +01:00
|
|
|
* @return array
|
|
|
|
*/
|
2017-01-15 16:35:42 +01:00
|
|
|
public function getAll($archived = false)
|
2014-12-02 17:26:55 +01:00
|
|
|
{
|
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);
|
2014-12-02 17:26:55 +01:00
|
|
|
|
|
|
|
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];
|
2015-10-09 11:58:18 +02:00
|
|
|
} else {
|
2016-04-21 06:58:09 +02:00
|
|
|
return ['items' => [], 'count' => 0];
|
2015-10-09 11:58:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
*
|
2015-10-09 11:58:18 +02:00
|
|
|
* @return array
|
2017-10-14 20:29:29 +02:00
|
|
|
*
|
2015-10-09 11:58:18 +02:00
|
|
|
* @throws \Exception
|
|
|
|
*/
|
2017-10-14 20:29:29 +02:00
|
|
|
public function getByGroupId($groupId, $archived = false, $limit = 1000, $useConnection = 'read')
|
2015-10-09 11:58:18 +02:00
|
|
|
{
|
2017-10-14 20:29:29 +02:00
|
|
|
if (is_null($groupId)) {
|
2015-10-09 11:58:18 +02:00
|
|
|
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);
|
2015-10-09 11:58:18 +02:00
|
|
|
|
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);
|
2015-10-09 11:58:18 +02: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);
|
|
|
|
|
2016-04-21 06:58:09 +02:00
|
|
|
return ['items' => $rtn, 'count' => $count];
|
2014-12-02 17:26:55 +01:00
|
|
|
} else {
|
2016-04-21 06:58:09 +02:00
|
|
|
return ['items' => [], 'count' => 0];
|
2014-12-02 17:26:55 +01:00
|
|
|
}
|
|
|
|
}
|
2013-05-16 17:03:34 +02:00
|
|
|
}
|