Passed
Push — develop ( 7ee7eb...278ae4 )
by Jens
02:44
created

Repository::loadSubset()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 6
nc 1
nop 1
dl 0
loc 8
rs 9.4285
c 1
b 0
f 0
1
<?php
2
/**
3
 * User: Jens
4
 * Date: 30-1-2017
5
 * Time: 20:15
6
 */
7
8
namespace library\storage;
9
10
11
class Repository
12
{
13
    protected $storagePath;
14
15
    protected $fileBasedSubsets = array('sitemap', 'applicationComponents', 'documentTypes', 'bricks', 'imageSet', 'images', 'files', 'users');
16
17
    protected $sitemap;
18
    protected $sitemapChanges = false;
19
20
    protected $applicationComponents;
21
    protected $applicationComponentsChanges = false;
22
23
    protected $documentTypes;
24
    protected $documentTypesChanges = false;
25
26
    protected $bricks;
27
    protected $bricksChanges = false;
28
29
    protected $imageSet;
30
    protected $imageSetChanges = false;
31
32
    protected $images;
33
    protected $imagesChanges = false;
34
35
    protected $files;
36
    protected $filesChanges = false;
37
38
    protected $users;
39
    protected $usersChanges = false;
40
41
    /**
42
     * Repository constructor.
43
     */
44
    public function __construct($storagePath)
45
    {
46
        $storagePath = realpath($storagePath);
47
        if (is_dir($storagePath) && $storagePath !== false) {
48
            $this->storagePath = $storagePath;
49
        } else {
50
            throw new \Exception('Repository not yet initialized.');
51
        }
52
    }
53
54
    public static function create($storagePath)
55
    {
56
        return mkdir($storagePath);
57
    }
58
59
    public function init()
60
    {
61
        $storageDefaultPath = realpath('../library/cc/install/_storage.json');
62
        $json = file_get_contents($storageDefaultPath);
63
        $json = json_decode($json);
64
        $this->sitemap = $json->sitemap;
65
        $this->sitemapChanges = true;
66
        $this->applicationComponents = $json->applicationComponents;
67
        $this->applicationComponentsChanges = true;
68
        $this->documentTypes = $json->documentTypes;
69
        $this->documentTypesChanges = true;
70
        $this->bricks = $json->bricks;
71
        $this->bricksChanges = true;
72
        $this->imageSet = $json->imageSet;
73
        $this->imageSetChanges = true;
74
        $this->images = $json->images;
75
        $this->imagesChanges = true;
76
        $this->files = $json->files;
77
        $this->filesChanges = true;
78
        $this->users = $json->users;
79
        $this->usersChanges = true;
80
        $this->save();
81
    }
82
83
    public function __get($name)
84
    {
85
        if (isset($this->$name)) {
86
            if (in_array($name, $this->fileBasedSubsets)) {
87
                return $this->$name;
88
            } else {
89
                dump();
90
            }
91
        } else {
92
            if (in_array($name, $this->fileBasedSubsets)) {
93
                return $this->loadSubset($name);
94
            } else {
95
                throw new \Exception('Trying to get undefined property from Repository: ' . $name);
96
            }
97
        }
98
    }
99
100
    public function __set($name, $value)
101
    {
102
        if (in_array($name, $this->fileBasedSubsets)) {
103
            $this->$name = $value;
104
            $changes = $name . 'Changes';
105
            $this->$changes = true;
106
        } else {
107
            throw new \Exception('Trying to persist unknown subset in repository: ' . $name . ' <br /><pre>' . print_r($value, true) . '</pre>');
108
        }
109
    }
110
111
    public function save()
112
    {
113
        $this->sitemapChanges ? $this->saveSubset('sitemap') : null;
114
        $this->applicationComponentsChanges ? $this->saveSubset('applicationComponents') : null;
115
        $this->documentTypesChanges ? $this->saveSubset('documentTypes') : null;
116
        $this->bricksChanges ? $this->saveSubset('bricks') : null;
117
        $this->imageSetChanges ? $this->saveSubset('imageSet') : null;
118
        $this->imagesChanges ? $this->saveSubset('images') : null;
119
        $this->filesChanges ? $this->saveSubset('files') : null;
120
        $this->usersChanges ? $this->saveSubset('users') : null;
121
    }
122
123
    protected function saveSubset($subset)
124
    {
125
        $json = json_encode($this->$subset);
126
        $subsetStoragePath = $this->storagePath . DIRECTORY_SEPARATOR . $subset . '.json';
127
        file_put_contents($subsetStoragePath, $json);
128
        $changes = $subset . 'Changes';
129
        $this->$changes = false;
130
    }
131
132
    protected function loadSubset($subset)
133
    {
134
        $subsetStoragePath = $this->storagePath . DIRECTORY_SEPARATOR . $subset . '.json';
135
        $json = file_get_contents($subsetStoragePath);
136
        $json = json_decode($json);
137
        $this->$subset = $json;
138
        return $json;
139
    }
140
}