Passed
Push — master ( b91050...6fb96f )
by Jens
02:59
created

Document   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 86
rs 10
c 1
b 0
f 0
wmc 13
lcom 1
cbo 1

4 Methods

Rating   Name   Duplication   Size   Complexity  
B __get() 0 21 7
A __set() 0 11 3
A getContent() 0 9 2
A __toString() 0 4 1
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
    public function __get($name)
49
    {
50
        if (in_array($name, $this->jsonEncodedFields)) {
51
            if (is_string($this->$name)) {
52
                return json_decode($this->$name);
53
            } else {
54
                return $this->$name;
55
            }
56
        } elseif ($name === 'content') {
57
            if ($this->dbHandle === null) {
58
                throw new \Exception('Document doesnt have a dbHandle handle. (path: ' . $this->path . ')');
59
            } else {
60
                if ($this->content === null) {
61
                    return $this->getContent();
62
                }
63
            }
64
        } elseif ($name === 'dbHandle') {
65
            throw new \Exception('Trying to get protected property repository.');
66
        }
67
        return $this->$name;
68
    }
69
70
    public function __set($name, $value)
71
    {
72
        if (in_array($name, $this->jsonEncodedFields)) {
73
            $this->$name = json_encode($value);
74
        } elseif ($name === 'content') {
75
            // Dont do anything for now..
76
            return;
77
        }
78
79
        $this->$name = $value;
80
    }
81
82
    /**
83
     * @param string $orderBy
84
     * @param string $order
85
     *
86
     * @return array
87
     * @throws \Exception
88
     */
89
    public function getContent($orderBy = 'title', $order = 'ASC')
0 ignored issues
show
Unused Code introduced by
The parameter $orderBy is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $order is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
90
    {
91
        if (empty($this->content)) {
92
            $docs = $this->documentStorage->getDocumentsWithState($this->path);
93
            $this->content = $docs;
94
        }
95
96
        return $this->content;
97
    }
98
99
    /**
100
     * @return string
101
     */
102
    public function __toString()
103
    {
104
        return 'Document:' . $this->title;
105
    }
106
107
108
}