Completed
Push — master ( 76c71b...91372c )
by dima
05:24
created

UserList::process()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 32
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 32
rs 8.8571
c 0
b 0
f 0
cc 3
eloc 22
nc 4
nop 1
1
<?php
2
namespace Core\Modules\UserList;
3
4
use Frameworkless\Controllers;
5
use Donquixote\Cellbrush\Table\Table;
6
7
/**
8
 * Description of UserListController
9
 *
10
 * @author d.lanec
11
 */
12
class UserList extends Controllers\ModuleController implements Controllers\ModuleInterface
13
{
14
15
	/**
16
	 *
17
	 * @var \Core\Models\User\UserRepository 
18
	 */
19
	protected $userRepository;
20
21
	function __construct(\Core\Models\User\UserRepository $userRepository)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
22
	{
23
		$this->userRepository = $userRepository;
24
	}
25
26
	public function process($args = [])
27
	{
28
29
		$this->logger->debug('UserList start');
30
31
		if ($args['fn'] == 'add') {
32
			$this->add();
33
		}
34
35
		$Users = $this->userRepository->findMany();
36
37
		$table	 = Table::create();
38
		$table->addColNames([0, 1, 2]);
39
		$table->addClass('table table-striped');
40
		$table->thead()
41
				->addRowName('head row')
42
				->th('head row', 0, 'Id')
43
				->th('head row', 1, 'Имя')
44
				->th('head row', 2, 'Email');
45
		$i		 = 0;
46
		foreach ($Users as $User) {
47
			$table->addRow($i)->tdMultiple([
48
				$User->getId(),
49
				$User->getName(),
50
				$User->getEmail()]);
51
			$i++;
52
		}
53
54
		return $this->render('default', [
55
					'table' => $table->render()
56
		]);
57
	}
58
59
	protected function add()
60
	{
61
62
		try {
63
64
			$User = $this->userRepository->build();
65
			$User->setEmail('[email protected]');
66
67
			if (!$this->UserRepository->save($User)) {
0 ignored issues
show
Bug introduced by
The property UserRepository does not seem to exist. Did you mean userRepository?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
68
				throw new Exception('User not save');
69
			} else {
70
				$this->logger->info("Пользователь успешно сохранен!");
71
			}
72
		} catch(\Frameworkless\Exceptions\ValidationException $ex) {
73
74
			foreach ($ex->getFailures() as $failure) {
75
				$this->logger->error("Property " . $failure->getPropertyPath() . ": " . $failure->getMessage() . "\n");
76
			}
77
78
			$this->logger->info("Произошла ошибка при сохранении пользователя");
79
		} catch(\Exception $ex) {
80
81
			$this->logger->error("system error:" . $ex->getMessage());
82
83
			$this->logger->info("Произошла ошибка при сохранении пользователя");
84
		}
85
86
	}
87
}
88