Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
13 / 13
100.00% covered (success)
100.00%
4 / 4
CRAP
100.00% covered (success)
100.00%
1 / 1
Model
100.00% covered (success)
100.00%
13 / 13
100.00% covered (success)
100.00%
4 / 4
7
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getPaste
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 purge
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 getStore
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
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.7.2
11 */
12
13namespace PrivateBin;
14
15use PrivateBin\Model\Paste;
16use PrivateBin\Persistence\PurgeLimiter;
17
18/**
19 * Model
20 *
21 * Factory of PrivateBin instance models.
22 */
23class Model
24{
25    /**
26     * Configuration.
27     *
28     * @var Configuration
29     */
30    private $_conf;
31
32    /**
33     * Data storage.
34     *
35     * @var Data\AbstractData
36     */
37    private $_store = null;
38
39    /**
40     * Factory constructor.
41     *
42     * @param configuration $conf
43     */
44    public function __construct(Configuration $conf)
45    {
46        $this->_conf = $conf;
47    }
48
49    /**
50     * Get a paste, optionally a specific instance.
51     *
52     * @param string $pasteId
53     * @return Paste
54     */
55    public function getPaste($pasteId = null)
56    {
57        $paste = new Paste($this->_conf, $this->getStore());
58        if ($pasteId !== null) {
59            $paste->setId($pasteId);
60        }
61        return $paste;
62    }
63
64    /**
65     * Checks if a purge is necessary and triggers it if yes.
66     */
67    public function purge()
68    {
69        PurgeLimiter::setConfiguration($this->_conf);
70        PurgeLimiter::setStore($this->getStore());
71        if (PurgeLimiter::canPurge()) {
72            $this->getStore()->purge($this->_conf->getKey('batchsize', 'purge'));
73        }
74    }
75
76    /**
77     * Gets, and creates if neccessary, a store object
78     *
79     * @return Data\AbstractData
80     */
81    public function getStore()
82    {
83        if ($this->_store === null) {
84            $class        = 'PrivateBin\\Data\\' . $this->_conf->getKey('class', 'model');
85            $this->_store = new $class($this->_conf->getSection('model_options'));
86        }
87        return $this->_store;
88    }
89}