Completed
Push — master ( 5442f7...aba4f0 )
by Tim
01:53
created

AbstractUrl   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 0
Metric Value
wmc 8
lcom 0
cbo 4
dl 0
loc 58
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
convert() 0 1 ?
B getIndexBase() 0 28 7
A prepareBase() 0 5 1
1
<?php
2
3
/**
4
 * AbstractUrl.
5
 */
6
declare(strict_types=1);
7
8
namespace HDNET\Calendarize\Service\Url;
9
10
use HDNET\Calendarize\Domain\Model\Index;
11
use HDNET\Calendarize\Domain\Repository\IndexRepository;
12
use HDNET\Calendarize\Features\SpeakingUrlInterface;
13
use HDNET\Calendarize\Service\AbstractService;
14
use HDNET\Calendarize\Utility\ConfigurationUtility;
15
use HDNET\Calendarize\Utility\HelperUtility;
16
use TYPO3\CMS\Core\Routing\Aspect\PersistedAliasMapper;
17
18
/**
19
 * AbstractUrl.
20
 */
21
abstract class AbstractUrl extends AbstractService
22
{
23
    /**
24
     * Convert the given information.
25
     *
26
     * @param $param1
27
     * @param $param2
28
     */
29
    abstract public function convert($param1, $param2);
30
31
    /**
32
     * Build the speaking base.
33
     *
34
     * @param int $indexUid
35
     *
36
     * @return string
37
     */
38
    protected function getIndexBase($indexUid): string
39
    {
40
        $indexRepository = HelperUtility::create(IndexRepository::class);
41
        $index = $indexRepository->findByUid((int) $indexUid);
42
        if (!($index instanceof Index)) {
43
            return 'idx-' . $indexUid;
44
        }
45
46
        $originalObject = $index->getOriginalObject();
47
        if (!($originalObject instanceof SpeakingUrlInterface)) {
48
            return 'idx-' . $indexUid;
49
        }
50
51
        $base = $originalObject->getRealUrlAliasBase();
52
        if (!(bool) ConfigurationUtility::get('disableDateInSpeakingUrl')) {
53
            $datePart = $index->isAllDay() ? 'Y-m-d' : 'Y-m-d-' . $GLOBALS['TYPO3_CONF_VARS']['SYS']['hhmm'];
54
            $dateInfo = $index->getStartDateComplete()
55
                ->format($datePart);
56
            $dateInfo = \preg_replace('/[^0-9\-]/', '-', $dateInfo);
57
            $base .= '-' . $dateInfo;
58
        }
59
60
        if ((bool) ConfigurationUtility::get('addIndexInSpeakingUrl') || \class_exists(PersistedAliasMapper::class)) {
61
            $base .= '-' . $indexUid;
62
        }
63
64
        return (string) $base;
65
    }
66
67
    /**
68
     * Prepare base
69
     *
70
     * @param string $base
71
     * @return string|string[]|null
72
     */
73
    protected function prepareBase(string $base): string {
74
        $result = \mb_strtolower($base);
75
76
        return \preg_replace('/[^a-z0-9\-]/', '-', $result);
77
    }
78
}
79