Skip to content

Commit

Permalink
Missing PHPDoc and some return types added
Browse files Browse the repository at this point in the history
  • Loading branch information
Ruben Romao committed Nov 23, 2023
1 parent c7c0589 commit 45d5a49
Show file tree
Hide file tree
Showing 29 changed files with 429 additions and 209 deletions.
20 changes: 11 additions & 9 deletions Api/Data/PostInterface.php
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
<?php declare(strict_types=1);
<?php
declare(strict_types=1);

namespace Rubenromao\Blog\Api\Data;
namespace Rubenromao\BlogPosts\Api\Data;

/**
* Blog post interface.
*
* @api
* @since 1.0.0
*/
interface PostInterface
{
const ID = 'id';
const TITLE = 'title';
const CONTENT = 'content';
const CREATED_AT = 'created_at';
public const ID = 'id';
public const TITLE = 'title';
public const CONTENT = 'content';
public const CREATED_AT = 'created_at';

/**
* @return int
Expand All @@ -23,7 +25,7 @@ public function getId();
* @param int $id
* @return $this
*/
public function setId($id);
public function setId(int $id);

/**
* @return string
Expand All @@ -34,7 +36,7 @@ public function getTitle();
* @param string $title
* @return $this
*/
public function setTitle($title);
public function setTitle(string $title);

/**
* @return string
Expand All @@ -45,7 +47,7 @@ public function getContent();
* @param string $content
* @return $this
*/
public function setContent($content);
public function setContent(string $content);

/**
* @return string
Expand Down
8 changes: 5 additions & 3 deletions Api/PostRepositoryInterface.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
<?php declare(strict_types=1);
<?php
declare(strict_types=1);

namespace Rubenromao\Blog\Api;
namespace Rubenromao\BlogPosts\Api;

use Rubenromao\Blog\Api\Data\PostInterface;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Exception\NoSuchEntityException;
use Rubenromao\BlogPosts\Api\Data\PostInterface;

/**
* Blog post CRUD interface.
*
* @api
* @since 1.0.0
*/
Expand Down
21 changes: 18 additions & 3 deletions Controller/Index/Index.php
Original file line number Diff line number Diff line change
@@ -1,21 +1,36 @@
<?php declare(strict_types=1);
<?php
declare(strict_types=1);

namespace Rubenromao\Blog\Controller\Index;
namespace Rubenromao\BlogPosts\Controller\Index;

use Magento\Framework\App\Action\HttpGetActionInterface;
use Magento\Framework\Controller\Result\Forward;
use Magento\Framework\Controller\Result\ForwardFactory;

/**
* Blog index controller.
*
* @package Rubenromao\BlogPosts\Controller\Index
*/
class Index implements HttpGetActionInterface
{
/**
* Constructor.
*
* @param ForwardFactory $forwardFactory
*/
public function __construct(
private ForwardFactory $forwardFactory,
private readonly ForwardFactory $forwardFactory,
) {}

/**
* @return Forward
*/
public function execute(): Forward
{
/** @var Forward $forward */
$forward = $this->forwardFactory->create();

return $forward->setController('post')->forward('list');
}
}
26 changes: 21 additions & 5 deletions Controller/Post/Detail.php
Original file line number Diff line number Diff line change
@@ -1,21 +1,37 @@
<?php declare(strict_types=1);
<?php
declare(strict_types=1);

namespace Rubenromao\Blog\Controller\Post;
namespace Rubenromao\BlogPosts\Controller\Post;

use Magento\Framework\App\Action\HttpGetActionInterface;
use Magento\Framework\App\RequestInterface;
use Magento\Framework\Event\ManagerInterface as EventManager;
use Magento\Framework\View\Result\Page;
use Magento\Framework\View\Result\PageFactory;

/**
* Blog post detail controller.
*
* @package Rubenromao\BlogPosts\Controller\Post
*/
class Detail implements HttpGetActionInterface
{
/**
* Constructor.
*
* @param PageFactory $pageFactory
* @param EventManager $eventManager
* @param RequestInterface $request
*/
public function __construct(
private PageFactory $pageFactory,
private EventManager $eventManager,
private RequestInterface $request,
private readonly PageFactory $pageFactory,
private readonly EventManager $eventManager,
private readonly RequestInterface $request,
) {}

/**
* @return Page
*/
public function execute(): Page
{
$this->eventManager->dispatch('rubenromao_blog_post_detail_view', [
Expand Down
22 changes: 18 additions & 4 deletions Controller/Post/ListAction.php
Original file line number Diff line number Diff line change
@@ -1,17 +1,31 @@
<?php declare(strict_types=1);
<?php
declare(strict_types=1);

namespace Rubenromao\Blog\Controller\Post;
namespace Rubenromao\BlogPosts\Controller\Post;

use Magento\Framework\App\Action\HttpGetActionInterface;
use Magento\Framework\View\Result\Page;
use Magento\Framework\View\Result\PageFactory;

/**
* Blog post list controller.
*
* @package Rubenromao\BlogPosts\Controller\Post
*/
class ListAction implements HttpGetActionInterface
{
/**
* Constructor.
*
* @param PageFactory $pageFactory
*/
public function __construct(
private PageFactory $pageFactory
) {}
private readonly PageFactory $pageFactory,
) {}

/**
* @return Page
*/
public function execute(): Page
{
return $this->pageFactory->create();
Expand Down
44 changes: 35 additions & 9 deletions Model/Post.php
Original file line number Diff line number Diff line change
@@ -1,38 +1,64 @@
<?php declare(strict_types=1);
<?php
declare(strict_types=1);

namespace Rubenromao\Blog\Model;
namespace Rubenromao\BlogPosts\Model;

use Rubenromao\Blog\Api\Data\PostInterface;
use Magento\Framework\Model\AbstractModel;
use Rubenromao\BlogPosts\Api\Data\PostInterface;

/**
* Class Post
*
* @package Rubenromao\BlogPosts\Model
*/
class Post extends AbstractModel implements PostInterface
{
protected function _construct()
/**
* @return void
*/
protected function _construct(): void
{
$this->_init(ResourceModel\Post::class);
}

public function getTitle()
/**
* @return string
*/
public function getTitle(): string
{
return $this->getData(self::TITLE);
}

public function setTitle($title)
/**
* @param string $title
* @return Post
*/
public function setTitle(string $title): Post
{
return $this->setData(self::TITLE, $title);
}

public function getContent()
/**
* @return string
*/
public function getContent(): string
{
return $this->getData(self::CONTENT);
}

public function setContent($content)
/**
* @param string $content
* @return Post
*/
public function setContent(string $content): Post
{
return $this->setData(self::CONTENT, $content);
}

public function getCreatedAt()
/**
* @return string
*/
public function getCreatedAt(): string
{
return $this->getData(self::CREATED_AT);
}
Expand Down
48 changes: 38 additions & 10 deletions Model/PostRepository.php
Original file line number Diff line number Diff line change
@@ -1,22 +1,39 @@
<?php declare(strict_types=1);
<?php
declare(strict_types=1);

namespace Rubenromao\Blog\Model;
namespace Rubenromao\BlogPosts\Model;

use Rubenromao\Blog\Api\Data\PostInterface;
use Rubenromao\Blog\Model\ResourceModel\Post as PostResourceModel;
use Rubenromao\Blog\Api\PostRepositoryInterface;
use Exception;
use Magento\Framework\Exception\CouldNotDeleteException;
use Magento\Framework\Exception\CouldNotSaveException;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Exception\NoSuchEntityException;
use Rubenromao\BlogPosts\Api\Data\PostInterface;
use Rubenromao\BlogPosts\Api\PostRepositoryInterface;
use Rubenromao\BlogPosts\Model\ResourceModel\Post as PostResourceModel;

/**
* Blog post CRUD class.
*
* @package Rubenromao\BlogPosts\Model
*/
class PostRepository implements PostRepositoryInterface
{
/**
* Constructor.
*
* @param PostFactory $postFactory
* @param PostResourceModel $postResourceModel
*/
public function __construct(
private PostFactory $postFactory,
private PostResourceModel $postResourceModel,
private readonly PostFactory $postFactory,
private readonly PostResourceModel $postResourceModel,
) {}

/**
* @param int $id
* @return PostInterface
* @throws NoSuchEntityException
*/
public function getById(int $id): PostInterface
{
$post = $this->postFactory->create();
Expand All @@ -29,24 +46,35 @@ public function getById(int $id): PostInterface
return $post;
}

/**
* @param PostInterface $post
* @return PostInterface
* @throws CouldNotSaveException
*/
public function save(PostInterface $post): PostInterface
{
try {
$this->postResourceModel->save($post);
} catch (\Exception $exception) {
} catch (Exception $exception) {
throw new CouldNotSaveException(__($exception->getMessage()));
}

return $post;
}

/**
* @param int $id
* @return bool
* @throws CouldNotDeleteException
* @throws NoSuchEntityException
*/
public function deleteById(int $id): bool
{
$post = $this->getById($id);

try {
$this->postResourceModel->delete($post);
} catch (\Exception $exception) {
} catch (Exception $exception) {
throw new CouldNotDeleteException(__($exception->getMessage()));
}

Expand Down
16 changes: 11 additions & 5 deletions Model/ResourceModel/Post.php
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
<?php declare(strict_types=1);
<?php
declare(strict_types=1);

namespace Rubenromao\Blog\Model\ResourceModel;
namespace Rubenromao\BlogPosts\Model\ResourceModel;

use Magento\Framework\Model\ResourceModel\Db\AbstractDb;

/**
* Class Post
*
* @package Rubenromao\BlogPosts\Model\ResourceModel
*/
class Post extends AbstractDb
{
const MAIN_TABLE = 'rubenromao_blog_post';
const ID_FIELD_NAME = 'id';
public const MAIN_TABLE = 'rubenromao_blog_post';
public const ID_FIELD_NAME = 'id';

protected function _construct()
protected function _construct(): void
{
$this->_init(self::MAIN_TABLE, self::ID_FIELD_NAME);
}
Expand Down
Loading

0 comments on commit 45d5a49

Please sign in to comment.