Completed
Pull Request — master (#79)
by Christopher
07:04
created

InstallController::getInstall()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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