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