Skip to content

Commit

Permalink
Update to handle deactivating a site between actives.
Browse files Browse the repository at this point in the history
  • Loading branch information
colinmo committed Aug 16, 2023
1 parent 73a9eb7 commit 34fbf16
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 36 deletions.
8 changes: 4 additions & 4 deletions src/app/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,8 @@ public function random(ServerRequestInterface $request, array $args): ResponseIn
public function next(ServerRequestInterface $request, array $args): ResponseInterface
{
if (isset($request->getHeader('referer')[0])) {
$site = $this->site->nextActive($request->getHeader('referer')[0]);
return $this->response->withHeader('Location', $site['url'])->withStatus(302);
$siteURL = $this->site->nextActive($request->getHeader('referer')[0]);
return $this->response->withHeader('Location', $siteURL)->withStatus(302);
}
return $this->random($request, $args);
}
Expand All @@ -127,8 +127,8 @@ public function next(ServerRequestInterface $request, array $args): ResponseInte
public function previous(ServerRequestInterface $request, array $args): ResponseInterface
{
if (isset($request->getHeader('referer')[0])) {
$site = $this->site->previousActive($request->getHeader('referer')[0]);
return $this->response->withHeader('Location', $site['url'])->withStatus(302);
$siteURL = $this->site->previousActive($request->getHeader('referer')[0]);
return $this->response->withHeader('Location', $siteURL)->withStatus(302);
}
return $this->random($request, $args);
}
Expand Down
53 changes: 39 additions & 14 deletions src/app/Model/Site.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,47 +62,47 @@ public function randomActive(): array
* Return the active site that's the previous one to the specfied referrer
*
* @param string $referrer URL the previous link was clicked on
* @return array
* @return string
* @throws PDOException
*/
public function previousActive(string $referrer): array
public function previousActive(string $referrer): string
{
$query = $this->db->prepare("SELECT previous as url from Sites WHERE :referrer like url||'%' AND active = 1 ORDER BY length(url) LIMIT 1");
$query = $this->db->prepare("SELECT previous from Sites WHERE :referrer like url||'%' AND active = 1 ORDER BY length(url) LIMIT 1");
if ($query->execute([$referrer])) {
$return = $query->fetch();
if ($return) {
return $return;
return $return['previous'];
}
}
return ['url' => '/'];
return '/';
}

/**
* Return the active site that's the next one to the specfied referrer
*
* @param string $referrer URL the next link was clicked on
* @return array
* @return string
* @throws PDOException
*/
public function nextActive(string $referrer): mixed
public function nextActive(string $referrer): string
{
$query = $this->db->prepare("SELECT next as url from Sites WHERE :referrer like url||'%' AND active = 1 ORDER BY length(url) LIMIT 1");
$query = $this->db->prepare("SELECT next from Sites WHERE :referrer like url||'%' AND active = 1 ORDER BY length(url) LIMIT 1");
if ($query->execute([$referrer])) {
$return = $query->fetch();
if ($return) {
return $return;
return $return['next'];
}
}
return ['url' => '/'];
return '/';
}

/**
* Return ALL Sites in the Sites table
*
* @return array|false
* @return array
* @throws PDOException
*/
public function all(): array|false
public function all(): array
{
$query = $this->db->prepare('SELECT * FROM Sites');
return ($query->execute() ? $query->fetchAll() : []);
Expand All @@ -114,10 +114,35 @@ public function getActiveSitesWithProfiles(): array|false
return ($query->execute() ? $query->fetchAll() : []);
}

// Updated to remove from ring if inactive, or add back to ring if active
public function setActive(String $url, bool $active)
{
$query = $this->db->prepare('UPDATE Sites SET active = :active WHERE url = :url');
return $query->execute([$active, $url]);
$existing = $this->getSite($url, false);
if (!!$existing['active'] == !!$active) {
// No change
return true;
}
$this->db->beginTransaction();
if ($active) {
// Add back to ring
$target = $this->randomActive();
$query = $this->db->prepare('UPDATE Sites SET active = :active, previous = :prev, next = :next WHERE url = :url');
$query->execute([true, $target['url'], $target['next'], $url]);
$query = $this->db->prepare('UPDATE sites SET next = :url WHERE url = :orig');
$query->execute([$url, $target['url']]);
$query = $this->db->prepare('UPDATE sites SET previous = :url WHERE url = :orig');
$query->execute([$url, $target['next']]);
} else {
// Remove from ring
$query = $this->db->prepare('UPDATE Sites SET active = :active, previous = :prev, next = :next WHERE url = :url');
$ok = $query->execute([false, '', '', $url]);
$query = $this->db->prepare('UPDATE sites SET next = :url WHERE url = :orig');
$query->execute([$existing['next'], $existing['previous']]);
$query = $this->db->prepare('UPDATE sites SET previous = :url WHERE url = :orig');
$query->execute([$existing['previous'], $existing['next']]);
}

return $this->db->commit();
}

public function setProfile(String $url, $card)
Expand Down
44 changes: 26 additions & 18 deletions tests/app/Model/SiteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,30 +80,23 @@ public function testRandomGetsResult(): void
public function testPreviousGetsResult(): void
{

$site = new App\Model\Site(
$this->db
);
$site = new App\Model\Site($this->db);
$one = $site->previousActive('https://lapse.nerdvana.org.au/index.html');
$this->assertIsArray($one);
$this->assertArrayHasKey('url', $one);
$this->assertEquals($one['url'], 'https://grift.com/');
$this->assertIsString($one);
$this->assertEquals($one, 'https://grift.com/');
}

public function testNextGetsResult(): void
{
$site = new App\Model\Site(
$this->db
);
$site = new App\Model\Site($this->db);
$this->assertSame('App\Model\Site', get_class($site));
$one = $site->nextActive('https://lapse.nerdvana.org.au/index.html');
$this->assertIsArray($one);
$this->assertArrayHasKey('url', $one);
$this->assertEquals($one['url'], 'https://vonexplaino.com/');
$this->assertIsString($one);
$this->assertEquals($one, 'https://vonexplaino.com/');

$one = $site->nextActive('https://vonexplaino.com/bob/builder');
$this->assertIsArray($one);
$this->assertArrayHasKey('url', $one);
$this->assertEquals($one['url'], 'https://grift.com/');
$this->assertIsString($one);
$this->assertEquals($one, 'https://grift.com/');
}

public function testAddSiteSlotsIn(): void
Expand All @@ -123,6 +116,21 @@ public function testAddSiteSlotsIn(): void
$this->assertSame(count($bob), 5);
}

public function testDeactivateSite(): void
{
$site = new App\Model\Site($this->db);
$result = $site->setActive('https://lapse.nerdvana.org.au/', false);
$this->assertTrue($result);
$x = $site->getSite('https://lapse.nerdvana.org.au/');
$this->assertTrue(!$x['active']);
$this->assertEquals($x['next'], null);
$x = $site->getSite('https://vonexplaino.com/');
$this->assertEquals($x['next'], 'https://grift.com/');
$x = $site->getSite('https://grift.com/');
$this->assertEquals($x['previous'],'https://vonexplaino.com/');

}

public function testNonexistantSite(): void
{
$site = new App\Model\Site($this->db);
Expand All @@ -141,14 +149,14 @@ public function testNonexistantPrevious(): void
{
$site = new App\Model\Site($this->db);
$result = $site->previousActive('https://why.here.com');
$this->assertSame($result, ['url' => '/']);
$this->assertSame($result, '/');
}

public function testNonexistantNext(): void
{
$site = new App\Model\Site($this->db);
$result = $site->nextActive('https://why.here.com');
$this->assertSame($result, ['url' => '/']);
$this->assertSame($result, '/');
}

public function testUnchecked(): void
Expand All @@ -168,7 +176,7 @@ public function testActiveSetOk(): void
$result = $site->setActive('https://no.com/', false);
$this->assertTrue($result);
$asite = $site->getSite('https://no.com/');
$this->assertSame($asite['active'], '');
$this->assertTrue(!$asite['active']);
}

public function testAllNoDb()
Expand Down

0 comments on commit 34fbf16

Please sign in to comment.