Passed
Pull Request — master (#53)
by Matthew
02:43
created

UsersService   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 4
eloc 35
dl 0
loc 56
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 42 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
      $setupLink = $this->GenerateNewInviteLink($user);
30
      $emailParameters = array(
31
        '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...
32
        'invitee' => $user->name,
33
        'inviter' => $inviter->name,
34
        'message' => $message,
35
        'setupLink' => $setupLink,
36
      );
37
38
      $mailMessage = new MailMessage();
39
40
      $mailMessage->to_addresses = array(
41
        $user->email => $user->name,
42
      );
43
44
      $mailMessage->subject = "$inviter->name invited you to use Hoot Draft!";
45
      $mailMessage->body = $this->app['phpdraft.TemplateRenderService']->RenderTemplate('UserInvite.html', $emailParameters);
46
      $mailMessage->altBody = "Hello partner! $inviter->name invited you to use Hoot Draft, a whizz banger of an app that makes offline fantasy drafts a real hoot! Setup your new account here: $setupLink";
47
      $mailMessage->is_html = true;
48
49
      $this->app['phpdraft.EmailService']->SendMail($mailMessage);
50
51
      $response->success = true;
52
    } catch (\Exception $ex) {
53
      $exceptionMessage = $ex->getMessage();
54
      $response->success = false;
55
      $response->errors[] = $exceptionMessage;
56
    }
57
58
    return $response;
59
  }
60
61
  private function GenerateNewInviteLink(LoginUser $user) {
62
    $encodedEmail = urlencode($user->email);
63
    $encodedToken = urlencode($user->verificationKey);
64
65
    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...
66
  }
67
}
68