Pomimo tego, że pracuję jako php dev od kilku lat, nigdy nie musiałem nic projektować od zera, zawsze korzystałem z frameworka który swój styl narzuca, a jak czegoś brakowało to wystarczyło dograć paczkę z composer i problem z głowy. Postanowiłem nadrobić swój słaby punkt i mocno podszlifować znajomość OOP i projektowanie aplikacji zgodnie z nim. Zdecydowałem, że napiszę moduł powiadomień sms, email oraz push od zera. Poniżej struktura, którą póki co zaplanowałem korzystając z obecnej wiedzy na temat OOP.
config.ini
server=test user=root pass=pass dbname=mydb
Config class
<?php /** * Class Config */ class Config { /** @var array|false */ public $config; /** * Config constructor. * @param string $fileName * @throws Exception */ public function __construct(string $fileName) { } else { throw new Exception('File ' . $fileName . 'not exists!'); } } /** * @param string $key * @return mixed */ public function get(string $key) { return $this->config[$key]; } }
Database class
<?php /** * Class Database */ class Database { protected $conn; private Config $config; public function __construct(Config $config) { $this->config = $config; } /** * @return PDO * @throws Exception */ public function getConnection() { try { $this->conn = new PDO( $this->config['server'], $this->config['user'], $this->config['pass'], $this->options ); return $this->conn; } catch (PDOException $e) { throw new Exception($e->getMessage()); } } public function closeConnection() { $this->conn = null; } }
class Notification
<?php /** * Class Notification */ class Notification { const TYPE_SMS = 0; const TYPE_EMAIL = 1; /** * @var int */ private int $id; /** * @var string */ private string $phone; /** * @var string */ private string $email; /** * @var string */ private string $message; /** * @var bool */ private bool $isSent; /** * @var int */ private int $type; /** * @return int */ public function getId(): int { return $this->id; } /** * @param int $id */ public function setId(int $id): void { $this->id = $id; } /** * @return string */ public function getPhone(): string { return $this->phone; } /** * @param string $phone */ public function setPhone(string $phone): void { $this->phone = $phone; } /** * @return string */ public function getEmail(): string { return $this->email; } /** * @param string $email */ public function setEmail(string $email): void { $this->email = $email; } /** * @return string */ public function getMessage(): string { return $this->message; } /** * @param string $message */ public function setMessage(string $message): void { $this->message = $message; } /** * @return bool */ public function isSent(): bool { return $this->isSent; } /** * @param bool $isSent */ public function setIsSent(bool $isSent): void { $this->isSent = $isSent; } /** * @return int */ { return $this->type; } /** * @param int $type */ { $this->type = $type; } }
NotificationDAO
<?php /** * Class NotificationDAO */ class NotificationDAO { /** @var Database */ private Database $db; /** * NotificationDAO constructor. * @param Database $database */ public function __construct(Database $database) { $this->db = $database; } /** * @param Notification $notification */ public function add(Notification $notification) { // TODO: Implement add(Notification $notification) method. } /** * @param int $notificationId */ public function delete(int $notificationId) { // TODO: Implement delete(int $notificationId) method. } /** * @param Notification $notification * @param int $notificationId */ public function update(Notification $notification, int $notificationId) { // TODO: Implement update(Notification $notification, int $notificationId) method. } /** * @param int $notificationId */ public function findOne(int $notificationId) { // TODO: Implement findOne(int $notificationId) method. } public function findAll() { // TODO: Implement findAll() method. } }
NotificationInterface
<?php /** * Class NotificationInterface */ interface NotificationInterface { /** * @param Notification $notification * @return mixed */ public function sendNotification(Notification $notification); }
EmailNotification
<?php /** * Class EmailNotification */ class EmailNotification implements NotificationInterface { /** * @param Notification $notification * @return mixed|void */ public function sendNotification(Notification $notification) { // TODO: Implement sendNotification() method. } }
SmsNotification
<?php /** * Class SmsNotification */ class SmsNotification implements NotificationInterface { /** * @param Notification $notification * @return mixed|void */ public function sendNotification(Notification $notification) { // TODO: Implement sendNotification() method. } }
PushNotification
<?php /** * Class PushNotification */ class PushNotification implements NotificationInterface { /** * @param Notification $notification * @return mixed|void */ public function sendNotification(Notification $notification) { // TODO: Implement sendNotification() method. } }
Validation class odpowiedzialna za sprawdzenie email, numer telefonu i wybrany typ
<?php /** * Class Validation */ class Validation { /** * @param string $value */ public function validateEmail(string $value) { // TODO: Implement validateEmail(string $value) method. } /** * @param string $value */ public function validatePhone(string $value) { // TODO: Implement validatePhone(string $value) method. } /** * @param int $value */ public function validateType(int $value) { // TODO: Implement validateType(int $value) method. } }
Czy to jest dobrze zaplanowana struktura, czy powinno to wyglądać inaczej? Co mogę zmienić/poprawić?