|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Admin\Controller; |
|
4
|
|
|
|
|
5
|
|
|
use Zend\Expressive\Template\TemplateRendererInterface as Template; |
|
6
|
|
|
use Zend\Diactoros\Response\HtmlResponse; |
|
7
|
|
|
use Admin\Model\Repository\ArticleRepositoryInterface; |
|
8
|
|
|
use Admin\Validator\ValidatorInterface as Validator; |
|
9
|
|
|
use Ramsey\Uuid\Uuid; |
|
10
|
|
|
use Zend\Session\SessionManager; |
|
11
|
|
|
use Zend\Expressive\Router\RouterInterface as Router; |
|
12
|
|
|
|
|
13
|
|
|
class ArticleController extends AbstractController |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* @var Template |
|
17
|
|
|
*/ |
|
18
|
|
|
private $template; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @var \Admin\Model\Repository\ArticleRepositoryInterface |
|
22
|
|
|
*/ |
|
23
|
|
|
private $articleRepo; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var \Zend\Validator\AbstractValidator |
|
27
|
|
|
*/ |
|
28
|
|
|
private $validator; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var SessionManager |
|
32
|
|
|
*/ |
|
33
|
|
|
private $session; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @var ROUTER |
|
37
|
|
|
*/ |
|
38
|
|
|
private $router; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* ArticleController constructor. |
|
42
|
|
|
* |
|
43
|
|
|
* @param Template $template |
|
44
|
|
|
* @param ArticleRepositoryInterface $articleRepo |
|
45
|
|
|
* @param Validator $validator |
|
46
|
|
|
* @param SessionManager $session |
|
47
|
|
|
* @param Router $routes |
|
|
|
|
|
|
48
|
|
|
*/ |
|
49
|
|
|
public function __construct( |
|
50
|
|
|
Template $template, |
|
51
|
|
|
ArticleRepositoryInterface $articleRepo, |
|
52
|
|
|
Validator $validator, |
|
53
|
|
|
SessionManager $session, |
|
54
|
|
|
Router $router |
|
55
|
|
|
) { |
|
56
|
|
|
$this->template = $template; |
|
57
|
|
|
$this->articleRepo = $articleRepo; |
|
58
|
|
|
$this->validator = $validator; |
|
|
|
|
|
|
59
|
|
|
$this->session = $session; |
|
60
|
|
|
$this->router = $router; |
|
|
|
|
|
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
public function index() : HtmlResponse |
|
64
|
|
|
{ |
|
65
|
|
|
$articleCollection = $this->articleRepo->fetchAllArticles(); |
|
66
|
|
|
|
|
67
|
|
|
$data = [ |
|
68
|
|
|
'message' => 'Article list', |
|
69
|
|
|
'articleCollection' => $articleCollection, |
|
70
|
|
|
]; |
|
71
|
|
|
|
|
72
|
|
|
return new HtmlResponse($this->template->render('admin::article/index', $data)); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
public function create() : \Psr\Http\Message\ResponseInterface |
|
76
|
|
|
{ |
|
77
|
|
|
$data = [ |
|
78
|
|
|
'message' => 'Create article', |
|
79
|
|
|
'errors' => false, |
|
80
|
|
|
'data' => $this->request->getParsedBody() |
|
81
|
|
|
]; |
|
82
|
|
|
|
|
83
|
|
|
if (count($data['data']) > 0) { |
|
84
|
|
|
$user = $this->session->getStorage()->user; |
|
|
|
|
|
|
85
|
|
|
try { |
|
86
|
|
|
$article = new \Admin\Model\Entity\ArticleEntity(); |
|
87
|
|
|
//@TODO fix this |
|
88
|
|
|
$dt = new \DateTime('now'); |
|
89
|
|
|
$data['data']['created_at'] = $dt->format('Y-m-d H:i:s'); |
|
90
|
|
|
|
|
91
|
|
|
//generate uuid |
|
92
|
|
|
$data['data']['article_uuid'] = hex2bin(Uuid::uuid1()->getHex()); |
|
93
|
|
|
$data['user_uuid'] = $user->admin_user_uuid; |
|
94
|
|
|
$this->validator->validate($data['data']); |
|
|
|
|
|
|
95
|
|
|
$article->exchangeArray($data['data']); |
|
96
|
|
|
if ($this->articleRepo->createArticle($article)) { |
|
97
|
|
|
return $this->response->withStatus(302)->withHeader( |
|
98
|
|
|
'Location', |
|
99
|
|
|
$this->router->generateUri('admin.articles', ['action' => 'index']) |
|
100
|
|
|
); |
|
101
|
|
|
} |
|
102
|
|
|
//@TODO there was an error saving article, set a flesh message for user |
|
103
|
|
|
|
|
104
|
|
|
// handle validation errors |
|
105
|
|
|
} catch (\Admin\Validator\ValidatorException $e) { |
|
106
|
|
|
$data['errors'] = $this->validator->getMessages(); |
|
107
|
|
|
|
|
108
|
|
|
// handle other errors |
|
109
|
|
|
} catch (\Exception $e) { |
|
110
|
|
|
var_dump($e->getMessage()); |
|
|
|
|
|
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
return new HtmlResponse($this->template->render('admin::article/create', $data)); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
public function doCreate() |
|
118
|
|
|
{ |
|
119
|
|
|
|
|
120
|
|
|
|
|
121
|
|
|
} |
|
122
|
|
|
} |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.