Create a gist now

Instantly share code, notes, and snippets.

@beberlei /hints.php Secret
Created Feb 10, 2015

What would you like to do?
<?php
declare(strict_types=1);
class UserController
{
const STATUS_ACTIVE = 1;
const STATUS_INACTIVE = 0;
private $service;
public function __construct()
{
$this->service = new UserService();
}
public function listAction(Request $request)
{
$status = (int)$request->get('status'); // this is a string
return [
'users' => $this->service->fetchUsers($status),
'total' => $this->service->fetchTotalCount($status)
];
}
}
class UserService
{
private $connection;
public function __construct()
{
$this->connection = new Connection();
}
public function fetchUsers(int $status): array
{
$sql = 'SELECT u.id, u.username FROM users u WHERE u.status = ? LIMIT 10';
return $this->connection->fetchAll($sql, [$status]);
}
public function fetchTotalCount(int $status): int
{
$sql = 'SELECT count(*) FROM users u WHERE u.status = ?';
return $this->connection->fetchColumn($sql, [$status]);
}
}
class Connection
{
public function fetchAll()
{
return [ ['id' => 1, 'username' => 'beberlei'] ];
}
public function fetchColumn()
{
return "17";
}
}
class Request
{
public function get($name)
{
if ($name === 'status') {
return "1";
}
}
}
$controller = new UserController();
$result = $controller->listAction(new Request());
var_dump($result);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment