-
Notifications
You must be signed in to change notification settings - Fork 451
/
AvatarCropperInstanceFactory.php
60 lines (47 loc) · 1.79 KB
/
AvatarCropperInstanceFactory.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
<?php
namespace Concrete\Core\User\Component;
use Concrete\Core\Application\ApplicationAwareInterface;
use Concrete\Core\Application\ApplicationAwareTrait;
use Concrete\Core\Config\Repository\Repository;
use Concrete\Core\Validation\CSRF\Token;
use Symfony\Component\HttpFoundation\Request;
class AvatarCropperInstanceFactory implements ApplicationAwareInterface
{
use ApplicationAwareTrait;
protected $tokenService;
protected $config;
public function __construct(Token $tokenService, Repository $config)
{
$this->tokenService = $tokenService;
$this->config = $config;
}
protected function getAccessTokenString(): string
{
return sprintf('avatar_cropper');
}
public function createInstance(): AvatarCropperInstance
{
$width = (int) $this->config->get('concrete.icons.user_avatar.width');
$height = (int) $this->config->get('concrete.icons.user_avatar.height');
$resolutionMultiplier = (int) $this->config->get('concrete.icons.user_avatar.resolution', 1);
$width = $width * $resolutionMultiplier;
$height = $height * $resolutionMultiplier;
$accessToken = $this->tokenService->generate($this->getAccessTokenString());
$instance = $this->app->make(AvatarCropperInstance::class);
$instance->setAccessToken($accessToken);
$instance->setWidth($width);
$instance->setHeight($height);
return $instance;
}
public function createInstanceFromRequest(Request $request)
{
return $this->createInstance();
}
public function instanceMatchesAccessToken(AvatarCropperInstance $instance, string $accessToken): bool
{
return $this->tokenService->validate(
$this->getAccessTokenString(),
$accessToken
);
}
}