1
+ <?php
2
+ /**
3
+ * Created by PhpStorm.
4
+ * User: francisc
5
+ * Date: 23/07/15
6
+ * Time: 21:54
7
+ */
8
+
9
+ namespace Application \Controller ;
10
+
11
+ use CleanPhp \Invoicer \Domain \Repository \OrderRepositoryInterface ;
12
+ use CleanPhp \Invoicer \Domain \Repository \CustomerRepositoryInterface ;
13
+ use CleanPhp \Invoicer \Domain \Entity \Order ;
14
+ use Zend \Mvc \Controller \AbstractActionController ;
15
+ use Zend \View \Model \ViewModel ;
16
+ use CleanPhp \Invoicer \Persistence \Hydrator \OrderHydrator ;
17
+ use CleanPhp \Invoicer \Service \InputFilter \OrderInputFilter ;
18
+
19
+ class OrdersController extends AbstractActionController
20
+ {
21
+ protected $ orderRepository ;
22
+ protected $ customerRepository ;
23
+ protected $ inputFilter ;
24
+ protected $ hydrator ;
25
+
26
+ public function __construct (OrderRepositoryInterface $ orderRepository , CustomerRepositoryInterface $ customerRepository , OrderInputFilter $ inputFilter ,
27
+ OrderHydrator $ hydrator
28
+ )
29
+ {
30
+ $ this ->orderRepository = $ orderRepository ;
31
+ $ this ->customerRepository = $ customerRepository ;
32
+ $ this ->inputFilter = $ inputFilter ;
33
+ $ this ->hydrator = $ hydrator ;
34
+ }
35
+
36
+ public
37
+ function indexAction ()
38
+ {
39
+ return [
40
+ 'orders ' => $ this ->orderRepository ->getAll ()
41
+ ];
42
+ }
43
+
44
+ public
45
+ function viewAction ()
46
+ {
47
+ $ id = $ this ->params ()->fromRoute ('id ' );
48
+ $ order = $ this ->orderRepository ->getById ($ id );
49
+
50
+ if (!$ order ) {
51
+ $ this ->getResponse ()->setStatusCode (404 );
52
+ return null ;
53
+ }
54
+
55
+ return [
56
+ 'order ' => $ order
57
+ ];
58
+ }
59
+
60
+ public function newAction ()
61
+ {
62
+ $ viewModel = new ViewModel ();
63
+ $ order = new Order ();
64
+ if ($ this ->getRequest ()->isPost ()) {
65
+ $ this ->inputFilter
66
+ ->setData ($ this ->params ()->fromPost ());
67
+ if ($ this ->inputFilter ->isValid ()) {
68
+ $ order = $ this ->hydrator ->hydrate ($ this ->inputFilter ->getValues (),
69
+ $ order
70
+ );
71
+ $ this ->orderRepository ->begin ()
72
+ ->persist ($ order )
73
+ ->commit ();
74
+ $ this ->flashMessenger ()->addSuccessMessage ('Order Created ' );
75
+ $ this ->redirect ()->toUrl ('/orders/view/ ' . $ order ->getId ());
76
+ } else {
77
+ $ this ->hydrator ->hydrate (
78
+ $ this ->params ()->fromPost (),
79
+ $ order
80
+ );
81
+ $ viewModel ->setVariable (
82
+ 'errors ' ,
83
+ $ this ->inputFilter ->getMessages ()
84
+ );
85
+ }
86
+ }
87
+ $ viewModel ->setVariable (
88
+ 'customers ' ,
89
+ $ this ->customerRepository ->getAll ()
90
+ );
91
+ $ viewModel ->setVariable ('order ' , $ order );
92
+ return $ viewModel ;
93
+ }
94
+ }
0 commit comments