Completed
Push — master ( 62a734...19902b )
by Anton
05:06
created

AbstractIdentityNode::getType()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace Covery\Client\Identities;
4
5
use Covery\Client\IdentityNode;
6
7
/**
8
 * Class AbstractIdentityNode
9
 *
10
 * AbstractIdentityNode is template for identities usage
11
 * You must extend it and hardcode type before usage, for example:
12
 *
13
 * class WebsiteIdentity extends AbstractIdentityNode
14
 * {
15
 *   public function __construct($id)
16
 *   {
17
 *      parent::__construct("website", $id);
18
 *   }
19
 * }
20
 *
21
 * @package Covery\Client
22
 */
23
abstract class AbstractIdentityNode implements IdentityNode
24
{
25
    /**
26
     * @var string
27
     */
28
    private $type;
29
30
    /**
31
     * @var int
32
     */
33
    private $id;
34
35
    /**
36
     * AbstractIdentityNode constructor.
37
     *
38
     * @param string $type
39
     * @param int $id
40
     */
41
    public function __construct($type, $id)
42
    {
43
        if (!is_string($type)) {
44
            throw new \InvalidArgumentException('Identity node type must be string');
45
        } elseif ($type === '') {
46
            throw new \InvalidArgumentException('Identity node type must be not empty');
47
        }
48
49
        if (!is_int($id)) {
50
            throw new \InvalidArgumentException('Identity node ID must be integer');
51
        } elseif ($id < 1) {
52
            throw new \InvalidArgumentException('Identity node ID must be positive non-zero integer');
53
        }
54
55
        $this->type = $type;
56
        $this->id = $id;
57
    }
58
59
    /**
60
     * Returns Identity type
61
     *
62
     * @return string
63
     */
64
    public function getType()
65
    {
66
        return $this->type;
67
    }
68
69
    /**
70
     * Returns Identity id
71
     *
72
     * @return int
73
     */
74
    public function getId()
75
    {
76
        return $this->id;
77
    }
78
79
    /**
80
     * Returns string representation of identity
81
     *
82
     * @return string
83
     */
84
    public function __toString()
85
    {
86
        return sprintf(
87
            'Identity %s=%d',
88
            $this->getType(),
89
            $this->getId()
90
        );
91
    }
92
}
93