Completed
Push — master ( f8f581...ddd2bd )
by Christopher
13s
created

InstallController   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 102
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 10

Importance

Changes 0
Metric Value
dl 0
loc 102
rs 10
c 0
b 0
f 0
wmc 6
lcom 1
cbo 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getFirstUserForm() 0 14 2
B postFirstUserForm() 0 34 2
A populateDefaultSettings() 0 49 2
1
<?php
2
3
namespace TechWilk\Rota\Controller;
4
5
use Locale;
6
use Psr\Http\Message\ResponseInterface;
7
use Psr\Http\Message\ServerRequestInterface;
8
use TechWilk\Rota\Crypt;
9
use TechWilk\Rota\Settings;
10
use TechWilk\Rota\SettingsQuery;
11
use TechWilk\Rota\Site;
12
use TechWilk\Rota\User;
13
use TechWilk\Rota\UserQuery;
14
15
class InstallController extends BaseController
16
{
17
    public function getFirstUserForm(ServerRequestInterface $request, ResponseInterface $response, $args)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $args is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
18
    {
19
        $this->logger->info("Fetch first user form GET '/install/user'");
20
21
        // don't run if we're already installed
22
        $existingUserCount = UserQuery::create()->count();
23
        if ($existingUserCount > 0) {
24
            return $response->withStatus(302)->withHeader('Location', $this->router->pathFor('login'));
25
        }
26
27
        return $this->view->render($response, 'login-sign-up.twig', [
28
            'message' => 'Your password will be auto-generated and displayed on the next screen.',
29
        ]);
30
    }
31
32
    public function postFirstUserForm(ServerRequestInterface $request, ResponseInterface $response, $args)
0 ignored issues
show
Unused Code introduced by
The parameter $args is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
33
    {
34
        $this->logger->info("Fetch first user form GET '/install/user'");
35
36
        // don't run if we're already installed
37
        $existingUserCount = UserQuery::create()->count();
38
        if ($existingUserCount > 0) {
39
            return $response->withStatus(302)->withHeader('Location', $this->router->pathFor('login'));
40
        }
41
42
        $data = $request->getParsedBody();
43
44
        $user = new User();
45
46
        $user->setFirstName($data['firstName']);
47
        $user->setLastName($data['lastName']);
48
        $user->setEmail($data['email']);
49
50
        $user->setIsAdmin(true);
51
        $user->setIsOverviewRecipient(true);
52
        $user->setRecieveReminderEmails(true);
53
54
        $password = Crypt::generateToken(20);
55
        $user->setPassword($password);
56
57
        $user->save();
58
59
        $this->populateDefaultSettings();
60
61
        return $this->view->render($response, 'login-credentials.twig', [
62
            'username' => $user->getEmail(),
63
            'message'  => 'Your password is: '.$password.' Copy it somewhere safe and you can change it once you\'ve logged in.',
64
        ]);
65
    }
66
67
    protected function populateDefaultSettings()
68
    {
69
        // don't run if we're already got settings
70
        $existingSettings = SettingsQuery::create()->count();
71
        if ($existingSettings > 0) {
72
            return;
73
        }
74
75
        $settings = new Settings();
76
77
        $site = new Site();
78
        $url = $site->getUrl()['base'].$this->router->pathFor('home');
79
        $settings->setSiteUrl($url);
80
81
        $settings->setOwner('Rota');
82
83
        $settings->setTimeZone(date_default_timezone_get());
84
        $settings->setLangLocale(Locale::getDefault());
85
86
        $settings->setTimeFormatLong('%A, %B %e @ %I:%M %p');
87
        $settings->setTimeFormatNormal('%d/%m/%y %I:%M %p');
88
        $settings->setTimeFormatShort('%a, <strong>%b %e</strong>, %I:%M %p');
89
        $settings->setTimeOnlyFormat('%l %M %p');
90
        $settings->setDateOnlyFormat('%A');
91
        $settings->setDayOnlyFormat('%A, %B %e');
92
93
        $settings->setNotificationEmail(<<<'EMAIL'
94
Dear [name]
95
96
This is an automatic reminder.
97
98
You have the roles: [rotaoutput]
99
During the service on [date] in [location].
100
[eventdetails]
101
102
If you have arranged a swap, please let us know.
103
104
Many thanks for your continued service!
105
EMAIL
106
);
107
108
        $settings->setToken(Crypt::generateToken(100));
109
        $settings->setSkin('skin-blue');
110
111
        // @todo remove unused legacy settings
112
        $settings->setDebugMode(false);
113
114
        $settings->save();
115
    }
116
}
117