Advertisement
Guest User

LegacyApp

a guest
Mar 22nd, 2018
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.93 KB | None | 0 0
  1. <?php
  2.  
  3. require_once("todo_conf.php");
  4.  
  5. // Caching Service
  6. class TodoCache {
  7.     private $cache;
  8.  
  9.     public function __construct() { $this->cache = array(); }
  10.     public function set($key, $val) { $this->cache[$key] = $val; }
  11.     public function get($key) { return $this->cache[$key]; }
  12.     public function hasEntry($key) { return isset($this->cache[$key]); }
  13. }
  14.  
  15. // Todo_Backend
  16. class Todo {
  17.     private $list;
  18.  
  19.     public function __construct() {
  20.         $this->dbConn = $this->getDbConn();
  21.         $this->cache = new TodoCache();
  22.     }
  23.  
  24.     private function logger($log_msg) {
  25.         echo $log_msg;
  26.     }
  27.  
  28.     private function getDbConn() {
  29.         $db = new PDO("pgsql:host=".DBHOST.";port=".DBPORT."; dbname='".DBNAME."'; user=".DBUSER." password=".DBPASS);
  30.         $db->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
  31.         return $db;
  32.     }
  33.  
  34.     private function getOwnerId($owner) {
  35.         $stmt = $this->dbConn->prepare("SELECT id FROM owners WHERE owner = :owner");
  36.         $stmt->execute(array(':owner'=>$owner));
  37.         return $stmt->fetchColumn(\PDO::FETCH_ASSOC);
  38.     }
  39.  
  40.     private function addItem($owner_id, $item) {
  41.         $this->logger("Adding $item for owner with id $owner_id\n");
  42.         $stmt = $this->dbConn->prepare("INSERT INTO todo (owner_id, item) VALUES (:owner_id,
  43.             :item)");
  44.         $stmt->execute(array(':owner_id'=>$owner_id, ':item'=>$item));
  45.         return $stmt->fetch(\PDO::FETCH_ASSOC);
  46.     }
  47.  
  48.     private function createOwnerAddItem($owner, $item) {
  49.         // entire body done in a transaction of course
  50.         $this->logger("Add $owner to the owner's list\n");
  51.         $stmt = $this->dbConn->prepare("INSERT INTO owners (owner) VALUES (:owner) RETURNING id");
  52.         $stmt->execute(array(':owner'=>$owner));
  53.         $res = $stmt->fetch(\PDO::FETCH_ASSOC);
  54.         $owner_id = $res['id'];
  55.         $this->addItem($owner_id, $item);
  56.     }
  57.  
  58.     public function add($owner, $item) {
  59.         $owner_id = $this->getOwnerId($owner);
  60.         if ($owner_id === false || is_null($owner_id)) {
  61.             $this->createOwnerAddItem($owner, $item);
  62.         } else {
  63.             $this->addItem($owner_id, $item);
  64.         }
  65.     }
  66.  
  67.     private function getOwnersItems($owner) {
  68.         if ($this->cache->hasEntry($owner)) {
  69.             return $this->cache->get($owner);
  70.         }
  71.         $stmt = $this->dbConn->prepare("SELECT item FROM todo t
  72.         JOIN owners o ON o.id = t.owner_id WHERE o.owner = :owner");
  73.         $stmt->execute(array(':owner'=>$owner));
  74.         $res = $stmt->fetchAll(\PDO::FETCH_ASSOC);
  75.         $this->cache->set($owner, $res);
  76.         return $res;
  77.     }
  78.  
  79.     public function display($owner) {
  80.         $rows = $this->getOwnersItems($owner);
  81.         foreach ($rows as $row) {
  82.             echo "$owner -> ${row['item']}\n";
  83.         }
  84.     }
  85.  
  86. }
  87.  
  88. // Consumer, Frontend/Controller
  89. $c = new Todo();
  90. $c->add('Adam', 'Buy bread');
  91. $c->add('Adam', 'Buy butter');
  92. $c->add('Jane', 'Visit Paris');
  93. $c->add('Jane', 'Fix car');
  94. $c->add('Adam', 'Fix Bike');
  95. $c->display('Jane');
  96. $c->display('Adam');
  97. $c->add('Adam', 'Yoga');
  98. $c->display('Adam');
  99.  
  100.  
  101. class TodoRestAPI {
  102.     public function POST_add($owner, $item) { }
  103.     public function GET_items($owner) { }
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement