Introduce "order" and "limit" argument instead of "param" array for BaseRepository and make Repositories more Dependency Injectable

This commit is contained in:
Philipp Holzer 2020-01-19 23:22:30 +01:00
parent 26d6afd27f
commit b46b72ad3b
No known key found for this signature in database
GPG key ID: D8365C3D36B77D90
4 changed files with 59 additions and 39 deletions

View file

@ -63,13 +63,15 @@ abstract class BaseRepository extends BaseFactory
* Chainable.
*
* @param array $condition
* @param array $params
* @param array $order An optional array with order information
* @param int|array $limit Optional limit information
*
* @return BaseCollection
* @throws \Exception
*/
public function select(array $condition = [], array $params = [])
public function select(array $condition = [], array $order = [], $limit = null)
{
$models = $this->selectModels($condition, $params);
$models = $this->selectModels($condition, $order, $limit);
return new static::$collection_class($models);
}
@ -81,14 +83,15 @@ abstract class BaseRepository extends BaseFactory
* Chainable.
*
* @param array $condition
* @param array $params
* @param array $order
* @param int? $max_id
* @param int? $since_id
* @param int $limit
*
* @return BaseCollection
* @throws \Exception
*/
public function selectByBoundaries(array $condition = [], array $params = [], int $max_id = null, int $since_id = null, int $limit = self::LIMIT)
public function selectByBoundaries(array $condition = [], array $order = [], int $max_id = null, int $since_id = null, int $limit = self::LIMIT)
{
$condition = DBA::collapseCondition($condition);
@ -104,9 +107,7 @@ abstract class BaseRepository extends BaseFactory
$boundCondition[] = $since_id;
}
$params['limit'] = $limit;
$models = $this->selectModels($boundCondition, $params);
$models = $this->selectModels($boundCondition, $order, $limit);
$totalCount = DBA::count(static::$table_name, $condition);
@ -175,12 +176,24 @@ abstract class BaseRepository extends BaseFactory
/**
* @param array $condition Query condition
* @param array $params Additional query parameters
* @param array $order An optional array with order information
* @param int|array $limit Optional limit information
*
* @return BaseModel[]
* @throws \Exception
*/
protected function selectModels(array $condition, array $params = [])
protected function selectModels(array $condition, array $order = [], $limit = null)
{
$params = [];
if (!empty($order)) {
$params['order'] = $order;
}
if (!empty($limit)) {
$params['limit'] = $limit;
}
$result = $this->dba->select(static::$table_name, [], $condition, $params);
/** @var BaseModel $prototype */

View file

@ -3,11 +3,7 @@
namespace Friendica\Collection;
use Friendica\BaseCollection;
use Friendica\Model\Introduction;
/**
* @property Introduction[] $models
*/
class Introductions extends BaseCollection
{

View file

@ -35,26 +35,28 @@ class Introduction extends BaseRepository
/**
* @param array $condition
* @param array $params
* @param array $order An optional array with order information
* @param int|array $limit Optional limit information
*
* @return Collection\Introductions
* @throws \Exception
*/
public function select(array $condition = [], array $params = [])
public function select(array $condition = [], array $order = [], $limit = null)
{
return parent::select($condition, $params);
return parent::select($condition, $order, $limit);
}
/**
* @param array $condition
* @param array $params
* @param array $order
* @param int|null $max_id
* @param int|null $since_id
* @param int $limit
* @param int|array $limit
* @return Collection\Introductions
* @throws \Exception
*/
public function selectByBoundaries(array $condition = [], array $params = [], int $max_id = null, int $since_id = null, int $limit = self::LIMIT)
public function selectByBoundaries(array $condition = [], array $order = [], int $max_id = null, int $since_id = null, int $limit = self::LIMIT)
{
return parent::selectByBoundaries($condition, $params, $max_id, $since_id, $limit);
return parent::selectByBoundaries($condition, $order, $max_id, $since_id, $limit);
}
}

View file

@ -2,15 +2,14 @@
namespace Friendica\Repository;
use Friendica\BaseModel;
use Friendica\BaseRepository;
use Friendica\Collection;
use Friendica\Core\L10n;
use Friendica\Database\DBA;
use Friendica\DI;
use Friendica\Database\Database;
use Friendica\Model;
use Friendica\Model\Group;
use Friendica\Network\HTTPException;
use Friendica\Util\ACLFormatter;
use Psr\Log\LoggerInterface;
class PermissionSet extends BaseRepository
{
@ -20,6 +19,16 @@ class PermissionSet extends BaseRepository
protected static $collection_class = Collection\PermissionSets::class;
/** @var ACLFormatter */
private $aclFormatter;
public function __construct(Database $dba, LoggerInterface $logger, ACLFormatter $aclFormatter)
{
parent::__construct($dba, $logger);
$this->aclFormatter = $aclFormatter;
}
/**
* @param array $data
* @return Model\PermissionSet
@ -52,27 +61,29 @@ class PermissionSet extends BaseRepository
/**
* @param array $condition
* @param array $params
* @param array $order An optional array with order information
* @param int|array $limit Optional limit information
*
* @return Collection\PermissionSets
* @throws \Exception
*/
public function select(array $condition = [], array $params = [])
public function select(array $condition = [], array $order = [], $limit = null)
{
return parent::select($condition, $params);
return parent::select($condition, $order, $limit);
}
/**
* @param array $condition
* @param array $params
* @param array $order
* @param int|null $max_id
* @param int|null $since_id
* @param int $limit
* @param int|array $limit
* @return Collection\PermissionSets
* @throws \Exception
*/
public function selectByBoundaries(array $condition = [], array $params = [], int $max_id = null, int $since_id = null, int $limit = self::LIMIT)
public function selectByBoundaries(array $condition = [], array $order = [], int $max_id = null, int $since_id = null, int $limit = self::LIMIT)
{
return parent::selectByBoundaries($condition, $params, $max_id, $since_id, $limit);
return parent::selectByBoundaries($condition, $order, $max_id, $since_id, $limit);
}
/**
@ -93,12 +104,10 @@ class PermissionSet extends BaseRepository
string $deny_cid = null,
string $deny_gid = null
) {
$ACLFormatter = DI::aclFormatter();
$allow_cid = $ACLFormatter->sanitize($allow_cid);
$allow_gid = $ACLFormatter->sanitize($allow_gid);
$deny_cid = $ACLFormatter->sanitize($deny_cid);
$deny_gid = $ACLFormatter->sanitize($deny_gid);
$allow_cid = $this->aclFormatter->sanitize($allow_cid);
$allow_gid = $this->aclFormatter->sanitize($allow_gid);
$deny_cid = $this->aclFormatter->sanitize($deny_cid);
$deny_gid = $this->aclFormatter->sanitize($deny_gid);
// Public permission
if (!$allow_cid && !$allow_gid && !$deny_cid && !$deny_gid) {
@ -134,7 +143,7 @@ class PermissionSet extends BaseRepository
public function selectByContactId($contact_id, $uid)
{
$groups = [];
if (DBA::exists('contact', ['id' => $contact_id, 'uid' => $uid, 'blocked' => false])) {
if ($this->dba->exists('contact', ['id' => $contact_id, 'uid' => $uid, 'blocked' => false])) {
$groups = Group::getIdsByContactId($contact_id);
}