Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 50 lines (47 sloc) 1.412 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
<?php
class Posts
{
    public function __construct(PDO $connection)
    {
        $this->connection = $connection;
    }

    /**
* @param int $threadId
* @param string $lastVisit Y-m-d format
* @return array fields for the selected post
*/
    public function lastPost($threadId, $lastVisit)
    {
        $stmt = $this->connection->prepare("SELECT * FROM posts WHERE id_thread = :id_thread AND date >= :date ORDER BY date");
        $stmt->bindValue(':id_thread', (int) $_GET['id_thread']);
        $stmt->bindValue(':date', $lastVisit);
        $stmt->execute();
        return $stmt->fetch();
    }
}
class LastPostAction
{
    public function __construct(Posts $posts, $template)
    {
        $this->posts = $posts;
        $this->template = $template;
    }

    public function execute(array $getParameters)
    {
        if (!isset($getParameters['last_visit'])) {
            $getParameters['last_visit'] = date('Y-m-d');
        }
        $post = $this->posts->lastPost($getParameters['id_thread'],
                                       $getParameters['last_visit']);
        require $this->template;
    }
}
$dsn = 'mysql:host=localhost;dbname=practical_php_refactoring';
$username = 'root';
$password = '';
$action = new LastPostAction(
                new Posts(new PDO($dsn, $username, $password)),
                'last_post.php'
          );
$action->execute($_GET);
?>
Something went wrong with that request. Please try again.