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