Skip to content

Commit

Permalink
legacy-to-ddd : Étape 4. Utilisation du domaine depuis cakephp
Browse files Browse the repository at this point in the history
  • Loading branch information
vibby committed Sep 28, 2022
1 parent 66c6c1e commit 3359570
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/cakephp/Controller/AppController.php
Expand Up @@ -37,6 +37,7 @@ class AppController extends Controller
public function initialize()
{
parent::initialize();
$this->loadComponent('Container');
$this->loadComponent('Flash');
$this->loadComponent('Auth', [
'authenticate' => [
Expand Down
15 changes: 12 additions & 3 deletions src/cakephp/Controller/BookmarksController.php
Expand Up @@ -2,6 +2,9 @@
namespace App\Controller;

use App\Controller\AppController;
use App\Model\Entity\Bookmark;
use Application\GetBookmark\GetBookmarkHandler;
use Application\GetBookmark\GetBookmarkInput;

/**
* Bookmarks Controller
Expand Down Expand Up @@ -36,9 +39,15 @@ public function index()
*/
public function view($id = null)
{
$bookmark = $this->Bookmarks->get($id, [
'contain' => ['Users', 'Tags']
]);
$input = new GetBookmarkInput();
$input->id = $id;
$handler = $this->Container->get(GetBookmarkHandler::class);
$bookmarkModel = $handler($input);
$bookmark= new Bookmark();
$bookmark->set('title', $bookmarkModel->title);
$bookmark->set('url', $bookmarkModel->url);
$bookmark->set('description', $bookmarkModel->description);
$bookmark->set('id', $bookmarkModel->id);
$this->set('bookmark', $bookmark);
$this->set('_serialize', ['bookmark']);
}
Expand Down
35 changes: 35 additions & 0 deletions src/cakephp/Controller/Component/ContainerComponent.php
@@ -0,0 +1,35 @@
<?php

namespace App\Controller\Component;

use App\Model\Table\BookmarksTable;
use Cake\Controller\Component;
use Cake\Controller\ComponentRegistry;
use Application\GetBookmark\GetBookmarkHandler;
use Cake\ORM\TableRegistry;
use Exception;

/**
* @property BookmarksTable $Bookmarks
*/
class ContainerComponent extends Component
{
private $container = [];

public function __construct(ComponentRegistry $registry, array $config = [])
{
parent::__construct($registry, $config);

$this->Bookmarks = TableRegistry::get('Bookmarks');
$this->container[GetBookmarkHandler::class] = new GetBookmarkHandler($this->Bookmarks); // On crée nos services ici
}

public function get($serviceName)
{
if (!isset($this->container[$serviceName])) {
throw new Exception('Cannot find service');
}

return $this->container[$serviceName];
}
}
19 changes: 18 additions & 1 deletion src/cakephp/Model/Table/BookmarksTable.php
Expand Up @@ -6,14 +6,16 @@
use Cake\ORM\RulesChecker;
use Cake\ORM\Table;
use Cake\Validation\Validator;
use Domain\Bookmark\Repository\BookmarkRepository;
use Domain\Bookmark\Model\Bookmark as BookmarkModel;

/**
* Bookmarks Model
*
* @property \Cake\ORM\Association\BelongsTo $Users
* @property \Cake\ORM\Association\BelongsToMany $Tags
*/
class BookmarksTable extends Table
class BookmarksTable extends Table implements BookmarkRepository
{

/**
Expand Down Expand Up @@ -124,4 +126,19 @@ protected function _buildTags($tagString)
}
return $out;
}

public function findById(string $id): ?BookmarkModel
{
$bookmarkEntity = $this->get($id);
if (!$bookmarkEntity instanceof Bookmark) {
return null;
}
$bookmarkModel = new BookmarkModel();
$bookmarkModel->id = $bookmarkEntity->id;
$bookmarkModel->title = $bookmarkEntity->get('title');
$bookmarkModel->url = $bookmarkEntity->get('url');
$bookmarkModel->description = $bookmarkEntity->get('description');

return $bookmarkModel;
}
}

0 comments on commit 3359570

Please sign in to comment.