Completed
Push — master ( 3c1969...5b5792 )
by Schlaefer
03:16 queued 10s
created

ManualBlocker   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 58
rs 10
c 0
b 0
f 0
wmc 5
lcom 1
cbo 2

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getReason() 0 4 1
A block() 0 22 3
1
<?php
2
3
declare(strict_types = 1);
4
5
/**
6
 * Saito - The Threaded Web Forum
7
 *
8
 * @copyright Copyright (c) the Saito Project Developers
9
 * @link https://github.com/Schlaefer/Saito
10
 * @license http://opensource.org/licenses/MIT
11
 */
12
13
namespace Saito\User\Blocker;
14
15
/**
16
 * Manually block a user through a admin/mod action
17
 */
18
class ManualBlocker extends BlockerAbstract
19
{
20
21
    /** @var int admin-ID */
22
    private $adminId;
23
24
    /** @var int duration in seconds */
25
    private $duration;
26
27
    /**
28
     * Constructor
29
     *
30
     * @param int $adminId user-ID of the person performing the block operation
31
     * @param int|null $duration Time in seconds how long the block should be active
32
     */
33
    public function __construct(int $adminId, ?int $duration = null)
34
    {
35
        $this->adminId = $adminId;
36
        $this->duration = $duration;
37
    }
38
39
    /**
40
     * {@inheritDoc}
41
     */
42
    public function getReason()
43
    {
44
        return 1;
45
    }
46
47
    /**
48
     * {@inheritdoc}
49
     *
50
     * @throws \InvalidArgumentException
51
     * @throws \Exception
52
     */
53
    public function block(int $userId): bool
54
    {
55
        $user = $this->Table->Users->get($userId);
56
        if (empty($user)) {
57
            throw new \InvalidArgumentException;
58
        }
59
60
        $conditions = [
61
            'blocked_by_user_id' => $this->adminId,
62
            'ended IS' => null,
63
            'reason' => $this->getReason(),
64
            'user_id' => $userId
65
        ];
66
67
        if ($this->duration) {
68
            $conditions['ends'] = bDate(time() + $this->duration);
69
        }
70
71
        $entity = $this->Table->newEntity($conditions);
72
73
        return (bool)$this->Table->save($entity);
74
    }
75
}
76