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
 delete
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
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.7.2
11 */
12
13namespace PrivateBin\Model;
14
15use Exception;
16use PrivateBin\Configuration;
17use PrivateBin\Data\AbstractData;
18
19/**
20 * AbstractModel
21 *
22 * Abstract model for PrivateBin objects.
23 */
24abstract 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    public function __construct(Configuration $configuration, AbstractData $storage)
66    {
67        $this->_conf       = $configuration;
68        $this->_store      = $storage;
69    }
70
71    /**
72     * Get ID.
73     *
74     * @access public
75     * @return string
76     */
77    public function getId()
78    {
79        return $this->_id;
80    }
81
82    /**
83     * Set ID.
84     *
85     * @access public
86     * @param string $id
87     * @throws Exception
88     */
89    public function setId($id)
90    {
91        if (!self::isValidId($id)) {
92            throw new Exception('Invalid paste ID.', 60);
93        }
94        $this->_id = $id;
95    }
96
97    /**
98     * Set data and recalculate ID.
99     *
100     * @access public
101     * @param  array $data
102     * @throws Exception
103     */
104    public function setData(array $data)
105    {
106        $data = $this->_sanitize($data);
107        $this->_validate($data);
108        $this->_data = $data;
109
110        // calculate a 64 bit checksum to avoid collisions
111        $this->setId(hash('fnv1a64', $data['ct']));
112    }
113
114    /**
115     * Get instance data.
116     *
117     * @access public
118     * @return array
119     */
120    public function get()
121    {
122        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    public static function isValidId($id)
158    {
159        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    protected function _validate(array $data)
179    {
180    }
181}