AbstractDataSource   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 127
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 13
c 0
b 0
f 0
lcom 1
cbo 0
dl 0
loc 127
ccs 0
cts 48
cp 0
rs 10

12 Methods

Rating   Name   Duplication   Size   Complexity  
getItems() 0 1 ?
getLayouts() 0 1 ?
getIncludes() 0 1 ?
A __construct() 0 6 1
A addItem() 0 3 1
A load() 0 6 1
A addUse() 0 17 4
A removeUse() 0 8 2
A setUp() 0 3 1
A tearDown() 0 3 1
A configure() 0 3 1
A process() 0 3 1
1
<?php
2
3
/*
4
 * This file is part of the Yosymfony\Spress.
5
 *
6
 * (c) YoSymfony <http://github.com/yosymfony>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Yosymfony\Spress\Core\DataSource;
13
14
/**
15
 * It is the superclass for all data sources.
16
 *
17
 * Data sources can load site data from certain locations.
18
 *
19
 * @api
20
 *
21
 * @author Victor Puertas <[email protected]>
22
 */
23
abstract class AbstractDataSource
24
{
25
    protected $referenceCounter;
26
    protected $params;
27
    protected $isConfigured;
28
29
    /**
30
     * Constructor.
31
     *
32
     * @param array $params Params for initialize the data source
33
     */
34
    public function __construct(array $params = [])
35
    {
36
        $this->params = $params;
37
        $this->referenceCounter = 0;
38
        $this->isConfigured = false;
39
    }
40
41
    /**
42
     * Returns the list of items.
43
     *
44
     * @return array Key-value array with Item's id as a key and the Item as value.
45
     */
46
    abstract public function getItems();
47
48
    /**
49
     * Returns the list of items with type "layout".
50
     *
51
     * @return array Key-value array with Item's id as a key and the Item as value.
52
     */
53
    abstract public function getLayouts();
54
55
    /**
56
     * Returns the list of items with type "include".
57
     *
58
     * @return array Key-value array with Item's id as a key and the Item as value.
59
     */
60
    abstract public function getIncludes();
61
62
    /**
63
     * Creates a new item or layout in the data source.
64
     *
65
     * @param Yosymfony\Spress\Core\DataSource\ItemInterface $item
66
     */
67
    public function addItem(ItemInterface $item)
68
    {
69
    }
70
71
    /**
72
     * Loads the data source.
73
     */
74
    public function load()
75
    {
76
        $this->addUse();
77
        $this->process();
78
        $this->removeUse();
79
    }
80
81
    /**
82
     * Marks as used the data source.
83
     * This method increases the internal reference count.
84
     * When the internal reference count goes from 0 to 1 setUp method
85
     * is invoked.
86
     */
87
    public function addUse()
88
    {
89
        if (false === $this->isConfigured) {
90
            $this->configure();
91
            $this->isConfigured = true;
92
        }
93
94
        if ($this->referenceCounter < 0) {
95
            $this->referenceCounter = 0;
96
        }
97
98
        if ($this->referenceCounter === 0) {
99
            $this->setUp();
100
        }
101
102
        ++$this->referenceCounter;
103
    }
104
105
    /**
106
     * Marks as unused the data source.
107
     * This method decreases the internal reference count.
108
     * When the internal reference count is 0 tearDown method
109
     * is invoked.
110
     */
111
    public function removeUse()
112
    {
113
        --$this->referenceCounter;
114
115
        if ($this->referenceCounter === 0) {
116
            $this->tearDown();
117
        }
118
    }
119
120
    /**
121
     * Brings up the connections to the data.
122
     * e.g: this is the ideal place to connect to the database.
123
     */
124
    public function setUp()
125
    {
126
    }
127
128
    /**
129
     * Brings down the connections to the data.
130
     * e.g: a database connection established in setUp should be closed in this method.
131
     */
132
    public function tearDown()
133
    {
134
    }
135
136
    /**
137
     * This method is used for setting up a site’s data source for the first time.
138
     */
139
    public function configure()
140
    {
141
    }
142
143
    /**
144
     * All data source data manipulations and queries.
145
     */
146
    public function process()
147
    {
148
    }
149
}
150