Conditions | 4 |
Paths | 4 |
Total Lines | 78 |
Code Lines | 54 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
52 | public function indexAction(Request $request) |
||
53 | { |
||
54 | if ($this->container->hasParameter('db_config_loaded')) { |
||
55 | return new RedirectResponse($this->router->generate('index_index')); |
||
56 | } |
||
57 | |||
58 | $template = file_get_contents(BASE_DIR . join(DIRECTORY_SEPARATOR, ['majima', 'Resources', 'views', 'Install', 'index.html'])); |
||
|
|||
59 | |||
60 | $params = [ |
||
61 | 'db_host' => $request->get('db_host'), |
||
62 | 'db_port' => $request->get('db_port'), |
||
63 | 'db_name' => $request->get('db_name'), |
||
64 | 'db_user' => $request->get('db_user'), |
||
65 | 'db_passw' => $request->get('db_passw'), |
||
66 | 'admin_user' => $request->get('admin_user'), |
||
67 | 'admin_passw' => $request->get('admin_passw'), |
||
68 | ]; |
||
69 | |||
70 | foreach ($params as $param) { |
||
71 | if (empty($param)) { |
||
72 | return new Response( |
||
73 | $template, |
||
74 | 200, |
||
75 | ['Content-Type' => 'text/html'] |
||
76 | ); |
||
77 | } |
||
78 | } |
||
79 | |||
80 | $defaultEncoder = new MessageDigestPasswordEncoder('sha512', true, 5000); |
||
81 | $adminUser = $params['admin_user']; |
||
82 | $adminSha1 = sha1(rand(100000, 999999)); |
||
83 | $adminPassword = $defaultEncoder->encodePassword($params['admin_passw'], $adminSha1); |
||
84 | |||
85 | unset($params['admin_user']); |
||
86 | unset($params['admin_passw']); |
||
87 | |||
88 | $configPath = BASE_DIR . join(DIRECTORY_SEPARATOR, ['var', 'conf', 'config.json']); |
||
89 | |||
90 | file_put_contents($configPath, json_encode($params, JSON_PRETTY_PRINT)); |
||
91 | |||
92 | $fs = new Filesystem(); |
||
93 | $fs->remove($this->container->getParameter('kernel.cache_dir')); |
||
94 | |||
95 | $sql = file_get_contents(BASE_DIR . join(DIRECTORY_SEPARATOR, ['majima', 'Resources', 'sql', 'install.sql'])); |
||
96 | |||
97 | $pdo = new \PDO( |
||
98 | sprintf( |
||
99 | "mysql:host=%s;port=%s;dbname=%s", |
||
100 | $params['db_host'], |
||
101 | $params['db_port'], |
||
102 | $params['db_name'] |
||
103 | ), |
||
104 | $params['db_user'], |
||
105 | $params['db_passw'] |
||
106 | ); |
||
107 | $pdo->exec($sql); |
||
108 | |||
109 | $qb = new \FluentPDO($pdo); |
||
110 | $qb->insertInto('users') |
||
111 | ->values([ |
||
112 | 'name' => $adminUser, |
||
113 | 'password' => $adminPassword, |
||
114 | 'salt' => $adminSha1, |
||
115 | ]) |
||
116 | ->execute(); |
||
117 | |||
118 | $qb->insertInto('users_roles') |
||
119 | ->values([ |
||
120 | 'userID' => 1, |
||
121 | 'role' => 'ROLE_ADMIN', |
||
122 | ]) |
||
123 | ->execute(); |
||
124 | |||
125 | $template = file_get_contents(BASE_DIR . join(DIRECTORY_SEPARATOR, ['majima', 'Resources', 'views', 'Install', 'success.html'])); |
||
126 | return new Response( |
||
127 | $template, |
||
128 | 200, |
||
129 | ['Content-Type' => 'text/html'] |
||
130 | ); |
||
132 | } |