UserList::process()   B
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 31
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 31
rs 8.8571
cc 3
eloc 23
nc 4
nop 0
1
<?php
2
namespace Core\Modules\UserList;
3
4
use Frameworkless\Controllers;
5
use Donquixote\Cellbrush\Table\Table;
6
use Symfony\Component\HttpFoundation\Request;
7
8
/**
9
 * Description of UserListController
10
 *
11
 * @author d.lanec
12
 */
13
class UserList extends Controllers\ModuleController{
14
15
    /**
16
     * UserRepository 
17
     * @var \Core\Models\User\UserRepository 
18
     */
19
    protected $userRepository;
20
21
    /**
22
     * Request
23
     * @var Symfony\Component\HttpFoundation\Request 
24
     */
25
    protected $request;
26
27
    protected $limit = 5;
28
29
    function __construct(\Core\Models\User\UserRepository $userRepository, Request $request){
0 ignored issues
show
Bug introduced by
You have injected the Request via parameter $request. This is generally not recommended as there might be multiple instances during a request cycle (f.e. when using sub-requests). Instead, it is recommended to inject the RequestStack and retrieve the current request each time you need it via getCurrentRequest().
Loading history...
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...
30
	$this->userRepository	 = $userRepository;
31
	$this->request		 = $request;
0 ignored issues
show
Documentation Bug introduced by
It seems like $request of type object<Symfony\Component\HttpFoundation\Request> is incompatible with the declared type object<Core\Modules\User...HttpFoundation\Request> of property $request.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
32
    }
33
34
    public function process(){
35
36
	if($this->request->query->get('fn') == 'add'){
37
	    $this->add();
38
	}
39
40
	$Users = $this->userRepository->findMany([], $this->limit);
0 ignored issues
show
Documentation introduced by
$this->limit is of type integer, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
41
42
	$table	 = Table::create();
43
	$table->addColNames([0, 1, 2, 3]);
44
	$table->addClass('table table-striped');
45
	$table->thead()
46
		->addRowName('head row')
47
		->th('head row', 0, 'Id')
48
		->th('head row', 1, 'Регистрация')
49
		->th('head row', 2, 'Имя')
50
		->th('head row', 3, 'Email');
51
	$i	 = 0;
52
	foreach($Users as $User){
53
	    $table->addRow($i)->tdMultiple([
54
		$User->getId(),
55
		$User->getCreatedAt("d.m.Y"),
56
		$User->getName(),
57
		$User->getEmail()]);
58
	    $i++;
59
	}
60
61
	return $this->render('default', [
62
		    'table' => $table->render()
63
	]);
64
    }
65
66
    protected function add(){
67
68
	try{
69
70
	    $User = $this->userRepository->build();
71
	    $User->setEmail('[email protected]');
72
73
	    if(!$this->userRepository->save($User)){
74
		throw new Exception('User not save');
75
	    } else{
76
		$this->logger->info("Пользователь успешно сохранен!");
77
	    }
78
	} catch(\Frameworkless\Exceptions\ValidationException $ex){
79
80
	    foreach($ex->getFailures() as $failure){
81
		$this->logger->error("Property " . $failure->getPropertyPath() . ": " . $failure->getMessage() . "\n");
82
	    }
83
84
	    $this->logger->info("Произошла ошибка при сохранении пользователя");
85
	} catch(\Exception $ex){
86
87
	    $this->logger->error("system error:" . $ex->getMessage());
88
89
	    $this->logger->info("Произошла ошибка при сохранении пользователя");
90
	}
91
    }
92
}
93