Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> Struktura modułu notyfikacji w OOP
PawelC
post 5.04.2020, 16:37:26
Post #1





Grupa: Zarejestrowani
Postów: 1 173
Pomógł: 121
Dołączył: 24.09.2007
Skąd: Toruń

Ostrzeżenie: (0%)
-----


Cześć,
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

  1. server=test
  2. user=root
  3. pass=pass
  4. dbname=mydb


Config class
  1. <?php
  2.  
  3. /**
  4.   * Class Config
  5.   */
  6. class Config
  7. {
  8. /** @var array|false */
  9. public $config;
  10.  
  11. /**
  12.   * Config constructor.
  13.   * @param string $fileName
  14.   * @throws Exception
  15.   */
  16. public function __construct(string $fileName)
  17. {
  18. if (file_exists($fileName)) {
  19. $this->config = parse_ini_file($fileName, false);
  20. } else {
  21. throw new Exception('File ' . $fileName . 'not exists!');
  22. }
  23.  
  24. }
  25.  
  26. /**
  27.   * @param string $key
  28.   * @return mixed
  29.   */
  30. public function get(string $key)
  31. {
  32. return $this->config[$key];
  33. }
  34. }

Database class

  1. <?php
  2.  
  3. /**
  4.   * Class Database
  5.   */
  6. class Database
  7. {
  8. private array $options = array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,);
  9. protected $conn;
  10. private Config $config;
  11.  
  12. public function __construct(Config $config)
  13. {
  14. $this->config = $config;
  15. }
  16.  
  17. /**
  18.   * @return PDO
  19.   * @throws Exception
  20.   */
  21. public function getConnection()
  22. {
  23. try {
  24. $this->conn = new PDO(
  25. $this->config['server'],
  26. $this->config['user'],
  27. $this->config['pass'],
  28. $this->options
  29. );
  30.  
  31. return $this->conn;
  32. } catch (PDOException $e) {
  33. throw new Exception($e->getMessage());
  34. }
  35. }
  36.  
  37. public function closeConnection()
  38. {
  39. $this->conn = null;
  40. }
  41. }


class Notification

  1. <?php
  2.  
  3. /**
  4.   * Class Notification
  5.   */
  6. class Notification
  7. {
  8. const TYPE_SMS = 0;
  9. const TYPE_EMAIL = 1;
  10.  
  11. /**
  12.   * @var int
  13.   */
  14. private int $id;
  15.  
  16. /**
  17.   * @var string
  18.   */
  19. private string $phone;
  20.  
  21. /**
  22.   * @var string
  23.   */
  24. private string $email;
  25.  
  26. /**
  27.   * @var string
  28.   */
  29. private string $message;
  30.  
  31. /**
  32.   * @var bool
  33.   */
  34. private bool $isSent;
  35.  
  36. /**
  37.   * @var int
  38.   */
  39. private int $type;
  40.  
  41. /**
  42.   * @return int
  43.   */
  44. public function getId(): int
  45. {
  46. return $this->id;
  47. }
  48.  
  49. /**
  50.   * @param int $id
  51.   */
  52. public function setId(int $id): void
  53. {
  54. $this->id = $id;
  55. }
  56.  
  57. /**
  58.   * @return string
  59.   */
  60. public function getPhone(): string
  61. {
  62. return $this->phone;
  63. }
  64.  
  65. /**
  66.   * @param string $phone
  67.   */
  68. public function setPhone(string $phone): void
  69. {
  70. $this->phone = $phone;
  71. }
  72.  
  73. /**
  74.   * @return string
  75.   */
  76. public function getEmail(): string
  77. {
  78. return $this->email;
  79. }
  80.  
  81. /**
  82.   * @param string $email
  83.   */
  84. public function setEmail(string $email): void
  85. {
  86. $this->email = $email;
  87. }
  88.  
  89. /**
  90.   * @return string
  91.   */
  92. public function getMessage(): string
  93. {
  94. return $this->message;
  95. }
  96.  
  97. /**
  98.   * @param string $message
  99.   */
  100. public function setMessage(string $message): void
  101. {
  102. $this->message = $message;
  103. }
  104.  
  105. /**
  106.   * @return bool
  107.   */
  108. public function isSent(): bool
  109. {
  110. return $this->isSent;
  111. }
  112.  
  113. /**
  114.   * @param bool $isSent
  115.   */
  116. public function setIsSent(bool $isSent): void
  117. {
  118. $this->isSent = $isSent;
  119. }
  120.  
  121. /**
  122.   * @return int
  123.   */
  124. public function getType(): int
  125. {
  126. return $this->type;
  127. }
  128.  
  129. /**
  130.   * @param int $type
  131.   */
  132. public function setType(int $type): void
  133. {
  134. $this->type = $type;
  135. }
  136. }


NotificationDAO

  1. <?php
  2.  
  3. /**
  4.   * Class NotificationDAO
  5.   */
  6. class NotificationDAO
  7. {
  8. /** @var Database */
  9. private Database $db;
  10.  
  11. /**
  12.   * NotificationDAO constructor.
  13.   * @param Database $database
  14.   */
  15. public function __construct(Database $database)
  16. {
  17. $this->db = $database;
  18. }
  19.  
  20. /**
  21.   * @param Notification $notification
  22.   */
  23. public function add(Notification $notification)
  24. {
  25. // TODO: Implement add(Notification $notification) method.
  26. }
  27.  
  28. /**
  29.   * @param int $notificationId
  30.   */
  31. public function delete(int $notificationId)
  32. {
  33. // TODO: Implement delete(int $notificationId) method.
  34. }
  35.  
  36. /**
  37.   * @param Notification $notification
  38.   * @param int $notificationId
  39.   */
  40. public function update(Notification $notification, int $notificationId)
  41. {
  42. // TODO: Implement update(Notification $notification, int $notificationId) method.
  43. }
  44.  
  45. /**
  46.   * @param int $notificationId
  47.   */
  48. public function findOne(int $notificationId)
  49. {
  50. // TODO: Implement findOne(int $notificationId) method.
  51. }
  52.  
  53.  
  54. public function findAll()
  55. {
  56. // TODO: Implement findAll() method.
  57. }
  58.  
  59. }

NotificationInterface

  1. <?php
  2.  
  3. /**
  4.   * Class NotificationInterface
  5.   */
  6. interface NotificationInterface
  7. {
  8. /**
  9.   * @param Notification $notification
  10.   * @return mixed
  11.   */
  12. public function sendNotification(Notification $notification);
  13. }


EmailNotification

  1. <?php
  2.  
  3. /**
  4.   * Class EmailNotification
  5.   */
  6. class EmailNotification implements NotificationInterface
  7. {
  8. /**
  9.   * @param Notification $notification
  10.   * @return mixed|void
  11.   */
  12. public function sendNotification(Notification $notification)
  13. {
  14. // TODO: Implement sendNotification() method.
  15. }
  16. }

SmsNotification

  1. <?php
  2.  
  3. /**
  4.   * Class SmsNotification
  5.   */
  6. class SmsNotification implements NotificationInterface
  7. {
  8. /**
  9.   * @param Notification $notification
  10.   * @return mixed|void
  11.   */
  12. public function sendNotification(Notification $notification)
  13. {
  14. // TODO: Implement sendNotification() method.
  15. }
  16. }


PushNotification
  1. <?php
  2.  
  3. /**
  4.   * Class PushNotification
  5.   */
  6. class PushNotification implements NotificationInterface
  7. {
  8. /**
  9.   * @param Notification $notification
  10.   * @return mixed|void
  11.   */
  12. public function sendNotification(Notification $notification)
  13. {
  14. // TODO: Implement sendNotification() method.
  15. }
  16. }

Validation class odpowiedzialna za sprawdzenie email, numer telefonu i wybrany typ

  1. <?php
  2.  
  3. /**
  4.   * Class Validation
  5.   */
  6. class Validation
  7. {
  8. /**
  9.   * @param string $value
  10.   */
  11. public function validateEmail(string $value)
  12. {
  13. // TODO: Implement validateEmail(string $value) method.
  14. }
  15.  
  16. /**
  17.   * @param string $value
  18.   */
  19. public function validatePhone(string $value)
  20. {
  21. // TODO: Implement validatePhone(string $value) method.
  22. }
  23.  
  24. /**
  25.   * @param int $value
  26.   */
  27. public function validateType(int $value)
  28. {
  29. // TODO: Implement validateType(int $value) method.
  30. }
  31. }


Czy to jest dobrze zaplanowana struktura, czy powinno to wyglądać inaczej? Co mogę zmienić/poprawić?
Go to the top of the page
+Quote Post

Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Wersja Lo-Fi Aktualny czas: 28.03.2024 - 18:48