Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
FernandoCalmet committed Sep 15, 2020
1 parent b7eb69a commit f8d1adb
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 21 deletions.
2 changes: 1 addition & 1 deletion public/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
$user->updateEmail($_POST['login_email']);
$user->updatePassword($_POST['login_password']);
$logged = new \App\Controller\User\Login($user);
$_SESSION['user'] = $logged;
//$_SESSION['user'] = $logged;
var_dump($logged);
} catch (Exception $ex) {
echo '<script language="javascript">alert("ERROR: ' . $ex->getMessage() . '")</script>';
Expand Down
10 changes: 8 additions & 2 deletions src/Controller/Note/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,20 @@

namespace App\Controller\Note;

use App\Controller\BaseController;
use App\Service\Note\Find;
use App\Service\Note\Create;
use App\Service\Note\Update;
use App\Service\Note\Delete;

abstract class Base extends BaseController
abstract class Base
{
protected $_noteRepository;

public function __construct()
{
$this->_noteRepository = new \App\Repository\NoteRepository();
}

protected function getServiceFindNote(): Find
{
return new Find($this->_noteRepository);
Expand Down
11 changes: 8 additions & 3 deletions src/Controller/Task/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,15 @@
use App\Service\Task\Update;
use App\Service\Task\Delete;

use App\Controller\BaseController;

abstract class Base extends BaseController
abstract class Base
{
protected $_taskRepository;

public function __construct()
{
$this->_taskRepository = new \App\Repository\TaskRepository();
}

protected function getServiceFindTask(): Find
{
return new Find($this->_taskRepository);
Expand Down
10 changes: 8 additions & 2 deletions src/Controller/User/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,21 @@

namespace App\Controller\User;

use App\Controller\BaseController;
use App\Service\User\Create;
use App\Service\User\Delete;
use App\Service\User\Find;
use App\Service\User\Update;
use App\Service\User\Login;

abstract class Base extends BaseController
abstract class Base
{
protected $_userRepository;

public function __construct()
{
$this->_userRepository = new \App\Repository\UserRepository();
}

protected function getServiceFindUser(): Find
{
return new Find($this->_userRepository);
Expand Down
20 changes: 9 additions & 11 deletions src/Database/DbProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,18 @@

class DbProvider
{
private static $_db;

public static function get()
public static function get(): PDO
{
if (!self::$_db) {
$db = __CONFIG__['db'];
$dsn = sprintf('mysql:host=%s;dbname=%s;charset=utf8', $db['hostname'], $db['database']);
$pdo = new PDO($dsn, $db['username'], $db['password']);
try {
$dsn = sprintf('mysql:host=%s;dbname=%s;charset=utf8', __CONFIG__['db']['hostname'], __CONFIG__['db']['database']);
$pdo = new PDO($dsn, __CONFIG__['db']['username'], __CONFIG__['db']['password']);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ);

self::$_db = $pdo;
return $pdo;
} catch (\PDOException $ex) {
echo "DataBase Error: " . $ex->getMessage();
} catch (\Exception $ex) {
echo "General Error: " . $ex->getMessage();
}

return self::$_db;
}
}
6 changes: 4 additions & 2 deletions src/Repository/BaseRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@

namespace App\Repository;

use App\Database\DbProvider;

abstract class BaseRepository
{
/** @var \PDO */
protected $database;

public function __construct(\PDO $database)
public function __construct()
{
$this->database = $database;
$this->database = DbProvider::get();
}

protected function getDb(): \PDO
Expand Down

0 comments on commit f8d1adb

Please sign in to comment.