Completed
Push — develop ( a0b39d...6e6eeb )
by Tino
07:33
created

Title::renderPageTitle()   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 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Copyright (c) by the ACP3 Developers.
5
 * See the LICENSE file at the top-level module directory for licensing details.
6
 */
7
8
namespace ACP3\Core\Breadcrumb;
9
10
use ACP3\Core\Breadcrumb\Event\GetSiteAndPageTitleBeforeEvent;
11
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
12
13
class Title
14
{
15
    /**
16
     * @var \ACP3\Core\Breadcrumb\Steps
17
     */
18
    protected $steps;
19
    /**
20
     * @var \Symfony\Component\EventDispatcher\EventDispatcherInterface
21
     */
22
    protected $eventDispatcher;
23
24
    /**
25
     * @var string
26
     */
27
    protected $pageTitle = '';
28
    /**
29
     * @var string
30
     */
31
    protected $pageTitlePostfix = '';
32
    /**
33
     * @var string
34
     */
35
    protected $pageTitlePrefix = '';
36
    /**
37
     * @var string
38
     */
39
    protected $pageTitleSeparator = '-';
40
    /**
41
     * @var string
42
     */
43
    private $metaTitle = '';
44
    /**
45
     * @var string
46
     */
47
    protected $siteTitleSeparator = '|';
48
    /**
49
     * @var string
50
     */
51
    protected $siteTitle = '';
52
    /**
53
     * @var string|null
54
     */
55
    private $siteSubtitle;
56
57
    /**
58
     * Title constructor.
59
     *
60
     * @param \ACP3\Core\Breadcrumb\Steps                                 $steps
61
     * @param \Symfony\Component\EventDispatcher\EventDispatcherInterface $eventDispatcher
62
     */
63
    public function __construct(Steps $steps, EventDispatcherInterface $eventDispatcher)
64
    {
65
        $this->steps = $steps;
66
        $this->eventDispatcher = $eventDispatcher;
67
    }
68
69
    /**
70
     * @return string
71
     */
72
    public function getSiteTitle()
73
    {
74
        return $this->siteTitle;
75
    }
76
77
    /**
78
     * @param string $title
79
     *
80
     * @return $this
81
     */
82
    public function setSiteTitle(string $title)
83
    {
84
        $this->siteTitle = $title;
85
86
        return $this;
87
    }
88
89
    /**
90
     * @return string|null
91
     */
92
    public function getSiteSubtitle()
93
    {
94
        return $this->siteSubtitle;
95
    }
96
97
    /**
98
     * @param string $siteSubtitle
99
     *
100
     * @return $this
101
     */
102
    public function setSiteSubtitle(string $siteSubtitle)
103
    {
104
        $this->siteSubtitle = $siteSubtitle;
105
106
        return $this;
107
    }
108
109
    /**
110
     * @return string
111
     */
112
    public function getPageTitle()
113
    {
114
        if (empty($this->pageTitle)) {
115
            $steps = $this->steps->getBreadcrumb();
116
            $lastCrumb = \end($steps);
117
118
            $this->pageTitle = $lastCrumb['title'];
119
        }
120
121
        return $this->pageTitle;
122
    }
123
124
    /**
125
     * @param string $title
126
     *
127
     * @return $this
128
     */
129
    public function setPageTitle(string $title)
130
    {
131
        $this->pageTitle = $title;
132
133
        return $this;
134
    }
135
136
    /**
137
     * @return string
138
     */
139
    public function getPageTitlePostfix()
140
    {
141
        return $this->pageTitlePostfix;
142
    }
143
144
    /**
145
     * @param string $value
146
     *
147
     * @return $this
148
     */
149
    public function setPageTitlePostfix(string $value)
150
    {
151
        $this->pageTitlePostfix = $value;
152
153
        return $this;
154
    }
155
156
    /**
157
     * @param string $value
158
     *
159
     * @return $this
160
     */
161
    public function setPageTitlePrefix(string $value)
162
    {
163
        $this->pageTitlePrefix = $value;
164
165
        return $this;
166
    }
167
168
    /**
169
     * @return string
170
     */
171
    public function getPageTitleSeparator()
172
    {
173
        return ' ' . $this->pageTitleSeparator . ' ';
174
    }
175
176
    /**
177
     * @param string $value
178
     *
179
     * @return $this
180
     */
181
    public function setPageTitleSeparator(string $value)
182
    {
183
        $this->pageTitleSeparator = $value;
184
185
        return $this;
186
    }
187
188
    /**
189
     * @return string
190
     */
191
    public function getMetaTitle(): string
192
    {
193
        return $this->metaTitle;
194
    }
195
196
    /**
197
     * @param string $metaTitle
198
     *
199
     * @return $this
200
     */
201
    public function setMetaTitle(string $metaTitle)
202
    {
203
        $this->metaTitle = $metaTitle;
204
205
        return $this;
206
    }
207
208
    /**
209
     * @return string
210
     */
211
    public function getSiteTitleSeparator()
212
    {
213
        return ' ' . $this->siteTitleSeparator . ' ';
214
    }
215
216
    /**
217
     * @param string $value
218
     *
219
     * @return $this
220
     */
221
    public function setSiteTitleSeparator(string $value)
222
    {
223
        $this->siteTitleSeparator = $value;
224
225
        return $this;
226
    }
227
228
    /**
229
     * Returns the title of the current page + the site title.
230
     *
231
     * @return string
232
     */
233
    public function getSiteAndPageTitle()
234
    {
235
        $this->eventDispatcher->dispatch(
236
            'core.breadcrumb.title.get_site_and_page_title_before',
237
            new GetSiteAndPageTitleBeforeEvent($this)
238
        );
239
240
        return $this->renderPageTitlePrefix()
241
            . $this->renderPageTitle()
242
            . $this->renderPageTitleSuffix()
243
            . $this->renderSiteTitle()
244
            . $this->renderSiteSubTitle();
245
    }
246
247
    protected function renderPageTitle(): string
248
    {
249
        return !empty($this->getMetaTitle()) ? $this->getMetaTitle() : $this->getPageTitle();
250
    }
251
252
    protected function renderPageTitlePrefix(): string
253
    {
254
        if (!empty($this->pageTitlePrefix)) {
255
            return $this->pageTitlePrefix . $this->getPageTitleSeparator();
256
        }
257
258
        return '';
259
    }
260
261
    protected function renderPageTitleSuffix(): string
262
    {
263
        if (!empty($this->getPageTitlePostfix())) {
264
            return $this->getPageTitleSeparator() . $this->getPageTitlePostfix();
265
        }
266
267
        return '';
268
    }
269
270
    protected function renderSiteTitle(): string
271
    {
272
        if (!empty($this->getSiteTitle())) {
273
            return $this->getSiteTitleSeparator() . $this->getSiteTitle();
274
        }
275
276
        return '';
277
    }
278
279
    protected function renderSiteSubTitle(): string
280
    {
281
        if (!empty($this->getSiteSubtitle())) {
282
            return $this->getPageTitleSeparator() . $this->getSiteSubtitle();
283
        }
284
285
        return '';
286
    }
287
}
288