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