Passed
Push — master ( 5545f1...83deac )
by Kirill
03:22
created

AccessTrait::getAccess()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
/**
4
 * Spiral Framework.
5
 *
6
 * @license   MIT
7
 * @author    Anton Titov (Wolfy-J)
8
 */
9
10
declare(strict_types=1);
11
12
namespace Spiral\Reactor\Traits;
13
14
use Spiral\Reactor\AbstractDeclaration;
15
use Spiral\Reactor\Exception\ReactorException;
16
17
/**
18
 * Provides ability to set access level for element.
19
 */
20
trait AccessTrait
21
{
22
    /**
23
     * @var string
24
     */
25
    private $access = AbstractDeclaration::ACCESS_PRIVATE;
26
27
    /**
28
     * @param string $access
29
     *
30
     * @return $this
31
     * @throws ReactorException
32
     */
33
    public function setAccess(string $access): self
34
    {
35
        if (
36
            !in_array($access, [
37
            AbstractDeclaration::ACCESS_PRIVATE,
38
            AbstractDeclaration::ACCESS_PROTECTED,
39
            AbstractDeclaration::ACCESS_PUBLIC
40
            ], true)
41
        ) {
42
            throw new ReactorException("Invalid declaration level '{$access}'");
43
        }
44
45
        $this->access = $access;
46
47
        return $this;
48
    }
49
50
    /**
51
     * @return string
52
     */
53
    public function getAccess(): string
54
    {
55
        return $this->access;
56
    }
57
58
    /**
59
     * @return $this
60
     */
61
    public function setPublic(): self
62
    {
63
        $this->setAccess(AbstractDeclaration::ACCESS_PUBLIC);
64
65
        return $this;
66
    }
67
68
    /**
69
     * @return $this
70
     */
71
    public function setProtected(): self
72
    {
73
        $this->setAccess(AbstractDeclaration::ACCESS_PROTECTED);
74
75
        return $this;
76
    }
77
78
    /**
79
     * @return $this
80
     */
81
    public function setPrivate(): self
82
    {
83
        $this->setAccess(AbstractDeclaration::ACCESS_PRIVATE);
84
85
        return $this;
86
    }
87
}
88