php-censor/src/Store/EnvironmentStore.php

106 lines
2.5 KiB
PHP
Raw Permalink Normal View History

2017-03-23 13:53:24 +01:00
<?php
namespace PHPCensor\Store;
2018-03-04 09:34:19 +01:00
use PHPCensor\Database;
2017-03-23 13:53:24 +01:00
use PHPCensor\Model\Environment;
use PHPCensor\Store;
2018-03-04 11:14:09 +01:00
use PHPCensor\Exception\HttpException;
2017-03-23 13:53:24 +01:00
class EnvironmentStore extends Store
{
2017-11-05 15:48:36 +01:00
/**
* @var string
*/
2018-02-23 19:49:59 +01:00
protected $tableName = 'environment';
2017-11-05 15:48:36 +01:00
/**
* @var string
*/
2018-02-23 19:49:59 +01:00
protected $modelName = '\PHPCensor\Model\Environment';
2017-11-05 15:48:36 +01:00
/**
* @var string
*/
2018-02-23 19:49:59 +01:00
protected $primaryKey = 'id';
2017-03-23 13:53:24 +01:00
/**
* Get a Environment by primary key (Id)
2017-10-14 20:29:29 +02:00
*
* @param integer $key
* @param string $useConnection
*
2017-03-23 13:53:24 +01:00
* @return null|Environment
*/
2017-10-14 20:29:29 +02:00
public function getByPrimaryKey($key, $useConnection = 'read')
2017-03-23 13:53:24 +01:00
{
2017-10-14 20:29:29 +02:00
return $this->getById($key, $useConnection);
2017-03-23 13:53:24 +01:00
}
/**
* Get a single Environment by Id.
2017-10-14 20:29:29 +02:00
*
* @param integer $id
* @param string $useConnection
*
2017-03-23 13:53:24 +01:00
* @return null|Environment
2017-10-14 20:29:29 +02:00
*
2017-03-23 13:53:24 +01:00
* @throws HttpException
*/
2017-10-14 20:29:29 +02:00
public function getById($id, $useConnection = 'read')
2017-03-23 13:53:24 +01:00
{
2017-10-14 20:29:29 +02:00
if (is_null($id)) {
2017-03-23 13:53:24 +01:00
throw new HttpException('Value passed to ' . __FUNCTION__ . ' cannot be null.');
}
$query = 'SELECT * FROM {{environment}} WHERE {{id}} = :id LIMIT 1';
$stmt = Database::getConnection($useConnection)->prepareCommon($query);
2017-10-14 20:29:29 +02:00
$stmt->bindValue(':id', $id);
2017-03-23 13:53:24 +01:00
if ($stmt->execute()) {
if ($data = $stmt->fetch(\PDO::FETCH_ASSOC)) {
return new Environment($data);
}
}
return null;
}
/**
* Get multiple Environment by Project id.
2017-10-14 20:29:29 +02:00
*
* @param integer $projectId
2017-03-23 13:53:24 +01:00
* @param string $useConnection
2017-10-14 20:29:29 +02:00
*
2017-03-23 13:53:24 +01:00
* @return array
2017-10-14 20:29:29 +02:00
*
2017-03-23 13:53:24 +01:00
* @throws \Exception
*/
2017-10-14 20:29:29 +02:00
public function getByProjectId($projectId, $useConnection = 'read')
2017-03-23 13:53:24 +01:00
{
2017-10-14 20:29:29 +02:00
if (is_null($projectId)) {
2017-03-23 13:53:24 +01:00
throw new \Exception('Value passed to ' . __FUNCTION__ . ' cannot be null.');
}
$query = 'SELECT * FROM {{environment}} WHERE {{project_id}} = :project_id';
$stmt = Database::getConnection($useConnection)->prepareCommon($query);
2017-10-14 20:29:29 +02:00
$stmt->bindValue(':project_id', $projectId);
2017-03-23 13:53:24 +01:00
if ($stmt->execute()) {
$res = $stmt->fetchAll(\PDO::FETCH_ASSOC);
$map = function ($item) {
return new Environment($item);
};
$rtn = array_map($map, $res);
$count = count($rtn);
return ['items' => $rtn, 'count' => $count];
} else {
return ['items' => [], 'count' => 0];
}
}
}