Passed
Push — develop ( f2da7f...a532f4 )
by Jens
03:01
created

Document::getPropertyIfExists()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 1
nc 2
nop 1
dl 0
loc 3
rs 10
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)) {
56
            if (isset($this->$name) && is_string($this->$name)) {
57
                return json_decode($this->$name);
58
            } else {
59
                return $this->getPropertyIfExists($name);
60
            }
61
        } elseif ($name === 'content') {
62
            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...
63
                throw new \Exception('Document doesnt have a dbHandle handle. (path: ' . $this->path . ')');
64
            } else {
65
                if ($this->content === null) {
66
                    return $this->getContent();
67
                }
68
            }
69
        } elseif ($name === 'dbHandle') {
70
            throw new \Exception('Trying to get protected property repository.');
71
        }
72
        return $this->getPropertyIfExists($name);
73
    }
74
75
    /**
76
     * @param $name
77
     * @param $value
78
     */
79
    public function __set($name, $value)
80
    {
81
        if (in_array($name, $this->jsonEncodedFields)) {
82
            $this->$name = json_encode($value);
83
        } elseif ($name === 'content') {
84
            // Dont do anything for now..
85
            return;
86
        }
87
88
        $this->$name = $value;
89
    }
90
91
    /**
92
     * @return array
93
     */
94
    public function getContent()
95
    {
96
        if (empty($this->content)) {
97
            $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...
98
            $this->content = $docs;
99
        }
100
101
        return $this->content;
102
    }
103
104
    /**
105
     * @return string
106
     */
107
    public function __toString()
108
    {
109
        return 'Document:' . $this->title;
110
    }
111
112
    /**
113
     * @param $name
114
     * @return array
115
     */
116
    private function getPropertyIfExists($name)
117
    {
118
        return isset($this->$name) ? $this->$name : array('');
119
    }
120
121
122
}