From 645936f309c2033607a0d867ee0b7e7626917bb5 Mon Sep 17 00:00:00 2001 From: Dan Cryer Date: Thu, 16 May 2013 16:03:34 +0100 Subject: [PATCH] PHPCI/Store PSR2 compliance. Issue #18 --- PHPCI/Controller/ProjectController.php | 7 +- PHPCI/Store/Base/BuildStoreBase.php | 188 ++++++++++++------------- PHPCI/Store/Base/ProjectStoreBase.php | 48 +++---- PHPCI/Store/Base/UserStoreBase.php | 80 +++++------ PHPCI/Store/BuildStore.php | 6 +- PHPCI/Store/ProjectStore.php | 6 +- PHPCI/Store/UserStore.php | 6 +- 7 files changed, 158 insertions(+), 183 deletions(-) diff --git a/PHPCI/Controller/ProjectController.php b/PHPCI/Controller/ProjectController.php index 22e580ab..7585bbea 100644 --- a/PHPCI/Controller/ProjectController.php +++ b/PHPCI/Controller/ProjectController.php @@ -78,8 +78,9 @@ class ProjectController extends b8\Controller protected function getLatestBuildsHtml($projectId, $start = 0) { - $criteria = array('project_id' => $projectId), 10, $start, array(), array('id' => 'DESC'); - $builds = $this->_buildStore->getWhere($criteria); + $criteria = array('project_id' => $projectId); + $order = array('id' => 'DESC'); + $builds = $this->_buildStore->getWhere($criteria, 10, $start, array(), $order); $view = new b8\View('BuildsTable'); $view->builds = $builds['items']; @@ -131,7 +132,7 @@ class ProjectController extends b8\Controller if (!is_null($code)) { $http = new \b8\HttpClient(); $url = 'https://github.com/login/oauth/access_token'; - $params = array('client_id' => $gh['id'], 'client_secret' => $gh['secret'], 'code' => $code) + $params = array('client_id' => $gh['id'], 'client_secret' => $gh['secret'], 'code' => $code); $resp = $http->post($url, $params); if ($resp['success']) { diff --git a/PHPCI/Store/Base/BuildStoreBase.php b/PHPCI/Store/Base/BuildStoreBase.php index 08635c48..89b3a4f9 100644 --- a/PHPCI/Store/Base/BuildStoreBase.php +++ b/PHPCI/Store/Base/BuildStoreBase.php @@ -5,6 +5,7 @@ */ namespace PHPCI\Store\Base; + use b8\Store; /** @@ -12,130 +13,115 @@ use b8\Store; */ class BuildStoreBase extends Store { - protected $_tableName = 'build'; - protected $_modelName = '\PHPCI\Model\Build'; - protected $_primaryKey = 'id'; + protected $tableName = 'build'; + protected $modelName = '\PHPCI\Model\Build'; + protected $primaryKey = 'id'; - public function getByPrimaryKey($value, $useConnection = 'read') - { - return $this->getById($value, $useConnection); - } + public function getByPrimaryKey($value, $useConnection = 'read') + { + return $this->getById($value, $useConnection); + } - public function getById($value, $useConnection = 'read') - { - if(is_null($value)) - { - throw new \b8\Exception\HttpException('Value passed to ' . __FUNCTION__ . ' cannot be null.'); - } + public function getById($value, $useConnection = 'read') + { + if (is_null($value)) { + throw new \b8\Exception\HttpException('Value passed to ' . __FUNCTION__ . ' cannot be null.'); + } - $stmt = \b8\Database::getConnection($useConnection)->prepare('SELECT * FROM build WHERE id = :id LIMIT 1'); - $stmt->bindValue(':id', $value); + $query = 'SELECT * FROM build WHERE id = :id LIMIT 1'; + $stmt = \b8\Database::getConnection($useConnection)->prepare($query); + $stmt->bindValue(':id', $value); - if($stmt->execute()) - { - if($data = $stmt->fetch(\PDO::FETCH_ASSOC)) - { - return new \PHPCI\Model\Build($data); - } - } + if ($stmt->execute()) { + if ($data = $stmt->fetch(\PDO::FETCH_ASSOC)) { + return new \PHPCI\Model\Build($data); + } + } - return null; - } + return null; + } - public function getByProjectId($value, $limit = null, $useConnection = 'read') - { - if(is_null($value)) - { - throw new \b8\Exception\HttpException('Value passed to ' . __FUNCTION__ . ' cannot be null.'); - } + public function getByProjectId($value, $limit = null, $useConnection = 'read') + { + if (is_null($value)) { + throw new \b8\Exception\HttpException('Value passed to ' . __FUNCTION__ . ' cannot be null.'); + } - $add = ''; + $add = ''; - if($limit) - { - $add .= ' LIMIT ' . $limit; - } + if ($limit) { + $add .= ' LIMIT ' . $limit; + } - $stmt = \b8\Database::getConnection($useConnection)->prepare('SELECT COUNT(*) AS cnt FROM build WHERE project_id = :project_id' . $add); - $stmt->bindValue(':project_id', $value); + $query = 'SELECT COUNT(*) AS cnt FROM build WHERE project_id = :project_id' . $add; + $stmt = \b8\Database::getConnection($useConnection)->prepare($query); + $stmt->bindValue(':project_id', $value); - if($stmt->execute()) - { - $res = $stmt->fetch(\PDO::FETCH_ASSOC); - $count = (int)$res['cnt']; - } - else - { - $count = 0; - } + if ($stmt->execute()) { + $res = $stmt->fetch(\PDO::FETCH_ASSOC); + $count = (int)$res['cnt']; + } else { + $count = 0; + } - $stmt = \b8\Database::getConnection('read')->prepare('SELECT * FROM build WHERE project_id = :project_id' . $add); - $stmt->bindValue(':project_id', $value); + $query = 'SELECT * FROM build WHERE project_id = :project_id' . $add; + $stmt = \b8\Database::getConnection('read')->prepare($query); + $stmt->bindValue(':project_id', $value); - if($stmt->execute()) - { - $res = $stmt->fetchAll(\PDO::FETCH_ASSOC); + if ($stmt->execute()) { + $res = $stmt->fetchAll(\PDO::FETCH_ASSOC); - $rtn = array_map(function($item) - { - return new \PHPCI\Model\Build($item); - }, $res); + $map = function ($item) { + return new \PHPCI\Model\Build($item); + }; + $rtn = array_map($map, $res); - return array('items' => $rtn, 'count' => $count); - } - else - { - return array('items' => array(), 'count' => 0); - } - } + return array('items' => $rtn, 'count' => $count); + } else { + return array('items' => array(), 'count' => 0); + } + } - public function getByStatus($value, $limit = null, $useConnection = 'read') - { - if(is_null($value)) - { - throw new \b8\Exception\HttpException('Value passed to ' . __FUNCTION__ . ' cannot be null.'); - } + public function getByStatus($value, $limit = null, $useConnection = 'read') + { + if (is_null($value)) { + throw new \b8\Exception\HttpException('Value passed to ' . __FUNCTION__ . ' cannot be null.'); + } - $add = ''; + $add = ''; - if($limit) - { - $add .= ' LIMIT ' . $limit; - } + if ($limit) { + $add .= ' LIMIT ' . $limit; + } - $stmt = \b8\Database::getConnection($useConnection)->prepare('SELECT COUNT(*) AS cnt FROM build WHERE status = :status' . $add); - $stmt->bindValue(':status', $value); + $query = 'SELECT COUNT(*) AS cnt FROM build WHERE status = :status' . $add; + $stmt = \b8\Database::getConnection($useConnection)->prepare($query); + $stmt->bindValue(':status', $value); - if($stmt->execute()) - { - $res = $stmt->fetch(\PDO::FETCH_ASSOC); - $count = (int)$res['cnt']; - } - else - { - $count = 0; - } + if ($stmt->execute()) { + $res = $stmt->fetch(\PDO::FETCH_ASSOC); + $count = (int)$res['cnt']; + } else { + $count = 0; + } - $stmt = \b8\Database::getConnection('read')->prepare('SELECT * FROM build WHERE status = :status' . $add); - $stmt->bindValue(':status', $value); + $query = 'SELECT * FROM build WHERE status = :status' . $add; + $stmt = \b8\Database::getConnection('read')->prepare($query); + $stmt->bindValue(':status', $value); - if($stmt->execute()) - { - $res = $stmt->fetchAll(\PDO::FETCH_ASSOC); + if ($stmt->execute()) { + $res = $stmt->fetchAll(\PDO::FETCH_ASSOC); - $rtn = array_map(function($item) - { - return new \PHPCI\Model\Build($item); - }, $res); - - return array('items' => $rtn, 'count' => $count); - } - else - { - return array('items' => array(), 'count' => 0); - } - } + $map = function ($item) { + return new \PHPCI\Model\Build($item); + }; + $rtn = array_map($map, $res); + return array('items' => $rtn, 'count' => $count); + } else { + return array('items' => array(), 'count' => 0); + } + } } diff --git a/PHPCI/Store/Base/ProjectStoreBase.php b/PHPCI/Store/Base/ProjectStoreBase.php index cf152e03..9a807373 100644 --- a/PHPCI/Store/Base/ProjectStoreBase.php +++ b/PHPCI/Store/Base/ProjectStoreBase.php @@ -5,6 +5,7 @@ */ namespace PHPCI\Store\Base; + use b8\Store; /** @@ -12,36 +13,33 @@ use b8\Store; */ class ProjectStoreBase extends Store { - protected $_tableName = 'project'; - protected $_modelName = '\PHPCI\Model\Project'; - protected $_primaryKey = 'id'; + protected $tableName = 'project'; + protected $modelName = '\PHPCI\Model\Project'; + protected $primaryKey = 'id'; - public function getByPrimaryKey($value, $useConnection = 'read') - { - return $this->getById($value, $useConnection); - } + public function getByPrimaryKey($value, $useConnection = 'read') + { + return $this->getById($value, $useConnection); + } - public function getById($value, $useConnection = 'read') - { - if(is_null($value)) - { - throw new \b8\Exception\HttpException('Value passed to ' . __FUNCTION__ . ' cannot be null.'); - } + public function getById($value, $useConnection = 'read') + { + if (is_null($value)) { + throw new \b8\Exception\HttpException('Value passed to ' . __FUNCTION__ . ' cannot be null.'); + } - $stmt = \b8\Database::getConnection($useConnection)->prepare('SELECT * FROM project WHERE id = :id LIMIT 1'); - $stmt->bindValue(':id', $value); + $query = 'SELECT * FROM project WHERE id = :id LIMIT 1'; + $stmt = \b8\Database::getConnection($useConnection)->prepare($query); + $stmt->bindValue(':id', $value); - if($stmt->execute()) - { - if($data = $stmt->fetch(\PDO::FETCH_ASSOC)) - { - return new \PHPCI\Model\Project($data); - } - } - - return null; - } + if ($stmt->execute()) { + if ($data = $stmt->fetch(\PDO::FETCH_ASSOC)) { + return new \PHPCI\Model\Project($data); + } + } + return null; + } } diff --git a/PHPCI/Store/Base/UserStoreBase.php b/PHPCI/Store/Base/UserStoreBase.php index 4ddbf8a9..49c0ed35 100644 --- a/PHPCI/Store/Base/UserStoreBase.php +++ b/PHPCI/Store/Base/UserStoreBase.php @@ -5,6 +5,7 @@ */ namespace PHPCI\Store\Base; + use b8\Store; /** @@ -12,57 +13,52 @@ use b8\Store; */ class UserStoreBase extends Store { - protected $_tableName = 'user'; - protected $_modelName = '\PHPCI\Model\User'; - protected $_primaryKey = 'id'; + protected $tableName = 'user'; + protected $modelName = '\PHPCI\Model\User'; + protected $primaryKey = 'id'; - public function getByPrimaryKey($value, $useConnection = 'read') - { - return $this->getById($value, $useConnection); - } + public function getByPrimaryKey($value, $useConnection = 'read') + { + return $this->getById($value, $useConnection); + } - public function getById($value, $useConnection = 'read') - { - if(is_null($value)) - { - throw new \b8\Exception\HttpException('Value passed to ' . __FUNCTION__ . ' cannot be null.'); - } + public function getById($value, $useConnection = 'read') + { + if (is_null($value)) { + throw new \b8\Exception\HttpException('Value passed to ' . __FUNCTION__ . ' cannot be null.'); + } - $stmt = \b8\Database::getConnection($useConnection)->prepare('SELECT * FROM user WHERE id = :id LIMIT 1'); - $stmt->bindValue(':id', $value); + $query = 'SELECT * FROM user WHERE id = :id LIMIT 1'; + $stmt = \b8\Database::getConnection($useConnection)->prepare($query); + $stmt->bindValue(':id', $value); - if($stmt->execute()) - { - if($data = $stmt->fetch(\PDO::FETCH_ASSOC)) - { - return new \PHPCI\Model\User($data); - } - } + if ($stmt->execute()) { + if ($data = $stmt->fetch(\PDO::FETCH_ASSOC)) { + return new \PHPCI\Model\User($data); + } + } - return null; - } + return null; + } - public function getByEmail($value, $useConnection = 'read') - { - if(is_null($value)) - { - throw new \b8\Exception\HttpException('Value passed to ' . __FUNCTION__ . ' cannot be null.'); - } + public function getByEmail($value, $useConnection = 'read') + { + if (is_null($value)) { + throw new \b8\Exception\HttpException('Value passed to ' . __FUNCTION__ . ' cannot be null.'); + } - $stmt = \b8\Database::getConnection($useConnection)->prepare('SELECT * FROM user WHERE email = :email LIMIT 1'); - $stmt->bindValue(':email', $value); + $query = 'SELECT * FROM user WHERE email = :email LIMIT 1'; + $stmt = \b8\Database::getConnection($useConnection)->prepare($query); + $stmt->bindValue(':email', $value); - if($stmt->execute()) - { - if($data = $stmt->fetch(\PDO::FETCH_ASSOC)) - { - return new \PHPCI\Model\User($data); - } - } - - return null; - } + if ($stmt->execute()) { + if ($data = $stmt->fetch(\PDO::FETCH_ASSOC)) { + return new \PHPCI\Model\User($data); + } + } + return null; + } } diff --git a/PHPCI/Store/BuildStore.php b/PHPCI/Store/BuildStore.php index 46097d1f..5355fac2 100644 --- a/PHPCI/Store/BuildStore.php +++ b/PHPCI/Store/BuildStore.php @@ -9,8 +9,6 @@ namespace PHPCI\Store; -require_once(APPLICATION_PATH . 'PHPCI/Store/Base/BuildStoreBase.php'); - use PHPCI\Store\Base\BuildStoreBase; /** @@ -21,5 +19,5 @@ use PHPCI\Store\Base\BuildStoreBase; */ class BuildStore extends BuildStoreBase { - // This class has been left blank so that you can modify it - changes in this file will not be overwritten. -} \ No newline at end of file + // This class has been left blank so that you can modify it - changes in this file will not be overwritten. +} diff --git a/PHPCI/Store/ProjectStore.php b/PHPCI/Store/ProjectStore.php index b36e9b0e..c844d756 100644 --- a/PHPCI/Store/ProjectStore.php +++ b/PHPCI/Store/ProjectStore.php @@ -9,8 +9,6 @@ namespace PHPCI\Store; -require_once(APPLICATION_PATH . 'PHPCI/Store/Base/ProjectStoreBase.php'); - use PHPCI\Store\Base\ProjectStoreBase; /** @@ -21,5 +19,5 @@ use PHPCI\Store\Base\ProjectStoreBase; */ class ProjectStore extends ProjectStoreBase { - // This class has been left blank so that you can modify it - changes in this file will not be overwritten. -} \ No newline at end of file + // This class has been left blank so that you can modify it - changes in this file will not be overwritten. +} diff --git a/PHPCI/Store/UserStore.php b/PHPCI/Store/UserStore.php index f6ddb23f..1a45ed18 100644 --- a/PHPCI/Store/UserStore.php +++ b/PHPCI/Store/UserStore.php @@ -9,8 +9,6 @@ namespace PHPCI\Store; -require_once(APPLICATION_PATH . 'PHPCI/Store/Base/UserStoreBase.php'); - use PHPCI\Store\Base\UserStoreBase; /** @@ -21,5 +19,5 @@ use PHPCI\Store\Base\UserStoreBase; */ class UserStore extends UserStoreBase { - // This class has been left blank so that you can modify it - changes in this file will not be overwritten. -} \ No newline at end of file + // This class has been left blank so that you can modify it - changes in this file will not be overwritten. +}