Skip to content

Instantly share code, notes, and snippets.

@beberlei
Created February 10, 2015 19:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save beberlei/8b23160dd0b4466fe1c5 to your computer and use it in GitHub Desktop.
Save beberlei/8b23160dd0b4466fe1c5 to your computer and use it in GitHub Desktop.
<?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