Passed
Pull Request — master (#431)
by El
03:02
created

AbstractModel::_validate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 0
nc 1
nop 1
dl 0
loc 2
ccs 1
cts 1
cp 1
crap 1
rs 10
c 0
b 0
f 0
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.2.1
11
 */
12
13
namespace PrivateBin\Model;
14
15
use Exception;
16
use PrivateBin\Configuration;
17
use PrivateBin\Data\AbstractData;
18
19
/**
20
 * AbstractModel
21
 *
22
 * Abstract model for PrivateBin objects.
23
 */
24
abstract class AbstractModel
25
{
26
    /**
27
     * Instance ID.
28
     *
29
     * @access protected
30
     * @var string
31
     */
32
    protected $_id = '';
33
34
    /**
35
     * Instance data.
36
     *
37
     * @access protected
38
     * @var array
39
     */
40
    protected $_data = array('meta' => array());
41
42
    /**
43
     * Configuration.
44
     *
45
     * @access protected
46
     * @var Configuration
47
     */
48
    protected $_conf;
49
50
    /**
51
     * Data storage.
52
     *
53
     * @access protected
54
     * @var AbstractData
55
     */
56
    protected $_store;
57
58
    /**
59
     * Instance constructor.
60
     *
61
     * @access public
62
     * @param  Configuration $configuration
63
     * @param  AbstractData $storage
64
     */
65 81
    public function __construct(Configuration $configuration, AbstractData $storage)
66
    {
67 81
        $this->_conf       = $configuration;
68 81
        $this->_store      = $storage;
69 81
    }
70
71
    /**
72
     * Get ID.
73
     *
74
     * @access public
75
     * @return string
76
     */
77 72
    public function getId()
78
    {
79 72
        return $this->_id;
80
    }
81
82
    /**
83
     * Set ID.
84
     *
85
     * @access public
86
     * @param string $id
87
     * @throws Exception
88
     */
89 76
    public function setId($id)
90
    {
91 76
        if (!self::isValidId($id)) {
92 5
            throw new Exception('Invalid paste ID.', 60);
93
        }
94 71
        $this->_id = $id;
95 71
    }
96
97
    /**
98
     * Set data and recalculate ID.
99
     *
100
     * @access public
101
     * @param  array $data
102
     * @throws Exception
103
     */
104 40
    public function setData(array $data)
105
    {
106 40
        $data = $this->_sanitize($data);
107 40
        $this->_validate($data);
108 36
        $this->_data = $data;
109
110
        // calculate a 64 bit checksum to avoid collisions
111 36
        $this->setId(hash(version_compare(PHP_VERSION, '5.6', '<') ? 'fnv164' : 'fnv1a64', $data['ct']));
112 36
    }
113
114
    /**
115
     * Get instance data.
116
     *
117
     * @access public
118
     * @return array
119
     */
120 1
    public function get()
121
    {
122 1
        return $this->_data;
123
    }
124
125
    /**
126
     * Store the instance's data.
127
     *
128
     * @access public
129
     * @throws Exception
130
     */
131
    abstract public function store();
132
133
    /**
134
     * Delete the current instance.
135
     *
136
     * @access public
137
     * @throws Exception
138
     */
139
    abstract public function delete();
140
141
    /**
142
     * Test if current instance exists in store.
143
     *
144
     * @access public
145
     * @return bool
146
     */
147
    abstract public function exists();
148
149
    /**
150
     * Validate ID.
151
     *
152
     * @access public
153
     * @static
154
     * @param  string $id
155
     * @return bool
156
     */
157 79
    public static function isValidId($id)
158
    {
159 79
        return (bool) preg_match('#\A[a-f\d]{16}\z#', (string) $id);
160
    }
161
162
    /**
163
     * Sanitizes data to conform with current configuration.
164
     *
165
     * @access protected
166
     * @param  array $data
167
     * @return array
168
     */
169
    abstract protected function _sanitize(array $data);
170
171
    /**
172
     * Validate data.
173
     *
174
     * @access protected
175
     * @param  array $data
176
     * @throws Exception
177
     */
178 11
    protected function _validate(array $data)
179
    {
180 11
    }
181
}
182