11<?php
22namespace App \Manager ;
33
4- use App \DB \Connection ;
54use App \DB \QueryBuilder ;
65use App \Entity \Type \Invoice ;
76use App \Entity \Type \Status ;
87use App \Hydration \StatusHydrator ;
9- use App \Repository \Type \Status as Repository ;
8+ use App \Repository \Type \InvoiceRepository ;
109
1110class InvoiceManager extends AbstractManager
1211{
1312 /**
14- * @var Repository
13+ * @var InvoiceRepository
1514 */
1615 private $ repository ;
1716
1817 /**
19- * @var Connection
18+ * InvoiceManager constructor.
19+ * @param InvoiceRepository $repository
2020 */
21- private $ connection ;
22-
23- /**
24- * CustomerManager constructor.
25- * @param Connection $connection
26- */
27- public function __construct (Connection $ connection )
21+ public function __construct (InvoiceRepository $ repository )
2822 {
29- $ this ->connection = $ connection ;
23+ $ this ->repository = $ repository ;
3024 }
3125
3226
@@ -47,35 +41,7 @@ public function findOne(int $id): Status
4741 */
4842 public function save (Invoice $ entity ):Invoice
4943 {
50- $ data = [
51- 'reference ' => $ entity ->getReference (),
52- 'total ' => $ entity ->getTotal (),
53- 'vat ' => $ entity ->getVat ()
54- ];
55- if ($ entity ->getStatus () instanceof Status){
56- $ data ['status_id ' ] = $ entity ->getStatus ()->getId ();
57- }
58- $ where = [];
59- if (null !== $ entity ->getId ()) {
60- $ where ['id ' ] = $ entity ->getId ();
61- }
62-
63- $ table = 'invoice ' ;
64-
65- $ sql = QueryBuilder::insertOrUpdate ($ data , $ table , $ where );
66-
67- if (null !== $ entity ->getId ()) {
68- $ data ['id ' ] = $ entity ->getId ();
69- }
70- $ dbCon = $ this ->connection ->open ();
71-
72- $ statement = $ dbCon ->prepare ($ sql );
73- $ statement ->execute (array_values ($ data ));
74-
75- if (null === $ entity ->getId ()){
76- $ entity ->setId ($ dbCon ->lastInsertId ());
77- }
78-
79- return $ entity ;
44+ $ savedEntity = $ this ->repository ->save ($ entity );
45+ return $ savedEntity ;
8046 }
8147}
0 commit comments