Skip to content

Commit

Permalink
Reformat code in accordance with PSR-12 and add necessary comments.
Browse files Browse the repository at this point in the history
Issue MIDU-177
  • Loading branch information
Gavric-Sava committed Mar 25, 2022
1 parent b88c849 commit 008ab0a
Show file tree
Hide file tree
Showing 23 changed files with 390 additions and 56 deletions.
5 changes: 1 addition & 4 deletions index.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
<?php

require 'vendor/autoload.php';

//require 'src/bootstrap.php';

use Logeecom\Bookstore\presentation\routers\BaseRouter;

// TODO comments;
// TODO refactor links to assets
// TODO refactor validation code - like SinglePageAuthorController

$request_path = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
(new BaseRouter())->route($request_path);
6 changes: 5 additions & 1 deletion src/business/logic/AuthorLogic.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@

class AuthorLogic
{

/**
* @var AuthorRepositoryInterface - Repository of author data.
* @author Sava Gavric <[email protected]>
*
*/
private AuthorRepositoryInterface $authorRepository;

public function __construct(AuthorRepositoryInterface $authorRepository)
Expand Down
6 changes: 5 additions & 1 deletion src/business/logic/BookLogic.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@

class BookLogic
{

/**
* @var BookRepositoryInterface - Repository of book data.
* @author Sava Gavric <[email protected]>
*
*/
private BookRepositoryInterface $bookRepository;

public function __construct(
Expand Down
20 changes: 19 additions & 1 deletion src/data_access/models/Author.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,23 @@

class Author implements \JsonSerializable
{

/**
* @var int|null - Id of the author. Null while object not persisted.
* @author Sava Gavric <[email protected]>
*
*/
private ?int $id;
/**
* @var string - First name of the author.
* @author Sava Gavric <[email protected]>
*
*/
private string $firstname;
/**
* @var string - Last name of the author.
* @author Sava Gavric <[email protected]>
*
*/
private string $lastname;

/**
Expand Down Expand Up @@ -101,6 +115,10 @@ public function setLastname(string $lastname): void
$this->lastname = $lastname;
}

/**
* Serializes Author object as JSON.
* @return array
*/
public function jsonSerialize(): array
{
return [
Expand Down
25 changes: 24 additions & 1 deletion src/data_access/models/Book.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,29 @@

class Book implements \JsonSerializable
{

/**
* @var int|null - Id of the book. Null while object not persisted.
* @author Sava Gavric <[email protected]>
*
*/
private ?int $id;
/**
* @var string - Title of the book.
* @author Sava Gavric <[email protected]>
*
*/
private string $title;
/**
* @var int - Year of publishing of the book.
* @author Sava Gavric <[email protected]>
*
*/
private int $year;
/**
* @var int - Id of author of the book.
* @author Sava Gavric <[email protected]>
*
*/
private int $author_id;

/**
Expand Down Expand Up @@ -128,6 +147,10 @@ public function setAuthorId(?int $author_id): void
$this->author_id = $author_id;
}

/**
* Serializes Book object as JSON.
* @return array
*/
public function jsonSerialize(): array
{
return [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,18 @@

class AuthorRepositoryDatabase implements AuthorRepositoryInterface
{

/**
* Name of table in DB.
* @author Sava Gavric <[email protected]>
*
*/
private const TABLE_NAME = "authors";

/**
* @var PDO - Connection to DB.
* @author Sava Gavric <[email protected]>
*
*/
private PDO $PDO;

public function __construct(PDO $PDO)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,8 @@ public function fetch(int $id): ?Author;
* Add an author.
*
* @param Author $author Author to be added.
* @return bool True if adding successful. Otherwise, false.
* @return Author|null Author object if adding successful. Otherwise, null.
* @author Sava Gavric <[email protected]>
*
*/
public function add(Author $author): ?Author;

Expand All @@ -41,17 +40,16 @@ public function add(Author $author): ?Author;
* @param int $id Id of author to be edited.
* @param string $firstname New firstname value.
* @param string $lastname New lastname value.
* @return bool True if editing successful. Otherwise, false.
* @return Author|null Author object if editing successful. Otherwise, null.
* @author Sava Gavric <[email protected]>
*
*/
public function edit(int $id, string $firstname, string $lastname): ?Author;

/**
* Delete an author.
*
* @param int $id Id of author to be deleted.
* @return bool True if deletion successful. Otherwise, not.
* @return bool True if deletion successful. Otherwise, false.
* @author Sava Gavric <[email protected]>
*
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,14 @@ class AuthorRepositorySession implements AuthorRepositoryInterface
{
/**
* Session key for list of authors.
*
* @author Sava Gavric <[email protected]>
*/
private const SESSION_TAG = "authors";
/**
* Session key for auto-generated author key.
*
* @author Sava Gavric <[email protected]>
*/
private const ID_TAG = "author_id";

Expand Down
11 changes: 10 additions & 1 deletion src/data_access/repositories/books/BookRepositoryDatabase.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,18 @@

class BookRepositoryDatabase implements BookRepositoryInterface
{

/**
* Name of table in DB.
* @author Sava Gavric <[email protected]>
*
*/
private const TABLE_NAME = "books";

/**
* @var PDO - Connection to DB.
* @author Sava Gavric <[email protected]>
*
*/
private PDO $PDO;

public function __construct(PDO $PDO)
Expand Down
16 changes: 12 additions & 4 deletions src/data_access/repositories/books/BookRepositoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@ interface BookRepositoryInterface
*/
public function fetchAll(): array;

/**
* Fetch all books from the given author.
*
* @param int $author_id - Id of the author.
* @return array Array of books.
* @author Sava Gavric <[email protected]>
*
*/
public function fetchAllFromAuthor(int $author_id): array;

/**
Expand All @@ -30,9 +38,9 @@ public function fetch(int $id): ?Book;
/**
* Add a book.
*
* @return bool True if adding successful. Otherwise, false.
* @param Book $book
* @return Book|null Book object if adding successful. Otherwise, null.
* @author Sava Gavric <[email protected]>
*
*/
public function add(Book $book): ?Book;

Expand All @@ -43,7 +51,7 @@ public function add(Book $book): ?Book;
* @param string $title New title value.
* @param int $year New year value.
* @param ?int $author_id New author Id value.
* @return bool True if editing successful. Otherwise, false.
* @return Book|null Book object if editing successful. Otherwise, null.
* @author Sava Gavric <[email protected]>
*
*/
Expand All @@ -53,7 +61,7 @@ public function edit(int $id, string $title, int $year, ?int $author_id): ?Book;
* Delete a book.
*
* @param int $id Id of book to be deleted.
* @return bool True if deletion successful. Otherwise, not.
* @return bool True if deletion successful. Otherwise, false.
* @author Sava Gavric <[email protected]>
*
*/
Expand Down
10 changes: 10 additions & 0 deletions src/data_access/repositories/books/BookRepositorySession.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,14 @@ class BookRepositorySession implements BookRepositoryInterface
{
/**
* Session key for list of books.
* @author Sava Gavric <[email protected]>
*
*/
private const SESSION_TAG = "books";
/**
* Session key for auto-generated book key.
* @author Sava Gavric <[email protected]>
*
*/
private const ID_TAG = "book_id";

Expand Down Expand Up @@ -116,8 +120,11 @@ public function delete(int $id): bool

/**
* Deletes all books from given author.
*
* @param int $author_id Id of author.
* @return bool Returns true if all books successfully deleted. Otherwise, returns false.
* @author Sava Gavric <[email protected]>
*
*/
public function deleteAllFromAuthor(int $author_id): bool
{
Expand All @@ -132,8 +139,11 @@ public function deleteAllFromAuthor(int $author_id): bool

/**
* Returns number of books written by the author.
*
* @param int $id Id of the author.
* @return int Number of books.
* @author Sava Gavric <[email protected]>
*
*/
public function countFromAuthor(int $id): int
{
Expand Down
22 changes: 21 additions & 1 deletion src/presentation/interfaces/BaseAuthorController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,35 @@

abstract class BaseAuthorController implements ControllerInterface
{

/**
* @inheritDoc
*/
abstract public function process(string $path): void;

/**
* Processes author list request.
* @return void
*/
abstract protected function processAuthorList(): void;

/**
* Processes author create request.
* @return void
*/
abstract protected function processAuthorCreate(): void;

/**
* Processes author edit request.
* @param int $id - Id of author.
* @return void
*/
abstract protected function processAuthorEdit(int $id): void;

/**
* Processes author delete request.
* @param int $id - Id of author.
* @return void
*/
abstract protected function processAuthorDelete(int $id): void;

/**
Expand Down
23 changes: 23 additions & 0 deletions src/presentation/interfaces/BaseBookController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,37 @@

abstract class BaseBookController implements ControllerInterface
{
/**
* @inheritDoc
*/
abstract public function process(string $path): void;

/**
* Processes book list request.
* @param int $author_id - Id of book author.
* @return void
*/
abstract protected function processBookList(int $author_id): void;

/**
* Processes book create request.
* @param int $author_id - Id of book author.
* @return void
*/
abstract protected function processBookCreate(int $author_id): void;

/**
* Processes book edit request.
* @param int $id - Id of book.
* @return void
*/
abstract protected function processBookEdit(int $id): void;

/**
* Processes book delete request.
* @param int $id - Id of book.
* @return void
*/
abstract protected function processBookDelete(int $id): void;

/**
Expand Down
8 changes: 8 additions & 0 deletions src/presentation/interfaces/ControllerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,13 @@

interface ControllerInterface
{
/**
* Parses path and processes the request.
*
* @param string $path Path of the request.
* @return void
* @author Sava Gavric <[email protected]>
*
*/
public function process(string $path): void;
}
8 changes: 8 additions & 0 deletions src/presentation/interfaces/RouterInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,13 @@

interface RouterInterface
{
/**
* Route execution to appropriate controller
*
* @param string $request_path - Path of the request.
* @return void
* @author Sava Gavric <[email protected]>
*
*/
public function route(string $request_path): void;
}
Loading

0 comments on commit 008ab0a

Please sign in to comment.