CreateDoor.php 1.56 KB
Newer Older
Jacob Priddy's avatar
Jacob Priddy committed
1
2
3
4
5
<?php

namespace Source\UseCases\Doors\CreateDoor;

use Source\Entities\Door;
6
use Source\Sanitize\CastsTo;
Jacob Priddy's avatar
Jacob Priddy committed
7
8
9
10
11
12
use Source\Entities\HashedSearchable;
use Source\Gateways\Doors\DoorsRepository;
use Source\Gateways\Tokens\TokensRepository;

class CreateDoor implements CreateDoorUseCase
{
13
14
    use CastsTo;

Jacob Priddy's avatar
Jacob Priddy committed
15
16
17
18
19
    /**
     * @var \Source\Gateways\Doors\DoorsRepository
     */
    protected DoorsRepository $doors;

20
21
22
    /**
     * @var \Source\Gateways\Tokens\TokensRepository
     *
Jacob Priddy's avatar
Jacob Priddy committed
23
24
25
26
27
    /**
     * @var \Source\Gateways\Tokens\TokensRepository
     */
    protected TokensRepository $tokens;

28
29
    protected string $salt;

Jacob Priddy's avatar
Jacob Priddy committed
30
31
32
    /**
     * @param \Source\Gateways\Doors\DoorsRepository   $doors
     * @param \Source\Gateways\Tokens\TokensRepository $tokens
33
     * @param string                                   $salt
Jacob Priddy's avatar
Jacob Priddy committed
34
     */
35
    public function __construct(DoorsRepository $doors, TokensRepository $tokens, string $salt)
Jacob Priddy's avatar
Jacob Priddy committed
36
37
38
    {
        $this->doors = $doors;
        $this->tokens = $tokens;
39
        $this->salt = $salt;
Jacob Priddy's avatar
Jacob Priddy committed
40
41
42
43
44
45
46
47
48
49
50
51
52
    }

    /**
     * @inheritDoc
     */
    public function create(array $attributes, Presenter $presenter): void
    {
        $token = $this->tokens::generateTokenString();

        $door = new Door(
            0,
            $attributes['location'],
            $attributes['name'],
53
54
55
            HashedSearchable::hash($this->salt, $token),
            $attributes['public'] ?? false,
            $attributes['notes'] ?? null
Jacob Priddy's avatar
Jacob Priddy committed
56
57
        );

Jacob Priddy's avatar
Jacob Priddy committed
58
        $door = $this->doors->create($door);
Jacob Priddy's avatar
Jacob Priddy committed
59
60
61
62
63
64

        $response = new ResponseModel($door, $token);

        $presenter->present($response);
    }
}