Advertisement
Guest User

Untitled

a guest
Apr 30th, 2017
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.36 KB | None | 0 0
  1. <?php
  2.  
  3. class UserRepository implements UserRepositoryInterface
  4. {
  5.     private $_db;
  6.    
  7.     private $_mysql_host = 'localhost';
  8.     private $_port = '3306';
  9.     private $_username = 'root';
  10.     private $_password = '';
  11.     private $_database = 'reservationsystem';
  12.    
  13.     public function UserRepository()
  14.     {
  15.         try
  16.         {
  17.             $this->_db = new PDO('mysql:host='.$this->_mysql_host.';dbname='.$this->_database.';port='.$this->_port, $this->_username, $this->_password);
  18.             $this->_db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  19.         }
  20.         catch(PDOException $e)
  21.         {
  22.             echo 'Połączenie nie mogło zostać nawiązane.<br>' . $e->getMessage();
  23.         }
  24.     }
  25.    
  26.     public function getUser(User $user)
  27.     {
  28.         try
  29.         {
  30.             $query = $this->_db->prepare('SELECT id FROM `Users` WHERE `fullname` = :fullname AND `login` = :login AND `password` = :password AND `phone` = :phone AND `email` = :email');
  31.             @$query->bindParam(':fullname', $user->getFullName(), PDO::PARAM_STR);
  32.             @$query->bindParam(':login', $user->getLogin(), PDO::PARAM_STR);
  33.             @$query->bindParam(':password', $user->getPassword(), PDO::PARAM_STR);
  34.             @$query->bindParam(':phone', $user->getPhone(), PDO::PARAM_STR);
  35.             @$query->bindParam(':email', $user->getEmail(), PDO::PARAM_STR);
  36.             $query->execute();
  37.            
  38.             return $query->fetch();
  39.         }
  40.         catch (PDOException $e)
  41.         {
  42.             echo $e->getMessage();
  43.         }
  44.     }
  45.    
  46.     public function getUserId(User $user)
  47.     {
  48.         return $this->getUser($user)['id'];
  49.     }
  50.    
  51.     public function getByFullname($fullname)
  52.     {
  53.        
  54.     }
  55.    
  56.     public function getById($id)
  57.     {
  58.        
  59.     }
  60.    
  61.     public function exists(User $user)
  62.     {
  63.         if (empty($this->getUser($user)))
  64.             return false;
  65.         else
  66.             return true;
  67.     }
  68.    
  69.     public function save(User $user)
  70.     {
  71.         try
  72.         {
  73.             if ($this->exists($user))
  74.                 return;
  75.            
  76.             $query = $this->_db->prepare('INSERT INTO `Users` (`fullname`,`login`,`password`,`phone`,`email`) VALUES(:fullname, :login, :password, :phone, :email)');
  77.             @$query->bindParam(':fullname', $user->getFullName(), PDO::PARAM_STR);
  78.             @$query->bindParam(':login', $user->getLogin(), PDO::PARAM_STR);
  79.             @$query->bindParam(':password', $user->getPassword(), PDO::PARAM_STR);
  80.             @$query->bindParam(':phone', $user->getPhone(), PDO::PARAM_STR);
  81.             @$query->bindParam(':email', $user->getEmail(), PDO::PARAM_STR);
  82.             $query->execute();
  83.         }
  84.         catch(PDOException $e)
  85.         {
  86.             echo 'Wystąpił błąd przy dodawaniu użytkownika <br>' .$e->getMessage();
  87.         }
  88.     }
  89.    
  90.     public function remove(User $user)
  91.     {
  92.         try
  93.         {
  94.             if (!$this->exists($user))
  95.                 return;
  96.  
  97.             $query = $this->_db->prepare('DELETE FROM `Users` WHERE `id` = :id');
  98.             @$query->bindParam(':id', $user->getId(), PDO::PARAM_INT);
  99.             $query->execute();
  100.         }
  101.         catch(PDOException $e)
  102.         {
  103.             echo 'Wystąpił błąd przy usuwaniu użytkownika <br>' .$e->getMessage();
  104.         }
  105.     }
  106. }
  107.  
  108. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement