Completed
Push — master ( 1cb1c1...b2ea65 )
by El
05:50
created

AbstractModel::setData()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 6
cts 6
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 1
crap 2
1
<?php
2
/**
3
 * PrivateBin
4
 *
5
 * a zero-knowledge paste bin
6
 *
7
 * @link      https://github.com/PrivateBin/PrivateBin
8
 * @copyright 2012 Sébastien SAUVAGE (sebsauvage.net)
9
 * @license   https://www.opensource.org/licenses/zlib-license.php The zlib/libpng License
10
 * @version   1.1
11
 */
12
13
namespace PrivateBin\Model;
14
15
use Exception;
16
use PrivateBin\Configuration;
17
use PrivateBin\Data\AbstractData;
18
use PrivateBin\Sjcl;
19
use stdClass;
20
21
/**
22
 * AbstractModel
23
 *
24
 * Abstract model for PrivateBin objects.
25
 */
26
abstract class AbstractModel
27
{
28
    /**
29
     * Instance ID.
30
     *
31
     * @access protected
32
     * @var string
33
     */
34
    protected $_id = '';
35
36
    /**
37
     * Instance data.
38
     *
39
     * @access protected
40
     * @var stdClass
41
     */
42
    protected $_data;
43
44
    /**
45
     * Configuration.
46
     *
47
     * @access protected
48
     * @var Configuration
49
     */
50
    protected $_conf;
51
52
    /**
53
     * Data storage.
54
     *
55
     * @access protected
56
     * @var AbstractData
57
     */
58
    protected $_store;
59
60
    /**
61
     * Instance constructor.
62
     *
63
     * @access public
64
     * @param  Configuration $configuration
65
     * @param  AbstractData $storage
66
     * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
67
     */
68 92
    public function __construct(Configuration $configuration, AbstractData $storage)
69
    {
70 92
        $this->_conf       = $configuration;
71 92
        $this->_store      = $storage;
72 92
        $this->_data       = new stdClass;
73 92
        $this->_data->meta = new stdClass;
74 92
    }
75
76
    /**
77
     * Get ID.
78
     *
79
     * @access public
80
     * @return string
81
     */
82 83
    public function getId()
83
    {
84 83
        return $this->_id;
85
    }
86
87
    /**
88
     * Set ID.
89
     *
90
     * @access public
91
     * @param string $id
92
     * @throws Exception
93
     * @return void
94
     */
95 90
    public function setId($id)
96
    {
97 90
        if (!self::isValidId($id)) {
98 4
            throw new Exception('Invalid paste ID.', 60);
99
        }
100 86
        $this->_id = $id;
101 86
    }
102
103
    /**
104
     * Set data and recalculate ID.
105
     *
106
     * @access public
107
     * @param string $data
108
     * @throws Exception
109
     * @return void
110
     */
111 46
    public function setData($data)
112
    {
113 46
        if (!Sjcl::isValid($data)) {
114 1
            throw new Exception('Invalid data.', 61);
115
        }
116 45
        $this->_data->data = $data;
117
118
        // We just want a small hash to avoid collisions:
119
        // Half-MD5 (64 bits) will do the trick
120 45
        $this->setId(substr(hash('md5', $data), 0, 16));
121 45
    }
122
123
    /**
124
     * Get instance data.
125
     *
126
     * @access public
127
     * @return stdClass
128
     */
129
    abstract public function get();
130
131
    /**
132
     * Store the instance's data.
133
     *
134
     * @access public
135
     * @throws Exception
136
     * @return void
137
     */
138
    abstract public function store();
139
140
    /**
141
     * Delete the current instance.
142
     *
143
     * @access public
144
     * @throws Exception
145
     * @return void
146
     */
147
    abstract public function delete();
148
149
    /**
150
     * Test if current instance exists in store.
151
     *
152
     * @access public
153
     * @return bool
154
     */
155
    abstract public function exists();
156
157
    /**
158
     * Validate ID.
159
     *
160
     * @access public
161
     * @static
162
     * @param  string $id
163
     * @return bool
164
     */
165 92
    public static function isValidId($id)
166
    {
167 92
        return (bool) preg_match('#\A[a-f\d]{16}\z#', (string) $id);
168
    }
169
}
170