Ajde_Document   B
last analyzed

Complexity

Total Complexity 39

Size/Duplication

Total Lines 224
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 8

Importance

Changes 0
Metric Value
dl 0
loc 224
rs 8.2857
c 0
b 0
f 0
wmc 39
lcom 2
cbo 8

25 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A fromRoute() 0 12 2
A setLayout() 0 9 2
A getLayout() 0 9 2
A setBody() 0 4 1
A getBody() 0 8 2
A setTitle() 0 4 1
A getTitle() 0 4 2
A getFullTitle() 0 12 2
A setDescription() 0 4 1
A getDescription() 0 8 2
A setAuthor() 0 4 1
A getAuthor() 0 8 2
A render() 0 4 1
A getCacheControl() 0 4 1
A setCacheControl() 0 4 1
A getContentType() 0 4 1
A setContentType() 0 4 1
A getMaxAge() 0 4 1
A setMaxAge() 0 4 1
A addResource() 0 3 1
A getResourceTypes() 0 3 1
A setContentTypeHeader() 0 4 1
A setCacheControlHeader() 0 4 1
C registerDocumentProcessor() 0 25 7
1
<?php
2
3
abstract class Ajde_Document extends Ajde_Object_Standard
4
{
5
    const CACHE_CONTROL_PUBLIC = 'public';
6
    const CACHE_CONTROL_PRIVATE = 'private';
7
    const CACHE_CONTROL_NOCACHE = 'no-cache';
8
9
    protected $_cacheControl = self::CACHE_CONTROL_PUBLIC;
10
    protected $_contentType = 'text/html';
11
    protected $_maxAge = 604800; // 1 week
12
13
    public function __construct()
14
    {
15
        $this->setFormat(strtolower(str_replace('Ajde_Document_Format_', '', get_class($this))));
0 ignored issues
show
Documentation Bug introduced by
The method setFormat does not exist on object<Ajde_Document>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
16
    }
17
18
    /**
19
     * @param Ajde_Core_Route $route
20
     *
21
     * @return Ajde_Document
22
     */
23
    public static function fromRoute(Ajde_Core_Route $route)
24
    {
25
        $format = $route->getFormat();
26
        $documentClass = 'Ajde_Document_Format_'.ucfirst($format);
27
        if (!class_exists($documentClass)) {
28
            $exception = new Ajde_Core_Exception_Routing("Document format $format not found",
29
                90009);
30
            Ajde::routingError($exception);
31
        }
32
33
        return new $documentClass();
34
    }
35
36
    /**
37
     * @return Ajde_Layout
38
     */
39
    public function setLayout(Ajde_Layout $layout)
40
    {
41
        if (!$layout instanceof Ajde_Layout) {
42
            $layout = new Ajde_Layout($layout);
43
        }
44
        $layout->setDocument($this);
45
46
        return $this->set('layout', $layout);
47
    }
48
49
    /**
50
     * @return Ajde_Layout
51
     */
52
    public function getLayout()
53
    {
54
        if (!$this->hasLayout()) {
0 ignored issues
show
Documentation Bug introduced by
The method hasLayout does not exist on object<Ajde_Document>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
55
            // Load default layout into document
56
            $this->setLayout(new Ajde_Layout(config('layout.frontend')));
57
        }
58
59
        return $this->get('layout');
60
    }
61
62
    /**
63
     * @param string $contents
64
     */
65
    public function setBody($contents)
66
    {
67
        $this->set('body', $contents);
68
    }
69
70
    /**
71
     * @return string
72
     */
73
    public function getBody()
74
    {
75
        if ($this->has('body')) {
76
            return $this->get('body');
77
        } else {
78
            return '';
79
        }
80
    }
81
82
    public function setTitle($title)
83
    {
84
        $this->set('title', $title);
85
    }
86
87
    public function getTitle()
88
    {
89
        return $this->has('title') ? $this->get('title') : trans('Untitled page');
90
    }
91
92
    public function getFullTitle()
93
    {
94
        $projectTitle = config('app.title');
95
        if ($this->has('title')) {
96
            return sprintf(config('layout.titleFormat'),
97
                $projectTitle,
98
                $this->get('title')
99
            );
100
        } else {
101
            return $projectTitle;
102
        }
103
    }
104
105
    public function setDescription($description)
106
    {
107
        $this->set('description', $description);
108
    }
109
110
    public function getDescription()
111
    {
112
        if ($this->has('description')) {
113
            return $this->get('description');
114
        } else {
115
            return config('app.description');
116
        }
117
    }
118
119
    public function setAuthor($author)
120
    {
121
        $this->set('author', $author);
122
    }
123
124
    public function getAuthor()
125
    {
126
        if ($this->has('author')) {
127
            return $this->get('author');
128
        } else {
129
            return config('app.author');
130
        }
131
    }
132
133
    public function render()
134
    {
135
        return $this->getLayout()->getContents();
136
    }
137
138
    public function getCacheControl()
139
    {
140
        return $this->_cacheControl;
141
    }
142
143
    public function setCacheControl($cacheControl)
144
    {
145
        $this->_cacheControl = $cacheControl;
146
    }
147
148
    public function getContentType()
149
    {
150
        return $this->_contentType;
151
    }
152
153
    public function setContentType($mimeType)
154
    {
155
        $this->_contentType = $mimeType;
156
    }
157
158
    public function getMaxAge()
159
    {
160
        return (int) $this->_maxAge;
161
    }
162
163
    public function setMaxAge($days)
164
    {
165
        $this->_maxAge = (int) (60 * 60 * 24 * $days);
166
    }
167
168
    /**
169
     * @param Ajde_Resource $resource
170
     */
171
    public function addResource(Ajde_Resource $resource)
172
    {
173
    }
174
175
    public function getResourceTypes()
176
    {
177
    }
178
179
    // Render helpers
180
181
    /**
182
     * @deprecated
183
     *
184
     * @throws Ajde_Core_Exception_Deprecated
185
     */
186
    protected function setContentTypeHeader($contentType = null)
0 ignored issues
show
Unused Code introduced by
The parameter $contentType 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...
187
    {
188
        throw new Ajde_Core_Exception_Deprecated();
189
    }
190
191
    /**
192
     * @deprecated
193
     *
194
     * @throws Ajde_Core_Exception_Deprecated
195
     */
196
    protected function setCacheControlHeader($cacheControl = null)
0 ignored issues
show
Unused Code introduced by
The parameter $cacheControl 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...
197
    {
198
        throw new Ajde_Core_Exception_Deprecated();
199
    }
200
201
    public static function registerDocumentProcessor($format, $registerOn = 'layout')
202
    {
203
        $documentProcessors = config('layout.filters.documentProcessors');
204
        if (is_array($documentProcessors) && isset($documentProcessors[$format])) {
205
            foreach ($documentProcessors[$format] as $processor) {
206
                $processorClass = 'Ajde_Document_Processor_'.ucfirst($format).'_'.$processor;
207
                if (!class_exists($processorClass)) {
208
                    // TODO:
209
                    throw new Ajde_Exception('Processor '.$processorClass.' not found', 90022);
210
                }
211
                if ($registerOn == 'layout') {
212
                    Ajde_Event::register('Ajde_Layout', 'beforeGetContents', $processorClass.'::preProcess');
213
                    Ajde_Event::register('Ajde_Layout', 'afterGetContents', $processorClass.'::postProcess');
214
                } elseif ($registerOn == 'compressor') {
215
                    Ajde_Event::register('Ajde_Resource_Local_Compressor', 'beforeCompress',
216
                        $processorClass.'::preCompress');
217
                    Ajde_Event::register('Ajde_Resource_Local_Compressor', 'afterCompress',
218
                        $processorClass.'::postCompress');
219
                } else {
220
                    // TODO:
221
                    throw new Ajde_Exception('Document processor must be registered on either \'layout\' or \'compressor\'');
222
                }
223
            }
224
        }
225
    }
226
}
227