Skip to content

Commit

Permalink
Update class definitions to use the namespaced versions
Browse files Browse the repository at this point in the history
Signed-off-by: Roland Dalmulder <contact@rolandd.com>
  • Loading branch information
roland-d committed Aug 3, 2023
1 parent b74c149 commit d86335e
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 46 deletions.
Expand Up @@ -10,6 +10,7 @@
namespace PatchTester\Model;

use Joomla\CMS\Factory;
use Joomla\Database\DatabaseDriver;
use Joomla\Registry\Registry;

/**
Expand All @@ -22,7 +23,7 @@ abstract class AbstractModel
/**
* The database driver.
*
* @var \JDatabaseDriver
* @var DatabaseDriver
* @since 4.0.0
*/
protected $db;
Expand All @@ -33,15 +34,16 @@ abstract class AbstractModel
* @since 4.0.0
*/
protected $state;
/**

/**
* Instantiate the model.
*
* @param Registry $state The model state.
* @param \JDatabaseDriver $db The database adpater.
* @param Registry|null $state The model state.
* @param DatabaseDriver|null $db The database adapter.
*
* @since 4.0.0
*/
public function __construct(Registry $state = null, \JDatabaseDriver $db = null)
public function __construct(Registry $state = null, DatabaseDriver $db = null)
{
$this->state = $state ?: new Registry();
$this->db = $db ?: Factory::getDbo();
Expand All @@ -50,7 +52,7 @@ public function __construct(Registry $state = null, \JDatabaseDriver $db = null)
/**
* Get the database driver.
*
* @return \JDatabaseDriver
* @return DatabaseDriver
*
* @since 4.0.0
*/
Expand All @@ -74,13 +76,13 @@ public function getState()
/**
* Set the database driver.
*
* @param \JDatabaseDriver $db The database driver.
* @param DatabaseDriver $db The database driver.
*
* @return void
*
* @since 4.0.0
*/
public function setDb(\JDatabaseDriver $db)
public function setDb(DatabaseDriver $db)
{
$this->db = $db;
}
Expand Down
Expand Up @@ -16,6 +16,7 @@
use Joomla\CMS\Http\HttpFactory;
use Joomla\CMS\Language\Text;
use Joomla\CMS\Version;
use Joomla\Database\DatabaseDriver;
use Joomla\Filesystem\File;
use Joomla\Filesystem\Folder;
use Joomla\Registry\Registry;
Expand Down Expand Up @@ -88,11 +89,11 @@ class PullModel extends AbstractModel
* Instantiate the model.
*
* @param Registry $state The model state.
* @param \JDatabaseDriver $db The database adpater.
* @param DatabaseDriver $db The database adpater.
*
* @since 4.0.0
*/
public function __construct(Registry $state = null, \JDatabaseDriver $db = null)
public function __construct(Registry $state = null, DatabaseDriver $db = null)
{
parent::__construct($state, $db);
$this->namespaceMapper = new \JNamespacePsr4Map();
Expand All @@ -113,7 +114,7 @@ public function __construct(Registry $state = null, \JDatabaseDriver $db = null)
public function apply(int $id): bool
{
$params = ComponentHelper::getParams('com_patchtester');
// Decide based on repository settings whether patch will be applied through Github or CIServer
// Decide based on repository settings whether patch will be applied through Github or CIServer
if ((bool) $params->get('ci_switch', 0)) {
return $this->applyWithCIServer($id);
}
Expand Down
Expand Up @@ -13,8 +13,10 @@
use Joomla\CMS\HTML\HTMLHelper;
use Joomla\CMS\Language\Text;
use Joomla\CMS\MVC\Model\ListModel;
use Joomla\Database\DatabaseQuery;
use PatchTester\GitHub\Exception\UnexpectedResponse;
use PatchTester\Helper;
use RuntimeException;

// phpcs:disable PSR1.Files.SideEffects
\defined('_JEXEC') or die;
Expand All @@ -40,7 +42,7 @@ class PullsModel extends ListModel
* @var array
* @since 2.0
*/
protected $sortFields = array('pulls.pull_id', 'pulls.title');
protected $sortFields = ['pulls.pull_id', 'pulls.title'];
/**
* Constructor.
*
Expand Down Expand Up @@ -86,7 +88,7 @@ public function getItems()
$this->getStart(),
$this->getState()->get('list.limit')
);
$db = $this->getDbo();
$db = $this->getDatabase();
$query = $db->getQuery(true)
->select($db->quoteName(['name', 'color']))
->from($db->quoteName('#__patchtester_pulls_labels'));
Expand Down Expand Up @@ -127,18 +129,18 @@ protected function getStoreId($id = '')
/**
* Gets an array of objects from the results of database query.
*
* @param \JDatabaseQuery|string $query The query.
* @param integer $limitstart Offset.
* @param integer $limit The number of records.
* @param DatabaseQuery|string $query The query.
* @param integer $limitstart Offset.
* @param integer $limit The number of records.
*
* @return array An array of results.
*
* @since 2.0
* @throws RuntimeException
*/
protected function getList($query, $limitstart = 0, $limit = 0)
protected function getList($query, int $limitstart = 0, int $limit = 0): array
{
return $this->getDbo()->setQuery($query, $limitstart, $limit)
return $this->getDatabase()->setQuery($query, $limitstart, $limit)
->loadObjectList();
}

Expand All @@ -147,11 +149,11 @@ protected function getList($query, $limitstart = 0, $limit = 0)
*
* This method ensures that the query is constructed only once for a given state of the model.
*
* @return \JDatabaseQuery A JDatabaseQuery object
* @return DatabaseQuery A DatabaseQuery object
*
* @since 2.0
*/
protected function getListQueryCache()
protected function getListQueryCache(): DatabaseQuery
{
// Capture the last store id used.
static $lastStoreId;
Expand All @@ -167,15 +169,15 @@ protected function getListQueryCache()
}

/**
* Method to get a JDatabaseQuery object for retrieving the data set from a database.
* Method to get a DatabaseQuery object for retrieving the data set from a database.
*
* @return \JDatabaseQuery A JDatabaseQuery object to retrieve the data set.
* @return DatabaseQuery A DatabaseQuery object to retrieve the data set.
*
* @since 2.0
*/
protected function getListQuery()
{
$db = $this->getDbo();
$db = $this->getDatabase();
$query = $db->getQuery(true);
$labelQuery = $db->getQuery(true);
$query->select('pulls.*')
Expand Down Expand Up @@ -270,35 +272,36 @@ protected function getListQuery()
*
* @since 2.0
*/
public function getSortFields()
public function getSortFields(): array
{
return $this->sortFields;
}

/**
* Method to request new data from GitHub
*
* @param integer $page The page of the request
* @param int $page The page of the request
*
* @return array
*
* @throws RuntimeException
* @since 2.0
* @throws \RuntimeException
*/
public function requestFromGithub($page)
public function requestFromGithub(int $page): array
{
if ($page === 1) {
$this->getDbo()->truncateTable('#__patchtester_pulls');
$this->getDbo()->truncateTable('#__patchtester_pulls_labels');
$this->getDatabase()->truncateTable('#__patchtester_pulls');
$this->getDatabase()->truncateTable('#__patchtester_pulls_labels');
}

try {
// TODO - Option to configure the batch size
// TODO - Option to configure the batch size
$batchSize = 100;
$pullsResponse = Helper::initializeGithub()->getOpenPulls($this->getState()->get('github_user'), $this->getState()->get('github_repo'), $page, $batchSize);
$pulls = json_decode($pullsResponse->body);
} catch (UnexpectedResponse $exception) {
throw new \RuntimeException(Text::sprintf('COM_PATCHTESTER_ERROR_GITHUB_FETCH', $exception->getMessage()), $exception->getCode(), $exception);
throw new RuntimeException(
Text::sprintf('COM_PATCHTESTER_ERROR_GITHUB_FETCH', $exception->getMessage()), $exception->getCode(), $exception);
}

// If this is page 1, let's check to see if we need to paginate
Expand Down Expand Up @@ -354,20 +357,20 @@ public function requestFromGithub($page)

$labels[] = implode(',', [
(int) $pull->number,
$this->getDbo()->quote($label->name),
$this->getDbo()->quote($label->color),
$this->getDatabase()->quote($label->name),
$this->getDatabase()->quote($label->color),
]);
}

// Build the data object to store in the database
$pullData = [
(int) $pull->number,
$this->getDbo()->quote(HTMLHelper::_('string.truncate', ($pull->title ?? ''), 150)),
$this->getDbo()->quote(HTMLHelper::_('string.truncate', ($pull->body ?? ''), 100)),
$this->getDbo()->quote($pull->html_url),
$this->getDatabase()->quote(HTMLHelper::_('string.truncate', ($pull->title ?? ''), 150)),
$this->getDatabase()->quote(HTMLHelper::_('string.truncate', ($pull->body ?? ''), 100)),
$this->getDatabase()->quote($pull->html_url),
(int) $isRTC,
(int) $isNPM,
$this->getDbo()->quote($branch),
$this->getDatabase()->quote($branch),
($pull->draft ? 1 : 0)
];
$data[] = implode(',', $pullData);
Expand All @@ -379,25 +382,26 @@ public function requestFromGithub($page)
}

try {
$this->getDbo()->setQuery($this->getDbo()->getQuery(true)
$this->getDatabase()->setQuery($this->getDatabase()->getQuery(true)
->insert('#__patchtester_pulls')
->columns(['pull_id', 'title', 'description', 'pull_url',
'is_rtc', 'is_npm', 'branch', 'is_draft'])
->values($data));
$this->getDbo()->execute();
} catch (\RuntimeException $exception) {
throw new \RuntimeException(Text::sprintf('COM_PATCHTESTER_ERROR_INSERT_DATABASE', $exception->getMessage()), $exception->getCode(), $exception);
$this->getDatabase()->execute();
} catch (RuntimeException $exception) {
throw new RuntimeException(
Text::sprintf('COM_PATCHTESTER_ERROR_INSERT_DATABASE', $exception->getMessage()), $exception->getCode(), $exception);
}

if ($labels) {
try {
$this->getDbo()->setQuery($this->getDbo()->getQuery(true)
$this->getDatabase()->setQuery($this->getDatabase()->getQuery(true)
->insert('#__patchtester_pulls_labels')
->columns(['pull_id', 'name', 'color'])
->values($labels));
$this->getDbo()->execute();
} catch (\RuntimeException $exception) {
throw new \RuntimeException(
$this->getDatabase()->execute();
} catch (RuntimeException $exception) {
throw new RuntimeException(
Text::sprintf(
'COM_PATCHTESTER_ERROR_INSERT_DATABASE',
$exception->getMessage()
Expand All @@ -424,6 +428,6 @@ public function requestFromGithub($page)
*/
public function truncateTable()
{
$this->getDbo()->truncateTable('#__patchtester_pulls');
$this->getDatabase()->truncateTable('#__patchtester_pulls');
}
}

0 comments on commit d86335e

Please sign in to comment.