Model   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 16
c 0
b 0
f 0
dl 0
loc 65
ccs 17
cts 17
cp 1
rs 10
wmc 7

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A getPaste() 0 7 2
A purge() 0 6 2
A getStore() 0 7 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.1
11
 */
12
13
namespace PrivateBin;
14
15
use PrivateBin\Model\Paste;
16
use PrivateBin\Persistence\PurgeLimiter;
17
18
/**
19
 * Model
20
 *
21
 * Factory of PrivateBin instance models.
22
 */
23
class 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 144
    public function __construct(Configuration $conf)
45
    {
46 144
        $this->_conf = $conf;
47
    }
48
49
    /**
50
     * Get a paste, optionally a specific instance.
51
     *
52
     * @param string $pasteId
53
     * @return Paste
54
     */
55 112
    public function getPaste($pasteId = null)
56
    {
57 112
        $paste = new Paste($this->_conf, $this->getStore());
58 112
        if ($pasteId !== null) {
59 78
            $paste->setId($pasteId);
60
        }
61 105
        return $paste;
62
    }
63
64
    /**
65
     * Checks if a purge is necessary and triggers it if yes.
66
     */
67 33
    public function purge()
68
    {
69 33
        PurgeLimiter::setConfiguration($this->_conf);
70 33
        PurgeLimiter::setStore($this->getStore());
71 33
        if (PurgeLimiter::canPurge()) {
72 33
            $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 121
    public function getStore()
82
    {
83 121
        if ($this->_store === null) {
84 121
            $class        = 'PrivateBin\\Data\\' . $this->_conf->getKey('class', 'model');
85 121
            $this->_store = new $class($this->_conf->getSection('model_options'));
86
        }
87 121
        return $this->_store;
88
    }
89
}
90