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