Passed
Pull Request — master (#53)
by Matthew
04:39
created

UsersService   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 4
eloc 33
dl 0
loc 54
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 2 1
A GenerateNewInviteLink() 0 5 1
A InviteNewUser() 0 40 2
1
<?php
2
namespace PhpDraft\Domain\Services;
3
4
use Silex\Application;
5
use Symfony\Component\HttpFoundation\Request;
6
use PhpDraft\Domain\Entities\LoginUser;
7
use PhpDraft\Domain\Models\PhpDraftResponse;
8
use PhpDraft\Domain\Models\MailMessage;
9
10
class UsersService {
11
  private $app;
12
13
  public function __construct(Application $app) {
14
    $this->app = $app;
15
  }
16
17
  public function InviteNewUser(LoginUser $user, $message) {
18
    $response = new PhpDraftResponse();
19
20
    $user->password = $this->app['phpdraft.SaltService']->GenerateSalt();
21
    $user->salt = $this->app['phpdraft.SaltService']->GenerateSalt();
22
    $user->roles = array('ROLE_COMMISH');
23
    $user->verificationKey = $this->app['phpdraft.SaltService']->GenerateSaltForUrl();
24
25
    try {
26
      $user = $this->app['phpdraft.LoginUserRepository']->Create($user);
27
      $inviter = $this->app['phpdraft.LoginUserService']->GetCurrentUser();
28
29
      $emailParameters = array(
30
        'imageBaseUrl' => sprintf("%s/images/email", APP_BASE_URL),
0 ignored issues
show
Bug introduced by
The constant PhpDraft\Domain\Services\APP_BASE_URL was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
31
        'invitee' => $user->name,
32
        'inviter' => $inviter->name,
33
        'message' => $message,
34
        'setupLink' => $this->GenerateNewInviteLink($user),
35
      );
36
37
      $mailMessage = new MailMessage();
38
39
      $mailMessage->to_addresses = array(
40
        $user->email => $user->name,
41
      );
42
43
      $mailMessage->subject = "$inviter->name invited you to use Hoot Draft!";
44
      $mailMessage->body = $this->app['phpdraft.TemplateRenderService']->RenderTemplate('UserInvite.html', $emailParameters);
45
      $mailMessage->is_html = true;
46
47
      $this->app['phpdraft.EmailService']->SendMail($mailMessage);
48
49
      $response->success = true;
50
    } catch (\Exception $ex) {
51
      $exceptionMessage = $ex->getMessage();
52
      $response->success = false;
53
      $response->errors[] = $exceptionMessage;
54
    }
55
56
    return $response;
57
  }
58
59
  private function GenerateNewInviteLink(LoginUser $user) {
60
    $encodedEmail = urlencode($user->email);
61
    $encodedToken = urlencode($user->verificationKey);
62
63
    return sprintf("%s/inviteSetup/%s/%s", APP_BASE_URL, $encodedEmail, $encodedToken);
0 ignored issues
show
Bug introduced by
The constant PhpDraft\Domain\Services\APP_BASE_URL was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
64
  }
65
}
66