Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> Ocena kodu OOP
JacekJagiello
post 12.04.2014, 20:22:59
Post #1





Grupa: Zarejestrowani
Postów: 22
Pomógł: 0
Dołączył: 21.11.2012

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


Cześć, chciałbym prosić o ocenę mojego kodu.

Ćwiczyczę sobie programowanie obiektowe, staram się trzymać zasad SOLID, ale najlepiej będzie jeśli ktoś z zewnątrz oceni to co napisałem. Moje pytanie jest - czy jest to po prostu dobry kod? Czy jest on dobrze zaprojektowany(czy jest elastyczny)? Czy jest zgodny z zasadami SOLID? I najważniejsze - co można w nim poprawić?
  1. interface ProductRepositoryInterface{
  2.  
  3. /**
  4. * Find all products
  5. *
  6. * @return array
  7. */
  8. public function findAll();
  9.  
  10.  
  11. /**
  12. * Find specific product by id
  13. *
  14. * @param int id
  15. *
  16. * @return array
  17. */
  18. public function findById($id);
  19.  
  20.  
  21. /**
  22. * Save single product
  23. *
  24. * @param array item
  25. *
  26. * @return bool
  27. */
  28. public function save($item);
  29. }
  30.  
  31.  
  32. class JsonProductReposiotry implements ProductRepositoryInterface
  33. {
  34. /**
  35. * Path to json file, witch contains data
  36. */
  37. private $file = 'database.json';
  38.  
  39. /**
  40. * Set file property
  41. */
  42. function __construct($file = 'database.json') {
  43.  
  44. if (!file_exists($file)) throw new Exception("Can not load {$file} file.");
  45.  
  46. $this->file = $file;
  47. }
  48.  
  49. /**
  50. * Find all products
  51. *
  52. * @return array
  53. */
  54. public function findAll()
  55. {
  56. return json_decode(file_get_contents($this->file), true);
  57. }
  58.  
  59. /**
  60. * Find specific product by id
  61. *
  62. * @param int id
  63. *
  64. * @return array
  65. */
  66. public function findById($id)
  67. {
  68. $items = json_decode(file_get_contents($this->file), true);
  69.  
  70. foreach ($items as $item) {
  71. if ($item['id'] == $id) return $item;
  72. }
  73. }
  74.  
  75. /**
  76. * Save single product
  77. *
  78. * @param array item
  79. *
  80. * @return bool
  81. */
  82. public function save($item)
  83. {
  84. $items = json_decode(file_get_contents($this->file), true); //get all current stored products
  85. $this->items[] = $item; //append new product
  86.  
  87. $result = file_put_contents($this->file, json_encode($this->items)); //save to file
  88.  
  89. return ($result) ? true : false;
  90. }
  91.  
  92. }
  93.  
  94. class Product
  95. {
  96. /**
  97. * Products properties that can be set
  98. */
  99. private $fillable = ['id', 'name', 'price'];
  100.  
  101.  
  102. /**
  103. * Automatic sets properties
  104. */
  105. public function __set($property, $value)
  106. {
  107. if (in_array($property, $this->fillable)) {
  108. $this->$property = $value;
  109. }
  110. }
  111.  
  112. }
  113.  
  114.  
  115. class ProductsManager
  116. {
  117. /**
  118. * Sotres all producst obiented from respository
  119. */
  120. private $products = [];
  121.  
  122.  
  123. /**
  124. * Set ProductsManager dependencies
  125. *
  126. * @param ProductRepositoryInterface
  127. */
  128. function __construct(ProductRepositoryInterface $repository) {
  129.  
  130. $this->repository = $repository;
  131. }
  132.  
  133.  
  134. /**
  135. * Add single Product instacne to products property
  136. */
  137. public function addProduct(Product $item)
  138. {
  139. $this->products[] = $item;
  140. }
  141.  
  142. /**
  143. * Get all products from respository
  144. *
  145. * @return array
  146. */
  147. public function findAll()
  148. {
  149. return $this->repository->findAll();
  150. }
  151.  
  152. /**
  153. * Save all products
  154. *
  155. * @return all products
  156. */
  157. public function saveAll()
  158. {
  159. foreach ($this->products as $product) {
  160.  
  161. $this->repository->save([
  162. 'id' => $product->id,
  163. 'name'=> $product->name,
  164. 'price' => $product->price
  165. ]);
  166.  
  167. }
  168.  
  169. return $this->findAll();
  170. }
  171. }


Poniżej proste zastosowanie tych klas
  1. $manager = new ProductsManager(new JsonProductReposiotry('database.json'));
  2.  
  3. $dress = new Product();
  4. $dress->id = '791';
  5. $dress->name = 'dress';
  6. $dress->price = '150';
  7.  
  8. $socks = new Product();
  9. $socks->id = '21';
  10. $socks->name = 'socks';
  11. $socks->price = '15';
  12.  
  13. $manager->addProduct($dress);
  14. $manager->addProduct($socks);
  15.  
  16.  
  17. $manager->saveAll();


Ten post edytował JacekJagiello 12.04.2014, 20:34:59
Go to the top of the page
+Quote Post

Posty w temacie


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: 18.05.2024 - 19:16