Refactored models.

This commit is contained in:
Dmitry Khomutov 2018-03-04 00:27:48 +07:00
parent 8dc1e8e857
commit 905d6d41d1
No known key found for this signature in database
GPG key ID: EC19426474B37AAC
23 changed files with 369 additions and 582 deletions

View file

@ -1,194 +0,0 @@
<?php
namespace b8;
use b8\Exception\HttpException;
use Symfony\Component\Cache\Simple\ArrayCache;
class Model
{
protected $getters = [];
protected $setters = [];
protected $data = [];
protected $modified = [];
protected $tableName;
protected $cache;
/**
* @param array $initialData
*/
public function __construct($initialData = [])
{
if (is_array($initialData)) {
$this->data = array_merge($this->data, $initialData);
}
$this->cache = new ArrayCache();
}
/**
* @return string
*/
public function getTableName()
{
return $this->tableName;
}
/**
* @return array
*/
public function getDataArray()
{
return $this->data;
}
/**
* @return array
*/
public function getModified()
{
return $this->modified;
}
/**
* @param array $values
*/
public function setValues(array $values)
{
foreach ($values as $key => $value) {
if (isset($this->setters[$key])) {
$func = $this->setters[$key];
if ($value === 'null') {
$value = null;
} elseif ($value === 'true') {
$value = true;
} elseif ($value === 'false') {
$value = false;
}
$this->{$func}($value);
}
}
}
/**
* @param string $column
*/
protected function setModified($column)
{
$this->modified[$column] = $column;
}
/**
* @param string $name
* @param mixed $value
*
* @throws HttpException\ValidationException
*/
protected function validateString($name, $value)
{
if (!is_string($value) && !is_null($value)) {
throw new HttpException\ValidationException('Column "', $name . '" must be a string.');
}
}
/**
* @param string $name
* @param mixed $value
*
* @throws HttpException\ValidationException
*/
protected function validateInt($name, &$value)
{
if (is_bool($value)) {
$value = $value ? 1 : 0;
}
if (!is_numeric($value) && !is_null($value)) {
throw new HttpException\ValidationException('Column "', $name . '" must be an integer.');
}
if (!is_int($value) && !is_null($value)) {
$value = (int)$value;
}
}
/**
* @param string $name
* @param mixed $value
*
* @throws HttpException\ValidationException
*/
protected function validateFloat($name, &$value)
{
if (!is_numeric($value) && !is_null($value)) {
throw new HttpException\ValidationException('Column "', $name . '" must be a float.');
}
if (!is_float($value) && !is_null($value)) {
$value = (float)$value;
}
}
/**
* @param string $name
* @param mixed $value
*
* @throws HttpException\ValidationException
*/
protected function validateDate($name, &$value)
{
if (is_string($value)) {
$value = empty($value) ? null : new \DateTime($value);
}
if ((!is_object($value) || !($value instanceof \DateTime)) && !is_null($value)) {
throw new HttpException\ValidationException('Column "', $name . '" must be a date object.');
}
$value = empty($value) ? null : $value->format('Y-m-d H:i:s');
}
/**
* @param string $name
* @param mixed $value
*
* @throws HttpException\ValidationException
*/
protected function validateNotNull($name, $value)
{
if (is_null($value)) {
throw new HttpException\ValidationException('Column "', $name . '" must not be null.');
}
}
/**
* @param string $key
*
* @return mixed
*/
public function __get($key)
{
if (array_key_exists($key, $this->getters)) {
$getter = $this->getters[$key];
return $this->{$getter}();
}
return null;
}
/**
* @param string $key
* @param mixed $value
*
* @return mixed
*/
public function __set($key, $value)
{
if (array_key_exists($key, $this->setters)) {
$setter = $this->setters[$key];
return $this->{$setter}($value);
}
}
}

View file

@ -2,6 +2,8 @@
namespace b8; namespace b8;
use PHPCensor\Model;
abstract class Store abstract class Store
{ {
/** /**

View file

@ -37,13 +37,13 @@ class Lang
/** /**
* Get a specific string from the language file. * Get a specific string from the language file.
* *
* @param string $string
* @param array ...$params * @param array ...$params
* *
* @return string * @return string
*/ */
public static function get($string, ...$params) public static function get(...$params)
{ {
$string = $params[0];
if (array_key_exists($string, self::$strings)) { if (array_key_exists($string, self::$strings)) {
$params[0] = self::$strings[$string]; $params[0] = self::$strings[$string];
return call_user_func_array('sprintf', $params); return call_user_func_array('sprintf', $params);

View file

@ -2,6 +2,149 @@
namespace PHPCensor; namespace PHPCensor;
abstract class Model extends \b8\Model use b8\Exception\HttpException;
class Model
{ {
/**
* @var array
*/
protected $getters = [];
/**
* @var array
*/
protected $setters = [];
/**
* @var array
*/
protected $data = [];
/**
* @var array
*/
protected $modified = [];
/**
* @var string
*/
protected $tableName;
/**
* @param array $initialData
*/
public function __construct($initialData = [])
{
if (is_array($initialData)) {
foreach ($initialData as $index => $item) {
if (!array_key_exists($index, $this->data)) {
throw new \InvalidArgumentException(sprintf(
'Model "%s" doesn\'t have field "%s"',
get_called_class(),
$index
));
}
$this->data[$index] = $item;
}
}
}
/**
* @return string
*/
public function getTableName()
{
return $this->tableName;
}
/**
* @return array
*/
public function getDataArray()
{
return $this->data;
}
/**
* @return array
*/
public function getModified()
{
return $this->modified;
}
/**
* @param string $column
*/
protected function setModified($column)
{
$this->modified[$column] = $column;
}
/**
* @param string $name
* @param mixed $value
*
* @throws HttpException\ValidationException
*/
protected function validateString($name, $value)
{
if (!is_string($value) && !is_null($value)) {
throw new HttpException\ValidationException('Column "' . $name . '" must be a string.');
}
}
/**
* @param string $name
* @param mixed $value
*
* @throws HttpException\ValidationException
*/
protected function validateInt($name, $value)
{
if (!is_integer($value) && !is_null($value)) {
throw new HttpException\ValidationException('Column "' . $name . '" must be an integer.');
}
}
/**
* @param string $name
* @param mixed $value
*
* @throws HttpException\ValidationException
*/
protected function validateFloat($name, $value)
{
if (!is_float($value) && !is_null($value)) {
throw new HttpException\ValidationException('Column "' . $name . '" must be a float.');
}
}
/**
* @param string $name
* @param mixed $value
*
* @throws HttpException\ValidationException
*/
protected function validateDate($name, $value)
{
if (!($value instanceof \DateTime) && !is_null($value)) {
throw new HttpException\ValidationException('Column "', $name . '" must be a date object.');
}
}
/**
* @param string $name
* @param mixed $value
*
* @throws HttpException\ValidationException
*/
protected function validateNotNull($name, $value)
{
if (is_null($value)) {
throw new HttpException\ValidationException('Column "', $name . '" must not be null.');
}
}
} }

View file

@ -124,9 +124,6 @@ class Build extends Model
'environment' => 'setEnvironment', 'environment' => 'setEnvironment',
'source' => 'setSource', 'source' => 'setSource',
'user_id' => 'setUserId', 'user_id' => 'setUserId',
// Foreign key setters:
'Project' => 'setProject',
]; ];
/** /**
@ -140,7 +137,7 @@ class Build extends Model
} }
/** /**
* @param $value int * @param integer $value
*/ */
public function setId($value) public function setId($value)
{ {
@ -167,7 +164,7 @@ class Build extends Model
} }
/** /**
* @param $value int * @param integer $value
*/ */
public function setProjectId($value) public function setProjectId($value)
{ {
@ -194,7 +191,7 @@ class Build extends Model
} }
/** /**
* @param $value string * @param string $value
*/ */
public function setCommitId($value) public function setCommitId($value)
{ {
@ -221,7 +218,7 @@ class Build extends Model
} }
/** /**
* @param $value int * @param integer $value
*/ */
public function setStatus($value) public function setStatus($value)
{ {
@ -248,7 +245,7 @@ class Build extends Model
} }
/** /**
* @param $value string * @param string $value
*/ */
public function setLog($value) public function setLog($value)
{ {
@ -274,7 +271,7 @@ class Build extends Model
} }
/** /**
* @param $value string * @param string $value
*/ */
public function setBranch($value) public function setBranch($value)
{ {
@ -305,17 +302,19 @@ class Build extends Model
} }
/** /**
* @param $value \DateTime * @param \DateTime $value
*/ */
public function setCreateDate($value) public function setCreateDate(\DateTime $value)
{ {
$this->validateDate('create_date', $value); $this->validateDate('create_date', $value);
if ($this->data['create_date'] === $value) { $stringValue = $value->format('Y-m-d H:i:s');
if ($this->data['create_date'] === $stringValue) {
return; return;
} }
$this->data['create_date'] = $value; $this->data['create_date'] = $stringValue;
$this->setModified('create_date'); $this->setModified('create_date');
} }
@ -335,17 +334,19 @@ class Build extends Model
} }
/** /**
* @param $value \DateTime * @param \DateTime $value
*/ */
public function setStartDate($value) public function setStartDate(\DateTime $value)
{ {
$this->validateDate('start_date', $value); $this->validateDate('start_date', $value);
if ($this->data['start_date'] === $value) { $stringValue = $value->format('Y-m-d H:i:s');
if ($this->data['start_date'] === $stringValue) {
return; return;
} }
$this->data['start_date'] = $value; $this->data['start_date'] = $stringValue;
$this->setModified('start_date'); $this->setModified('start_date');
} }
@ -365,17 +366,19 @@ class Build extends Model
} }
/** /**
* @param $value \DateTime * @param \DateTime $value
*/ */
public function setFinishDate($value) public function setFinishDate(\DateTime $value)
{ {
$this->validateDate('finish_date', $value); $this->validateDate('finish_date', $value);
if ($this->data['finish_date'] === $value) { $stringValue = $value->format('Y-m-d H:i:s');
if ($this->data['finish_date'] === $stringValue) {
return; return;
} }
$this->data['finish_date'] = $value; $this->data['finish_date'] = $stringValue;
$this->setModified('finish_date'); $this->setModified('finish_date');
} }
@ -391,7 +394,7 @@ class Build extends Model
} }
/** /**
* @param $value string * @param string $value
*/ */
public function setCommitterEmail($value) public function setCommitterEmail($value)
{ {
@ -417,7 +420,7 @@ class Build extends Model
} }
/** /**
* @param $value string * @param string $value
*/ */
public function setCommitMessage($value) public function setCommitMessage($value)
{ {
@ -443,7 +446,7 @@ class Build extends Model
} }
/** /**
* @param $value string * @param string $value
*/ */
public function setTag($value) public function setTag($value)
{ {
@ -469,7 +472,7 @@ class Build extends Model
} }
/** /**
* @param $value integer * @param integer $value
*/ */
public function setSource($value) public function setSource($value)
{ {
@ -495,7 +498,7 @@ class Build extends Model
} }
/** /**
* @param $value integer * @param integer $value
*/ */
public function setUserId($value) public function setUserId($value)
{ {
@ -522,7 +525,7 @@ class Build extends Model
} }
/** /**
* @param $value string * @param string $value
*/ */
public function setEnvironment($value) public function setEnvironment($value)
{ {
@ -540,8 +543,9 @@ class Build extends Model
/** /**
* Set the value of status only if it synced with db. Must not be null. * Set the value of status only if it synced with db. Must not be null.
* *
* @param $value int * @param integer $value
* @return bool *
* @return boolean
*/ */
public function setStatusSync($value) public function setStatusSync($value)
{ {
@ -561,9 +565,9 @@ class Build extends Model
/** /**
* Return a value from the build's "extra" JSON array. * Return a value from the build's "extra" JSON array.
* *
* @param null $key * @param string|null $key
* *
* @return mixed|null|string * @return array|string|null
*/ */
public function getExtra($key = null) public function getExtra($key = null)
{ {
@ -581,7 +585,7 @@ class Build extends Model
} }
/** /**
* @param $value string * @param string $value
*/ */
public function setExtra($value) public function setExtra($value)
{ {
@ -599,8 +603,8 @@ class Build extends Model
/** /**
* Set the value of extra. * Set the value of extra.
* *
* @param $name string * @param string $name
* @param $value mixed * @param mixed $value
*/ */
public function setExtraValue($name, $value) public function setExtraValue($name, $value)
{ {
@ -615,7 +619,7 @@ class Build extends Model
/** /**
* Set the values of extra. * Set the values of extra.
* *
* @param $values mixed * @param mixed $values
*/ */
public function setExtraValues($values) public function setExtraValues($values)
{ {
@ -643,37 +647,6 @@ class Build extends Model
return Factory::getStore('Project', 'PHPCensor')->getById($key); return Factory::getStore('Project', 'PHPCensor')->getById($key);
} }
/**
* Set Project - Accepts an ID, an array representing a Project or a Project model.
*
* @param $value mixed
*/
public function setProject($value)
{
// Is this an instance of Project?
if ($value instanceof Project) {
return $this->setProjectObject($value);
}
// Is this an array representing a Project item?
if (is_array($value) && !empty($value['id'])) {
return $this->setProjectId($value['id']);
}
// Is this a scalar value representing the ID of this foreign key?
return $this->setProjectId($value);
}
/**
* Set Project - Accepts a Project model.
*
* @param $value Project
*/
public function setProjectObject(Project $value)
{
return $this->setProjectId($value->getId());
}
/** /**
* Get BuildError models by BuildId for this Build. * Get BuildError models by BuildId for this Build.
* *
@ -763,6 +736,9 @@ class Build extends Model
/** /**
* Store build metadata * Store build metadata
*
* @param string $key
* @param string $value
*/ */
public function storeMeta($key, $value) public function storeMeta($key, $value)
{ {
@ -813,7 +789,9 @@ class Build extends Model
/** /**
* Get an array of plugins to run if there's no .php-censor.yml file. * Get an array of plugins to run if there's no .php-censor.yml file.
*
* @param Builder $builder * @param Builder $builder
*
* @return array * @return array
*/ */
protected function getZeroConfigPlugins(Builder $builder) protected function getZeroConfigPlugins(Builder $builder)
@ -982,7 +960,7 @@ class Build extends Model
/** /**
* Get the number of seconds a build has been running for. * Get the number of seconds a build has been running for.
* *
* @return int * @return integer
*/ */
public function getDuration() public function getDuration()
{ {
@ -1044,7 +1022,7 @@ class Build extends Model
/** /**
* Create an SSH key file on disk for this build. * Create an SSH key file on disk for this build.
* *
* @param string $cloneTo * @param string $cloneTo
* *
* @return string * @return string
*/ */

View file

@ -71,13 +71,10 @@ class BuildError extends Model
'create_date' => 'setCreateDate', 'create_date' => 'setCreateDate',
'hash' => 'setHash', 'hash' => 'setHash',
'is_new' => 'setIsNew', 'is_new' => 'setIsNew',
// Foreign key setters:
'Build' => 'setBuild',
]; ];
/** /**
* @return int * @return integer
*/ */
public function getId() public function getId()
{ {
@ -87,7 +84,7 @@ class BuildError extends Model
} }
/** /**
* @return int * @return integer
*/ */
public function getBuildId() public function getBuildId()
{ {
@ -117,7 +114,7 @@ class BuildError extends Model
} }
/** /**
* @return int * @return integer
*/ */
public function getLineStart() public function getLineStart()
{ {
@ -127,7 +124,7 @@ class BuildError extends Model
} }
/** /**
* @return int * @return integer
*/ */
public function getLineEnd() public function getLineEnd()
{ {
@ -137,7 +134,7 @@ class BuildError extends Model
} }
/** /**
* @return int * @return integer
*/ */
public function getSeverity() public function getSeverity()
{ {
@ -191,7 +188,7 @@ class BuildError extends Model
} }
/** /**
* @param $value int * @param integer $value
*/ */
public function setId($value) public function setId($value)
{ {
@ -208,7 +205,7 @@ class BuildError extends Model
} }
/** /**
* @param $value int * @param integer $value
*/ */
public function setBuildId($value) public function setBuildId($value)
{ {
@ -225,7 +222,7 @@ class BuildError extends Model
} }
/** /**
* @param $value string * @param string $value
*/ */
public function setPlugin($value) public function setPlugin($value)
{ {
@ -242,7 +239,7 @@ class BuildError extends Model
} }
/** /**
* @param $value string * @param string $value
*/ */
public function setFile($value) public function setFile($value)
{ {
@ -258,7 +255,7 @@ class BuildError extends Model
} }
/** /**
* @param $value int * @param integer $value
*/ */
public function setLineStart($value) public function setLineStart($value)
{ {
@ -274,7 +271,7 @@ class BuildError extends Model
} }
/** /**
* @param $value int * @param integer $value
*/ */
public function setLineEnd($value) public function setLineEnd($value)
{ {
@ -290,7 +287,7 @@ class BuildError extends Model
} }
/** /**
* @param $value int * @param integer $value
*/ */
public function setSeverity($value) public function setSeverity($value)
{ {
@ -307,7 +304,7 @@ class BuildError extends Model
} }
/** /**
* @param $value string * @param string $value
*/ */
public function setMessage($value) public function setMessage($value)
{ {
@ -324,24 +321,26 @@ class BuildError extends Model
} }
/** /**
* @param $value \DateTime * @param \DateTime $value
*/ */
public function setCreateDate($value) public function setCreateDate(\DateTime $value)
{ {
$this->validateNotNull('create_date', $value); $this->validateNotNull('create_date', $value);
$this->validateDate('create_date', $value); $this->validateDate('create_date', $value);
if ($this->data['create_date'] === $value) { $stringValue = $value->format('Y-m-d H:i:s');
if ($this->data['create_date'] === $stringValue) {
return; return;
} }
$this->data['create_date'] = $value; $this->data['create_date'] = $stringValue;
$this->setModified('create_date'); $this->setModified('create_date');
} }
/** /**
* @param $value string * @param string $value
*/ */
public function setHash($value) public function setHash($value)
{ {
@ -358,7 +357,7 @@ class BuildError extends Model
} }
/** /**
* @param $value int * @param integer $value
*/ */
public function setIsNew($value) public function setIsNew($value)
{ {
@ -377,56 +376,16 @@ class BuildError extends Model
/** /**
* Get the Build model for this BuildError by Id. * Get the Build model for this BuildError by Id.
* *
* @return \PHPCensor\Model\Build * @return \PHPCensor\Model\Build|null
*/ */
public function getBuild() public function getBuild()
{ {
$key = $this->getBuildId(); $buildId = $this->getBuildId();
if (empty($buildId)) {
if (empty($key)) {
return null; return null;
} }
$cacheKey = 'php-censor.build-' . $key; return Factory::getStore('Build', 'PHPCensor')->getById($buildId);
$rtn = $this->cache->get($cacheKey);
if (empty($rtn)) {
$rtn = Factory::getStore('Build', 'PHPCensor')->getById($key);
$this->cache->set($cacheKey, $rtn);
}
return $rtn;
}
/**
* Set Build - Accepts an ID, an array representing a Build or a Build model.
*
* @param $value mixed
*/
public function setBuild($value)
{
// Is this an instance of Build?
if ($value instanceof Build) {
return $this->setBuildObject($value);
}
// Is this an array representing a Build item?
if (is_array($value) && !empty($value['id'])) {
return $this->setBuildId($value['id']);
}
// Is this a scalar value representing the ID of this foreign key?
return $this->setBuildId($value);
}
/**
* Set Build - Accepts a Build model.
*
* @param $value Build
*/
public function setBuildObject(Build $value)
{
return $this->setBuildId($value->getId());
} }
/** /**

View file

@ -45,13 +45,10 @@ class BuildMeta extends Model
'build_id' => 'setBuildId', 'build_id' => 'setBuildId',
'meta_key' => 'setMetaKey', 'meta_key' => 'setMetaKey',
'meta_value' => 'setMetaValue', 'meta_value' => 'setMetaValue',
// Foreign key setters:
'Build' => 'setBuild',
]; ];
/** /**
* @return int * @return integer
*/ */
public function getId() public function getId()
{ {
@ -61,7 +58,7 @@ class BuildMeta extends Model
} }
/** /**
* @return int * @return integer
*/ */
public function getBuildId() public function getBuildId()
{ {
@ -91,7 +88,7 @@ class BuildMeta extends Model
} }
/** /**
* @param int $value * @param integer $value
*/ */
public function setId($value) public function setId($value)
{ {
@ -108,7 +105,7 @@ class BuildMeta extends Model
} }
/** /**
* @param int $value * @param integer $value
*/ */
public function setBuildId($value) public function setBuildId($value)
{ {
@ -125,7 +122,7 @@ class BuildMeta extends Model
} }
/** /**
* @param $value string * @param string $value
*/ */
public function setMetaKey($value) public function setMetaKey($value)
{ {
@ -142,7 +139,7 @@ class BuildMeta extends Model
} }
/** /**
* @param $value string * @param string $value
*/ */
public function setMetaValue($value) public function setMetaValue($value)
{ {
@ -165,51 +162,11 @@ class BuildMeta extends Model
*/ */
public function getBuild() public function getBuild()
{ {
$key = $this->getBuildId(); $buildId = $this->getBuildId();
if (empty($buildId)) {
if (empty($key)) {
return null; return null;
} }
$cacheKey = 'php-censor.build-' . $key; return Factory::getStore('Build', 'PHPCensor')->getById($buildId);
$rtn = $this->cache->get($cacheKey);
if (empty($rtn)) {
$rtn = Factory::getStore('Build', 'PHPCensor')->getById($key);
$this->cache->set($cacheKey, $rtn);
}
return $rtn;
}
/**
* Set Build - Accepts an ID, an array representing a Build or a Build model.
*
* @param $value mixed
*/
public function setBuild($value)
{
// Is this an instance of Build?
if ($value instanceof Build) {
return $this->setBuildObject($value);
}
// Is this an array representing a Build item?
if (is_array($value) && !empty($value['id'])) {
return $this->setBuildId($value['id']);
}
// Is this a scalar value representing the ID of this foreign key?
return $this->setBuildId($value);
}
/**
* Set Build - Accepts a Build model.
*
* @param $value Build
*/
public function setBuildObject(Build $value)
{
return $this->setBuildId($value->getId());
} }
} }

View file

@ -42,7 +42,7 @@ class Environment extends Model
]; ];
/** /**
* @return int * @return integer
*/ */
public function getId() public function getId()
{ {
@ -52,7 +52,7 @@ class Environment extends Model
} }
/** /**
* @return int * @return integer
*/ */
public function getProjectId() public function getProjectId()
{ {
@ -82,7 +82,7 @@ class Environment extends Model
} }
/** /**
* @param $value int * @param integer $value
*/ */
public function setId($value) public function setId($value)
{ {
@ -99,7 +99,7 @@ class Environment extends Model
} }
/** /**
* @param $value int * @param integer $value
*/ */
public function setProjectId($value) public function setProjectId($value)
{ {
@ -116,7 +116,7 @@ class Environment extends Model
} }
/** /**
* @param $value string * @param string $value
*/ */
public function setName($value) public function setName($value)
{ {
@ -133,7 +133,7 @@ class Environment extends Model
} }
/** /**
* @param $value array * @param array $value
*/ */
public function setBranches($value) public function setBranches($value)
{ {

View file

@ -94,7 +94,7 @@ class Project extends Model
]; ];
/** /**
* @return int * @return integer
*/ */
public function getId() public function getId()
{ {
@ -174,7 +174,7 @@ class Project extends Model
} }
/** /**
* @return int * @return integer
*/ */
public function getAllowPublicStatus() public function getAllowPublicStatus()
{ {
@ -184,7 +184,7 @@ class Project extends Model
} }
/** /**
* @return int * @return integer
*/ */
public function getArchived() public function getArchived()
{ {
@ -194,7 +194,7 @@ class Project extends Model
} }
/** /**
* @return int * @return integer
*/ */
public function getGroupId() public function getGroupId()
{ {
@ -204,7 +204,7 @@ class Project extends Model
} }
/** /**
* @return int * @return integer
*/ */
public function getDefaultBranchOnly() public function getDefaultBranchOnly()
{ {
@ -214,7 +214,7 @@ class Project extends Model
} }
/** /**
* @param $value int * @param integer $value
*/ */
public function setId($value) public function setId($value)
{ {
@ -231,7 +231,7 @@ class Project extends Model
} }
/** /**
* @param $value string * @param string $value
*/ */
public function setTitle($value) public function setTitle($value)
{ {
@ -248,7 +248,7 @@ class Project extends Model
} }
/** /**
* @param $value string * @param string $value
*/ */
public function setReference($value) public function setReference($value)
{ {
@ -265,7 +265,7 @@ class Project extends Model
} }
/** /**
* @param $value string * @param string $value
*/ */
public function setBranch($value) public function setBranch($value)
{ {
@ -282,7 +282,7 @@ class Project extends Model
} }
/** /**
* @param $value int * @param integer $value
*/ */
public function setDefaultBranchOnly($value) public function setDefaultBranchOnly($value)
{ {
@ -299,7 +299,7 @@ class Project extends Model
} }
/** /**
* @param $value string * @param string $value
*/ */
public function setSshPrivateKey($value) public function setSshPrivateKey($value)
{ {
@ -315,7 +315,7 @@ class Project extends Model
} }
/** /**
* @param $value string * @param string $value
*/ */
public function setType($value) public function setType($value)
{ {
@ -332,7 +332,7 @@ class Project extends Model
} }
/** /**
* @param $value string * @param string $value
*/ */
public function setLastCommit($value) public function setLastCommit($value)
{ {
@ -348,7 +348,7 @@ class Project extends Model
} }
/** /**
* @param $value string * @param string $value
*/ */
public function setBuildConfig($value) public function setBuildConfig($value)
{ {
@ -364,7 +364,7 @@ class Project extends Model
} }
/** /**
* @param $value string * @param string $value
*/ */
public function setSshPublicKey($value) public function setSshPublicKey($value)
{ {
@ -380,7 +380,7 @@ class Project extends Model
} }
/** /**
* @param $value int * @param integer $value
*/ */
public function setAllowPublicStatus($value) public function setAllowPublicStatus($value)
{ {
@ -397,7 +397,7 @@ class Project extends Model
} }
/** /**
* @param $value int * @param integer $value
*/ */
public function setArchived($value) public function setArchived($value)
{ {
@ -414,7 +414,7 @@ class Project extends Model
} }
/** /**
* @param $value int * @param integer $value
*/ */
public function setGroupId($value) public function setGroupId($value)
{ {
@ -437,52 +437,13 @@ class Project extends Model
*/ */
public function getGroup() public function getGroup()
{ {
$key = $this->getGroupId(); $groupId = $this->getGroupId();
if (empty($key)) { if (empty($groupId)) {
return null; return null;
} }
$cacheKey = 'php-censor.project-group-' . $key; return Factory::getStore('ProjectGroup', 'PHPCensor')->getById($groupId);
$rtn = $this->cache->get($cacheKey);
if (empty($rtn)) {
$rtn = Factory::getStore('ProjectGroup', 'PHPCensor')->getById($key);
$this->cache->set($cacheKey, $rtn);
}
return $rtn;
}
/**
* Set Group - Accepts an ID, an array representing a ProjectGroup or a ProjectGroup model.
*
* @param $value mixed
*/
public function setGroup($value)
{
// Is this an instance of ProjectGroup?
if ($value instanceof ProjectGroup) {
return $this->setGroupObject($value);
}
// Is this an array representing a ProjectGroup item?
if (is_array($value) && !empty($value['id'])) {
return $this->setGroupId($value['id']);
}
// Is this a scalar value representing the ID of this foreign key?
return $this->setGroupId($value);
}
/**
* Set Group - Accepts a ProjectGroup model.
*
* @param $value ProjectGroup
*/
public function setGroupObject(ProjectGroup $value)
{
return $this->setGroupId($value->getId());
} }
/** /**
@ -613,17 +574,19 @@ class Project extends Model
} }
/** /**
* @param $value \DateTime * @param \DateTime $value
*/ */
public function setCreateDate($value) public function setCreateDate(\DateTime $value)
{ {
$this->validateDate('create_date', $value); $this->validateDate('create_date', $value);
if ($this->data['create_date'] === $value) { $stringValue = $value->format('Y-m-d H:i:s');
if ($this->data['create_date'] === $stringValue) {
return; return;
} }
$this->data['create_date'] = $value; $this->data['create_date'] = $stringValue;
$this->setModified('create_date'); $this->setModified('create_date');
} }
@ -639,7 +602,7 @@ class Project extends Model
} }
/** /**
* @param $value integer * @param integer $value
*/ */
public function setUserId($value) public function setUserId($value)
{ {
@ -726,22 +689,13 @@ class Project extends Model
*/ */
public function getEnvironmentsObjects() public function getEnvironmentsObjects()
{ {
$key = $this->getId(); $projectId = $this->getId();
if (empty($key)) { if (empty($projectId)) {
return null; return null;
} }
$cacheKey = 'php-censor.project-environments-' . $key; return $this->getEnvironmentStore()->getByProjectId($projectId);
$rtn = $this->cache->get($cacheKey);
if (empty($rtn)) {
$store = $this->getEnvironmentStore();
$rtn = $store->getByProjectId($key);
$this->cache->set($cacheKey, $rtn);
}
return $rtn;
} }
/** /**

View file

@ -43,7 +43,7 @@ class ProjectGroup extends Model
]; ];
/** /**
* @return int * @return integer
*/ */
public function getId() public function getId()
{ {
@ -53,7 +53,7 @@ class ProjectGroup extends Model
} }
/** /**
* @param $value int * @param integer $value
*/ */
public function setId($value) public function setId($value)
{ {
@ -80,7 +80,7 @@ class ProjectGroup extends Model
} }
/** /**
* @param $value string * @param string $value
*/ */
public function setTitle($value) public function setTitle($value)
{ {
@ -111,17 +111,19 @@ class ProjectGroup extends Model
} }
/** /**
* @param $value \DateTime * @param \DateTime $value
*/ */
public function setCreateDate($value) public function setCreateDate(\DateTime $value)
{ {
$this->validateDate('create_date', $value); $this->validateDate('create_date', $value);
if ($this->data['create_date'] === $value) { $stringValue = $value->format('Y-m-d H:i:s');
if ($this->data['create_date'] === $stringValue) {
return; return;
} }
$this->data['create_date'] = $value; $this->data['create_date'] = $stringValue;
$this->setModified('create_date'); $this->setModified('create_date');
} }
@ -137,7 +139,7 @@ class ProjectGroup extends Model
} }
/** /**
* @param $value integer * @param integer $value
*/ */
public function setUserId($value) public function setUserId($value)
{ {

View file

@ -64,7 +64,7 @@ class User extends Model
]; ];
/** /**
* @return int * @return integer
*/ */
public function getId() public function getId()
{ {
@ -104,7 +104,7 @@ class User extends Model
} }
/** /**
* @return int * @return integer
*/ */
public function getIsAdmin() public function getIsAdmin()
{ {
@ -164,7 +164,7 @@ class User extends Model
} }
/** /**
* @param $value int * @param integer $value
*/ */
public function setId($value) public function setId($value)
{ {
@ -181,7 +181,7 @@ class User extends Model
} }
/** /**
* @param $value string * @param string $value
*/ */
public function setEmail($value) public function setEmail($value)
{ {
@ -198,7 +198,7 @@ class User extends Model
} }
/** /**
* @param $value string * @param string $value
*/ */
public function setHash($value) public function setHash($value)
{ {
@ -215,7 +215,7 @@ class User extends Model
} }
/** /**
* @param $value string * @param string $value
*/ */
public function setName($value) public function setName($value)
{ {
@ -232,7 +232,7 @@ class User extends Model
} }
/** /**
* @param $value int * @param integer $value
*/ */
public function setIsAdmin($value) public function setIsAdmin($value)
{ {
@ -249,7 +249,7 @@ class User extends Model
} }
/** /**
* @param $value string * @param string $value
*/ */
public function setProviderKey($value) public function setProviderKey($value)
{ {
@ -266,7 +266,7 @@ class User extends Model
} }
/** /**
* @param $value string * @param string $value
*/ */
public function setProviderData($value) public function setProviderData($value)
{ {
@ -282,7 +282,7 @@ class User extends Model
} }
/** /**
* @param $value string * @param string $value
*/ */
public function setRememberKey($value) public function setRememberKey($value)
{ {
@ -298,7 +298,7 @@ class User extends Model
} }
/** /**
* @param $value string * @param string $value
*/ */
public function setLanguage($value) public function setLanguage($value)
{ {
@ -312,7 +312,7 @@ class User extends Model
} }
/** /**
* @param $value string * @param string $value
*/ */
public function setPerPage($value) public function setPerPage($value)
{ {

View file

@ -61,7 +61,7 @@ class BuildService
) { ) {
$build = new Build(); $build = new Build();
$build->setCreateDate(new \DateTime()); $build->setCreateDate(new \DateTime());
$build->setProject($project); $build->setProjectId($project->getId());
$build->setStatus(Build::STATUS_PENDING); $build->setStatus(Build::STATUS_PENDING);
$build->setEnvironment($environment); $build->setEnvironment($environment);
@ -116,15 +116,17 @@ class BuildService
{ {
$data = $copyFrom->getDataArray(); $data = $copyFrom->getDataArray();
// Clean up unwanted properties from the original build:
unset($data['id']);
unset($data['status']);
unset($data['log']);
unset($data['start_date']);
unset($data['finish_date']);
$build = new Build(); $build = new Build();
$build->setValues($data); $build->setProjectId($data['project_id']);
$build->setCommitId($data['commit_id']);
$build->setBranch($data['branch']);
$build->setTag($data['tag']);
$build->setCommitterEmail($data['committer_email']);
$build->setCommitMessage($data['commit_message']);
$build->setExtra($data['extra']);
$build->setEnvironment($data['environment']);
$build->setSource($data['source']);
$build->setUserId($data['user_id']);
$build->setCreateDate(new \DateTime()); $build->setCreateDate(new \DateTime());
$build->setStatus(Build::STATUS_PENDING); $build->setStatus(Build::STATUS_PENDING);

View file

@ -96,7 +96,7 @@ class ProjectService
} }
if (array_key_exists('group', $options)) { if (array_key_exists('group', $options)) {
$project->setGroup($options['group']); $project->setGroupId($options['group']);
} }
// Allow certain project types to set access information: // Allow certain project types to set access information:

View file

@ -88,7 +88,7 @@ foreach($projects as $project):
?> ?>
<div class="project-box" id="project-box-<?= $project->getId(); ?>"> <div class="project-box" id="project-box-<?= $project->getId(); ?>">
<div class="small-box small-box-full bg-<?= $subcls; ?>"> <div class="small-box small-box-full bg-<?= $subcls; ?>">
<div class="inner"> <div class="inner">
<h3> <h3>
<a href="<?= APP_URL; ?>project/view/<?= $project->getId(); ?>"> <a href="<?= APP_URL; ?>project/view/<?= $project->getId(); ?>">
@ -120,7 +120,7 @@ foreach($projects as $project):
echo '<span class="small-box-footer-build small-box-footer bg-gray"><i class="fa fa-minus"></i></span>'; echo '<span class="small-box-footer-build small-box-footer bg-gray"><i class="fa fa-minus"></i></span>';
} else { } else {
$build = $builds[$project->getId()][$idx]; $build = $builds[$project->getId()][$idx];
$link = APP_URL . 'build/view/' . $build->id; $link = APP_URL . 'build/view/' . $build->getId();
switch ($build->getStatus()) { switch ($build->getStatus()) {
case 0: case 0:
$class = 'bg-blue'; $class = 'bg-blue';

View file

@ -87,7 +87,7 @@ if ($buildCount > 0) {
?> ?>
<div class="small-box small-box-full bg-<?= $subcls; ?>"> <div class="small-box small-box-full bg-<?= $subcls; ?>">
<div class="inner"> <div class="inner">
<h3> <h3>
<a href="<?= APP_URL; ?>project/view/<?= $project->getId(); ?>"> <a href="<?= APP_URL; ?>project/view/<?= $project->getId(); ?>">
@ -119,7 +119,7 @@ if ($buildCount > 0) {
echo '<span class="small-box-footer-build small-box-footer bg-gray"><i class="fa fa-minus"></i></span>'; echo '<span class="small-box-footer-build small-box-footer bg-gray"><i class="fa fa-minus"></i></span>';
} else { } else {
$build = $builds[$idx]; $build = $builds[$idx];
$link = APP_URL . 'build/view/' . $build->id; $link = APP_URL . 'build/view/' . $build->getId();
switch ($build->getStatus()) { switch ($build->getStatus()) {
case 0: case 0:
$class = 'bg-blue'; $class = 'bg-blue';

View file

@ -92,7 +92,7 @@ foreach($builds as $project_id => $project_envs):
?> ?>
<div class="project-box"> <div class="project-box">
<div class="small-box small-box-full bg-<?= $subcls; ?>"> <div class="small-box small-box-full bg-<?= $subcls; ?>">
<div class="inner"> <div class="inner">
<h3> <h3>
<a href="<?= APP_URL; ?>project/view/<?= $project->getId(); ?>"> <a href="<?= APP_URL; ?>project/view/<?= $project->getId(); ?>">
@ -127,7 +127,7 @@ foreach($builds as $project_id => $project_envs):
echo '<span class="small-box-footer-build small-box-footer bg-gray"><i class="fa fa-minus"></i></span>'; echo '<span class="small-box-footer-build small-box-footer bg-gray"><i class="fa fa-minus"></i></span>';
} else { } else {
$build = $project_env['latest'][$idx]; $build = $project_env['latest'][$idx];
$link = APP_URL . 'build/view/' . $build->id; $link = APP_URL . 'build/view/' . $build->getId();
switch ($build->getStatus()) { switch ($build->getStatus()) {
case 0: case 0:
$class = 'bg-blue'; $class = 'bg-blue';

View file

@ -4,9 +4,8 @@ namespace Tests\b8;
use b8\Form; use b8\Form;
use b8\Config; use b8\Config;
use PHPUnit\Framework\TestCase;
class FormTest extends TestCase class FormTest extends \PHPUnit\Framework\TestCase
{ {
public function testFormBasics() public function testFormBasics()
{ {
@ -17,7 +16,7 @@ class FormTest extends TestCase
self::assertTrue($f->getAction() == '/'); self::assertTrue($f->getAction() == '/');
self::assertTrue($f->getMethod() == 'POST'); self::assertTrue($f->getMethod() == 'POST');
$config = new Config([ new Config([
'b8' => [ 'b8' => [
'view' => [ 'view' => [
'path' => __DIR__ . '/data/view/' 'path' => __DIR__ . '/data/view/'

View file

@ -1,43 +0,0 @@
<?php
namespace Tests\PHPCensor\Helper;
use Tests\PHPCensor\LocalizationTestCase;
class LangTest extends LocalizationTestCase
{
public function testSuccess()
{
self::assertTrue(true);
}
/**
* @return array
*/
public function localizationsProvider()
{
$directory = SRC_DIR . 'Languages' . DIRECTORY_SEPARATOR;
$languages = [];
foreach(glob($directory . '*') as $file) {
$language = include($file);
$languages[$file] = [
$language
];
}
return $languages;
}
/**
* @dataProvider localizationsProvider
*/
/*public function testLocalizations(array $strings)
{
$directory = SRC_DIR . 'Languages' . DIRECTORY_SEPARATOR;
$en = include($directory . 'lang.en.php');
foreach ($en as $enIndex => $enString) {
self::assertArrayHasKey($enIndex, $strings);
}
}*/
}

View file

@ -1,24 +0,0 @@
<?php
namespace Tests\PHPCensor;
class LocalizationTestCase extends \PHPUnit\Framework\TestCase
{
/**
* Returns a string representation of the test case.
*
* @return string
*/
public function toString()
{
$class = new \ReflectionClass($this);
$buffer = sprintf(
'%s::%s',
$class->name,
$this->getName(false)
);
return $buffer . $this->getDataSetAsString(false);
}
}

View file

@ -2,6 +2,7 @@
namespace Tests\PHPCensor\Model; namespace Tests\PHPCensor\Model;
use b8\Exception\HttpException\ValidationException;
use PHPCensor\Model\Build; use PHPCensor\Model\Build;
use PHPCensor\Model; use PHPCensor\Model;
@ -12,15 +13,67 @@ use PHPCensor\Model;
*/ */
class BuildTest extends \PHPUnit\Framework\TestCase class BuildTest extends \PHPUnit\Framework\TestCase
{ {
public function setUp() public function testConstruct()
{
}
public function testExecute_TestIsAValidModel()
{ {
$build = new Build(); $build = new Build();
self::assertTrue($build instanceof \b8\Model);
self::assertTrue($build instanceof Model); self::assertInstanceOf('PHPCensor\Model', $build);
self::assertInstanceOf('PHPCensor\Model\Build', $build);
$build = new Build([
'project_id' => 100,
'branch' => 'master',
]);
self::assertEquals([
'id' => null,
'project_id' => 100,
'commit_id' => null,
'status' => null,
'log' => null,
'branch' => 'master',
'tag' => null,
'create_date' => null,
'start_date' => null,
'finish_date' => null,
'committer_email' => null,
'commit_message' => null,
'extra' => null,
'environment' => null,
'source' => Build::SOURCE_UNKNOWN,
'user_id' => 0,
], $build->getDataArray());
try {
$build = new Build([
'project_id' => 101,
'branch' => 'dev',
'unknown' => 'unknown',
]);
} catch (\InvalidArgumentException $e) {
self::assertEquals(
'Model "PHPCensor\Model\Build" doesn\'t have field "unknown"',
$e->getMessage()
);
}
$build = new Build();
$build->setLog('log');
self::assertEquals('log', $build->getLog());
$build->setLog(null);
self::assertEquals(null, $build->getLog());
try {
$build->setLog([]);
} catch (ValidationException $e) {
self::assertEquals(
'Column "log" must be a string.',
$e->getMessage()
);
}
} }
public function testExecute_TestBaseBuildDefaults() public function testExecute_TestBaseBuildDefaults()

View file

@ -15,7 +15,6 @@ class ProjectTest extends \PHPUnit\Framework\TestCase
public function testExecute_TestIsAValidModel() public function testExecute_TestIsAValidModel()
{ {
$project = new Project(); $project = new Project();
self::assertTrue($project instanceof \b8\Model);
self::assertTrue($project instanceof Model); self::assertTrue($project instanceof Model);
} }

View file

@ -139,7 +139,7 @@ class BuildServiceTest extends \PHPUnit\Framework\TestCase
{ {
$build = new Build(); $build = new Build();
$build->setId(1); $build->setId(1);
$build->setProject(101); $build->setProjectId(101);
$build->setCommitId('abcde'); $build->setCommitId('abcde');
$build->setStatus(Build::STATUS_FAILED); $build->setStatus(Build::STATUS_FAILED);
$build->setLog('Test'); $build->setLog('Test');

View file

@ -94,7 +94,7 @@ class BuildStatusServiceTest extends \PHPUnit\Framework\TestCase
$project = $this->getProjectMock($config[$configId]['previousBuild'], $setProject); $project = $this->getProjectMock($config[$configId]['previousBuild'], $setProject);
$build->setProjectObject($project); $build->setProjectId($project->getId());
return $build; return $build;
} }