Skip to content

Commit

Permalink
Split methods into separate setters and getters
Browse files Browse the repository at this point in the history
  • Loading branch information
christeredvartsen committed Aug 26, 2021
1 parent e619a2d commit 6fabf2f
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 53 deletions.
53 changes: 14 additions & 39 deletions src/Auth/AccessControl/AbstractQuery.php
Original file line number Diff line number Diff line change
@@ -1,53 +1,28 @@
<?php
<?php declare(strict_types=1);
namespace Imbo\Auth\AccessControl;

/**
* Abstract query interface for access control
*/
abstract class AbstractQuery {
/**
* Limit
*
* @var int
*/
private $limit = 20;

/**
* Page
*
* @var int
*/
private $page = 1;

/**
* Set or get the limit
*
* @param int $limit The limit to set. Skip to get the current value
* @return self|int
*/
public function limit($limit = null) {
if ($limit === null) {
return $this->limit;
}

$this->limit = (int) $limit;
private int $limit = 20;
private int $page = 1;

public function setLimit(int $limit): self {
$this->limit = $limit;
return $this;
}

/**
* Set or get the page
*
* @param int $page The page to set. Skip to get the current value
* @return self|int
*/
public function page($page = null) {
if ($page === null) {
return $this->page;
}

$this->page = (int) $page;
public function getLimit(): int {
return $this->limit;
}

public function setPage(int $page): self {
$this->page = $page;
return $this;
}

public function getPage(): int {
return $this->page;
}
}
4 changes: 2 additions & 2 deletions src/Auth/AccessControl/Adapter/ArrayAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ public function __construct(array $accessList = [], $groups = []) {
public function getGroups(GroupQuery $query, GroupsModel $model): array {
$model->setHits(count($this->groups));

$offset = ($query->page() - 1) * $query->limit();
return array_slice($this->groups, $offset, $query->limit(), true);
$offset = ($query->getPage() - 1) * $query->getLimit();
return array_slice($this->groups, $offset, $query->getLimit(), true);
}

public function groupExists(string $groupName): bool {
Expand Down
4 changes: 2 additions & 2 deletions src/Auth/AccessControl/Adapter/MongoDB.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,8 @@ public function __construct(array $params = null, MongoClient $client = null, Mo
public function getGroups(GroupQuery $query, GroupsModel $model): array {
$cursor = $this->getGroupsCollection()
->find([], [
'skip' => ($query->page() - 1) * $query->limit(),
'limit' => $query->limit(),
'skip' => ($query->getPage() - 1) * $query->getLimit(),
'limit' => $query->getLimit(),
]);

$groups = [];
Expand Down
4 changes: 2 additions & 2 deletions tests/Auth/AccessControl/Adapter/ArrayAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,8 @@ public function getGroupsData() : array {
['g1' => [], 'g2' => [], 'g3' => [], 'g4' => [], 'g5' => []],
['g3' => [], 'g4' => []],
(new GroupQuery())
->page(2)
->limit(2),
->setPage(2)
->setLimit(2),
],
];
}
Expand Down
18 changes: 10 additions & 8 deletions tests/Auth/AccessControl/GroupQueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,22 @@ public function setUp() : void {
}

/**
* @covers ::limit
* @covers ::setLimit
* @covers ::getLimit
*/
public function testSetAndGetLimit() : void {
$this->assertSame(20, $this->query->limit());
$this->assertSame($this->query, $this->query->limit(10));
$this->assertSame(10, $this->query->limit());
$this->assertSame(20, $this->query->getLimit());
$this->assertSame($this->query, $this->query->setLimit(10));
$this->assertSame(10, $this->query->getLimit());
}

/**
* @covers ::page
* @covers ::setPage
* @covers ::getPage
*/
public function testSetAndGetPage() : void {
$this->assertSame(1, $this->query->page());
$this->assertSame($this->query, $this->query->page(2));
$this->assertSame(2, $this->query->page());
$this->assertSame(1, $this->query->getPage());
$this->assertSame($this->query, $this->query->setPage(2));
$this->assertSame(2, $this->query->getPage());
}
}

0 comments on commit 6fabf2f

Please sign in to comment.