Repository   A
last analyzed

Complexity

Total Complexity 22

Size/Duplication

Total Lines 237
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 111
dl 0
loc 237
rs 10
c 0
b 0
f 0
wmc 22

12 Methods

Rating   Name   Duplication   Size   Complexity  
A getContentRepository() 0 3 1
A __get() 0 13 4
A init() 0 9 1
A __set() 0 9 2
A initContentDb() 0 5 1
A initConfigIfNotExists() 0 9 2
A __construct() 0 8 3
A save() 0 6 1
A initConfigStorage() 0 15 1
A saveSubset() 0 13 3
A getContentDbHandle() 0 6 2
A loadSubset() 0 7 1
1
<?php
2
/**
3
 * User: Jens
4
 * Date: 30-1-2017
5
 * Time: 20:15
6
 */
7
8
namespace CloudControl\Cms\storage;
9
10
use CloudControl\Cms\storage\repository\ContentRepository;
11
12
/**
13
 * Class Repository
14
 * @package CloudControl\Cms\storage
15
 * @property array sitemap
16
 * @property array applicationComponents
17
 * @property array documentTypes
18
 * @property array bricks
19
 * @property array imageSet
20
 * @property array images
21
 * @property array files
22
 * @property array users
23
 * @property array valuelists
24
 * @property array redirects
25
 * @property array activityLog
26
 */
27
class Repository
28
{
29
    protected $storagePath;
30
31
    protected $fileBasedSubsets = array(
32
        'sitemap',
33
        'applicationComponents',
34
        'documentTypes',
35
        'bricks',
36
        'imageSet',
37
        'images',
38
        'files',
39
        'users',
40
        'valuelists',
41
        'redirects',
42
        'activityLog'
43
    );
44
45
    protected $sitemap;
46
    protected $sitemapChanges = false;
47
48
    protected $applicationComponents;
49
    protected $applicationComponentsChanges = false;
50
51
    protected $documentTypes;
52
    protected $documentTypesChanges = false;
53
54
    protected $bricks;
55
    protected $bricksChanges = false;
56
57
    protected $imageSet;
58
    protected $imageSetChanges = false;
59
60
    protected $images;
61
    protected $imagesChanges = false;
62
63
    protected $files;
64
    protected $filesChanges = false;
65
66
    protected $users;
67
    protected $usersChanges = false;
68
69
    protected $valuelists;
70
    protected $valuelistsChanges = false;
71
72
    protected $redirects;
73
    protected $redirectsChanges = false;
74
75
    protected $activityLog;
76
    protected $activityLogChanges = false;
77
78
    protected $contentDbHandle;
79
80
    /**
81
     * @var ContentRepository
82
     */
83
    protected $contentRepository;
84
85
86
    /**
87
     * Repository constructor.
88
     * @param $storagePath
89
     * @throws \Exception
90
     */
91
    public function __construct($storagePath)
92
    {
93
        $storagePath = realpath($storagePath);
94
        if (is_dir($storagePath) && $storagePath !== false) {
95
            $this->storagePath = $storagePath;
96
            $this->contentRepository = new ContentRepository($storagePath);
97
        } else {
98
            throw new \Exception('Repository not yet initialized.');
99
        }
100
    }
101
102
    /**
103
     * Initiates default storage
104
     * @param $baseStorageDefaultPath
105
     * @param $baseStorageSqlPath
106
     */
107
    public function init($baseStorageDefaultPath, $baseStorageSqlPath)
108
    {
109
        $storageDefaultPath = realpath($baseStorageDefaultPath);
110
        $contentSqlPath = realpath($baseStorageSqlPath);
111
112
        $this->initConfigStorage($storageDefaultPath);
113
        $this->initContentDb($contentSqlPath);
114
115
        $this->save();
116
    }
117
118
    /**
119
     * Load filebased subset and return it's contents
120
     *
121
     * @param $name
122
     * @return mixed|string
123
     * @throws \Exception
124
     */
125
    public function __get($name)
126
    {
127
        if (isset($this->$name)) {
128
            if (in_array($name, $this->fileBasedSubsets)) {
129
                return $this->$name;
130
            } else {
131
                throw new \Exception('Trying to get undefined property from Repository: ' . $name);
132
            }
133
        } else {
134
            if (in_array($name, $this->fileBasedSubsets)) {
135
                return $this->loadSubset($name);
136
            } else {
137
                throw new \Exception('Trying to get undefined property from Repository: ' . $name);
138
            }
139
        }
140
    }
141
142
    /**
143
     * Set filebased subset contents
144
     * @param $name
145
     * @param $value
146
     * @throws \Exception
147
     */
148
    public function __set($name, $value)
149
    {
150
        if (in_array($name, $this->fileBasedSubsets)) {
151
            $this->$name = $value;
152
            $changes = $name . 'Changes';
153
            $this->$changes = true;
154
        } else {
155
            throw new \Exception('Trying to persist unknown subset in repository: ' . $name . ' <br /><pre>' . print_r($value,
156
                    true) . '</pre>');
157
        }
158
    }
159
160
    /**
161
     * Persist all subsets
162
     */
163
    public function save()
164
    {
165
        $host = $this;
166
        array_map(function ($value) use ($host) {
167
            $host->saveSubset($value);
168
        }, $this->fileBasedSubsets);
169
    }
170
171
    /**
172
     * Persist subset to disk
173
     * @param $subset
174
     */
175
    public function saveSubset($subset)
176
    {
177
        $changes = $subset . 'Changes';
178
        if ($this->$changes === true) {
179
            if (!defined('JSON_PRETTY_PRINT')) {
180
                $json = json_encode($this->$subset);
181
            } else {
182
                $json = json_encode($this->$subset, JSON_PRETTY_PRINT);
183
            }
184
            $subsetStoragePath = $this->storagePath . DIRECTORY_SEPARATOR . $subset . '.json';
185
            file_put_contents($subsetStoragePath, $json);
186
187
            $this->$changes = false;
188
        }
189
    }
190
191
    /**
192
     * Load subset from disk
193
     * @param $subset
194
     * @return mixed|string
195
     */
196
    protected function loadSubset($subset)
197
    {
198
        $subsetStoragePath = $this->storagePath . DIRECTORY_SEPARATOR . $subset . '.json';
199
        $json = file_get_contents($subsetStoragePath);
200
        $json = json_decode($json);
201
        $this->$subset = $json;
202
        return $json;
203
    }
204
205
    /**
206
     * @param $contentSqlPath
207
     */
208
    protected function initContentDb($contentSqlPath)
209
    {
210
        $db = $this->getContentDbHandle();
211
        $sql = file_get_contents($contentSqlPath);
212
        $db->exec($sql);
213
    }
214
215
    /**
216
     * @param $storageDefaultPath
217
     */
218
    protected function initConfigStorage($storageDefaultPath)
219
    {
220
        $json = file_get_contents($storageDefaultPath);
221
        $json = json_decode($json);
222
        $this->initConfigIfNotExists($json, 'sitemap');
223
        $this->initConfigIfNotExists($json, 'applicationComponents');
224
        $this->initConfigIfNotExists($json, 'documentTypes');
225
        $this->initConfigIfNotExists($json, 'bricks');
226
        $this->initConfigIfNotExists($json, 'imageSet');
227
        $this->initConfigIfNotExists($json, 'images');
228
        $this->initConfigIfNotExists($json, 'files');
229
        $this->initConfigIfNotExists($json, 'users');
230
        $this->initConfigIfNotExists($json, 'valuelists');
231
        $this->initConfigIfNotExists($json, 'redirects');
232
        $this->initConfigIfNotExists($json, 'activityLog');
233
    }
234
235
    /**
236
     * @return \PDO
237
     */
238
    protected function getContentDbHandle()
239
    {
240
        if ($this->contentDbHandle === null) {
241
            $this->contentDbHandle = new \PDO('sqlite:' . $this->storagePath . DIRECTORY_SEPARATOR . 'content.db');
242
        }
243
        return $this->contentDbHandle;
244
    }
245
246
    private function initConfigIfNotExists($json, $subsetName)
247
    {
248
        $subsetFileName = $this->storagePath . DIRECTORY_SEPARATOR . $subsetName . '.json';
249
        if (file_exists($subsetFileName)) {
250
            $this->loadSubset($subsetName);
251
        } else {
252
            $changes = $subsetName . 'Changes';
253
            $this->$subsetName = $json->$subsetName;
254
            $this->$changes = true;
255
        }
256
    }
257
258
    /**
259
     * @return ContentRepository
260
     */
261
    public function getContentRepository()
262
    {
263
        return $this->contentRepository;
264
    }
265
}