Passed
Push — develop ( e356f7...3769a4 )
by Jens
02:12
created

Document::__get()   C

Complexity

Conditions 8
Paths 7

Size

Total Lines 22
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 12
nc 7
nop 1
dl 0
loc 22
rs 6.6037
c 0
b 0
f 0
1
<?php
2
/**
3
 * User: Jens
4
 * Date: 17-2-2017
5
 * Time: 13:00
6
 */
7
8
namespace CloudControl\Cms\storage\entities;
9
10
use CloudControl\Cms\storage\storage\DocumentStorage;
11
12
/**
13
 * Class Document
14
 * @package CloudControl\Cms\storage
15
 * @property array fields
16
 * @property array bricks
17
 * @property array dynamicBricks
18
 * @property array content
19
 * @property-write \PDO dbHandle
20
 * @property-write DocumentStorage documentStorage
21
 * @property boolean unpublishedChanges
22
 */
23
class Document
24
{
25
    public $id;
26
    public $path;
27
    public $title;
28
    public $slug;
29
    public $type;
30
    public $documentType;
31
    public $documentTypeSlug;
32
    public $state;
33
    public $lastModificationDate;
34
    public $creationDate;
35
    public $lastModifiedBy;
36
    protected $fields;
37
    protected $bricks;
38
    protected $dynamicBricks;
39
    protected $content;
40
41
    protected $dbHandle;
42
43
    protected $jsonEncodedFields = array('fields', 'bricks', 'dynamicBricks');
44
    protected $orderableFields = array('title', 'slug', 'type', 'documentType', 'documentTypeSlug', 'state', 'lastModificationDate', 'creationDate', 'lastModifiedBy');
45
46
    public static $DOCUMENT_STATES = array('published', 'unpublished');
47
48
    /**
49
     * @param $name
50
     * @return array|mixed
51
     * @throws \Exception
52
     */
53
    public function __get($name)
54
    {
55
        if (in_array($name, $this->jsonEncodedFields, true)) {
56
            if (isset($this->$name) && is_string($this->$name)) {
57
                return $this->decodeJsonToFieldContainer($name);
58
            }
59
60
            return $this->getPropertyIfExists($name);
61
        }
62
63
        if ($name === 'content') {
64
            if ($this->dbHandle === null) {
0 ignored issues
show
introduced by
The property dbHandle is declared write-only in CloudControl\Cms\storage\entities\Document.
Loading history...
65
                throw new \RuntimeException('Document doesnt have a dbHandle handle. (path: ' . $this->path . ')');
66
            }
67
68
            if ($this->content === null) {
69
                return $this->getContent();
70
            }
71
        } elseif ($name === 'dbHandle') {
72
            throw new \RuntimeException('Trying to get protected property repository.');
73
        }
74
        return $this->getPropertyIfExists($name);
75
    }
76
77
    /**
78
     * @param $name
79
     * @param $value
80
     */
81
    public function __set($name, $value)
82
    {
83
        if (in_array($name, $this->jsonEncodedFields, true)) {
84
            $this->$name = json_encode($value);
85
        } elseif ($name === 'content') {
86
            // Dont do anything for now..
87
            return;
88
        }
89
90
        $this->$name = $value;
91
    }
92
93
    /**
94
     * @return array
95
     */
96
    public function getContent()
97
    {
98
        if (empty($this->content)) {
99
            $docs = $this->documentStorage->getDocumentsWithState($this->path);
0 ignored issues
show
introduced by
The property documentStorage is declared write-only in CloudControl\Cms\storage\entities\Document.
Loading history...
100
            $this->content = $docs;
101
        }
102
103
        return $this->content;
104
    }
105
106
    /**
107
     * @return string
108
     */
109
    public function __toString()
110
    {
111
        return 'Document:' . $this->title;
112
    }
113
114
    /**
115
     * @param $name
116
     * @return array
117
     */
118
    private function getPropertyIfExists($name)
119
    {
120
        return isset($this->$name) ? $this->$name : array('');
121
    }
122
123
    /**
124
     * @param $name
125
     * @return mixed
126
     */
127
    private function decodeJsonToFieldContainer($name)
128
    {
129
        $stdObj = json_decode($this->$name);
130
        $temp = serialize($stdObj);
131
        $temp = preg_replace('@^O:8:"stdClass":@', 'O:' . strlen(FieldContainer::class) . ':"' . FieldContainer::class . '":', $temp);
132
        return unserialize($temp);
133
    }
134
135
136
}