Added addUser function to repository

This commit is contained in:
Kevin Frantz 2018-07-15 09:51:48 +02:00
parent 2e1394f96a
commit 80be8e7361
6 changed files with 53 additions and 9 deletions

View File

@ -54,6 +54,15 @@ final class User implements UserInterface
{
$this->passwordHash = $hash;
}
/**
* In a real application you should use a salt ;)
* @param string $password
*/
public function setPasswordHashByPassword(string $password): void
{
$this->passwordHash = password_hash($password, PASSWORD_BCRYPT);
}
public function getEmail(): string
{

View File

@ -18,6 +18,8 @@ interface UserInterface extends UniqueInterface
*/
public function setPasswordHash(string $hash):void;
public function setPasswordHashByPassword(string $password): void;
/**
* @return string
*/

View File

@ -0,0 +1,28 @@
<?php
namespace repository\user;
use repository\AbstractRepository;
use entity\user\UserInterface as UserEntityInterface;
/**
*
* @author kevinfrantz
*
*/
final class User extends AbstractRepository implements UserInterface
{
public function addUser(UserEntityInterface $user): void
{
$statement = $this->database->prepare('INSERT INTO `user` (`name`, `email`, `hash`) VALUES (?, ?, ?);');
$statement->execute([
$user->getName(),
$user->getEmail(),
$user->getPasswordHash()
]);
}
public function getUserByMailAndHash(string $mail, string $hash): UserEntityInterface
{}
}

View File

@ -12,11 +12,6 @@ interface UserInterface
{
public function addUser(UserEntityInterface $user):void;
/**
* This function just exist for maintaining reasons
*/
public function deleteAllUsers():void;
public function getUserByMailAndHash(string $mail,string $hash):UserEntityInterface;
}

View File

@ -1,8 +1,8 @@
CREATE TABLE `test_db`.`user` (
`id` INT NOT NULL AUTO_INCREMENT,
`name` VARCHAR(45) NULL,
`email` VARCHAR(45) NULL,
`hash` VARCHAR(45) NULL,
`name` VARCHAR(120) NULL,
`email` VARCHAR(120) NULL,
`hash` VARCHAR(60) NULL,
PRIMARY KEY (`id`),
UNIQUE INDEX `id_UNIQUE` (`id` ASC),
UNIQUE INDEX `email_UNIQUE` (`email` ASC));

View File

@ -2,6 +2,8 @@
use core\Core;
use Doctrine\Common\Collections\ArrayCollection;
use repository\product\Product as ProductRepository;
use repository\user\User as UserRepository;
use entity\user\User;
require __DIR__ . '/../vendor/autoload.php';
@ -44,6 +46,14 @@ foreach ($lines as $number=>$line) {
$products->add($product);
}
$productRepository = new ProductRepository((new Core())->getDatabase());
$productRepository = new ProductRepository(new Core());
$productRepository->addProducts($products);
echo "Insert dummy user...\n";
$user= new User();
$user->setName('Maxla Mustermensch');
$user->setPasswordHashByPassword('passwort:)');
$user->setEmail('test@mail.test');
$userRepository = new UserRepository(new Core());
$userRepository->addUser($user);
?>